|
发表于 2018-4-28 08:00:00
数据库的基本操作命令
1.登录MySQL
- -- 进入数据库的方法一
- mysql -uroot -pmysql # mysql 数据库密码(显示)
- -- 进入数据库的方法二
- mysql -uroot -p # 隐藏密码输入
复制代码
2.数据库的基本操作
- -- 显示数据库版本(记得加;)
- select version();
- -- 显示当前的时间
- select now();
- -- 查看所有数据库
- show databases;
- -- 创建数据库
- create database 数据库名 charset=utf8;
- -- 创建淘宝数据库
- create database taobao;
- -- 创建淘宝数据库并指定编码
- create database taobao charset=utf8;
- -- 查看创建数据库的语句
- show create database school
复制代码
3.使用数据库taobao数据库
- -- 使用数据库
- use school;
- -- 显示数据库中所有的表
- show tables;
- --删除数据库
- drop database school;
复制代码
4.数据表的基本操作
- -- auto_increment :自动增长
- -- not null :表示不为空
- -- primary key :表示主键
- -- default :默认值
复制代码
- -- 查看当前的数据库中所有的表
- -- show tables;
- -- 创建students数据表
- create table students(
- id int unsigned not null auto_increment primary key,
- name varchar(50) not null default "张三",
- age tinyint unsigned not null default 18,
- high decimal(5,2) not null,
- gender enum("男", "女", "保密")default "保密",
- cls_id int unsigned not null
- );
- -- 插入一条数据到students表中
- insert into students values(0, "mike", 18, 145,"保密",2)
- -- 查询students表中的所有的数据
- select * from students;
- -- 查看创建表的语句
- show create table students;
- -- 删除表
- drop table students;
- -- 查看表的字段
- desc students;
- -- 添加表的字段
- alter table students add birth datetime;
- -- 修改字段:不改变字段名字
- alter table students modify birth date;
- -- 修改字段:不重命名版
- alter table students change birth birthday date default "2020-01-01";
- -- 删除字段
- alter table students drop cls_id;
- -- 插入数据 insert into 表明 value(...)
- -- 主键可以用 0 null default来占位
- insert into students values(null, "lily", 22, 168, 2, "1990-01-01");
- -- 部分插入
- insert into students(high) values(172);
- -- 多行插入
- insert into students(name, high) values("李四", 178),("老王", 1.44);
- -- 多行插入全部数据
- insert into students values(null, "lily", 23, 173, 2, "1990-01-01"), (null, "xiao", 22, 189, 2, "1990-02-03");
- -- 修改表
- -- 修改全部年龄
- update students set age= 30;
- -- 修改指定id的年龄
- update students set age=28 where id=1;
- --查询表的内容
- select * from students;
- -- 定条件查询
- select * from students where id=2;
- select * from students where id>=1 and id<=3;
- --查询指定的列
- select id, name from students where id>=1 and id<=3;
- select name, id from students where id>=1 and id<=3;
- -- 可以用as来为列表指定别名(显示出来的名字就是指定的名字)
- select name as 姓名, id as 学号 from students where id>=1 and id<=3;
- -- 物理删除
- delete from student where id=6;
- --逻辑删除(用新的字段作为条件限制显示信息)
- alter table students add is_delete bit default 0;
- -- 把id=1的is_delete改为1
- update students set is_delete=1 where id=1;
- -- 查询然后条件限制为is_delete=0 就可以隐藏数据
- select * from students where is_delete=0;
复制代码
5.数据表的查询操作
- -- 查询所有字段
- select * from students;
- -- 查询指定字段
- select name, age from students;
- -- 给字段起别名(用别名显示)
- select name as 姓名, age as 年龄 from students;
- -- 从指定的表中寻找指定的字段
- select students.name, students.age from students;
- -- 用as起别名再用别名调用字段
- select s.name, s.age from students as s;
- -- 利用distinct字段消除重复行
- select distinct gender from students;
- -- 条件查询(比较运算符)
- select * from students where age>19;
- select * from students where age<19;
- select * from students where age!=18;
- -- 条件查询(逻辑运算符)
- select * from students where age>=17 and age<=27;
- select * from students where age>=13 or high>=159;
- select * from students where not(age<=17 and gender=2);
- -- 模糊查询
- -- 查询以"李"开头的所有名字
- select * from students where name like "李%";
- -- 查询以"王"字结尾的所有名字
- select * from students where name like "%王";
- -- 查询有"三"的所有名字
- select * from students where name like "%三%";
- -- 查询有两个字的名字
- select * from students where name like "__";
- -- 查询有三个字的名字
- select * from students where name like "___";
- -- 查询至少有两个的名字
- select * from students where name like "%__%";
- -- 空判断is null
- select * from students where high is null;
复制代码
6.数据表内数据的排序
- -- order by 字段 查询改字段内的的排序
- -- asc从小到大排序,默然从小到大
- -- desc 从大到小排序
- select * from students where (age between 18 and 26)and gender=2 order by age;
- -- 查询students表中,年纪17到30岁的女性 身高从高到矮
- select * from students where (age between 17 and 30) and gender=2 order by high desc;
- -- order by 多个字段
- -- 查询students表中,按high 降序, age升序
- select * from students where(age between 17 and 37) and gender=2 order by high desc ,age asc;
复制代码
7.数据表的集合函数
- -- 聚合函数
- -- count(*)统计列数,count(字段)一样
- select count(*) from students where gender=2;
- -- 最大值,最小值,求和,平均
- select max(age), min(age),sum(age),avg(age) from students;
复制代码
8.分组
- -- group by 按照性别分组
- select gender from students group by gender;
- -- 在students表中,计算每个性别的人数
- select gender, count(*) from students group by gender;
- --在students表中,通过性别分组显示名字
- select gender, group_concat(name) from students group by gender;
- -- group by + having :用来分组查询后指定一些条件的查询结果
- select gender,count(*) from students group by gender having count(*)>2;
复制代码
9.分页
- -- 查询前3行女生的信息
- select * from students where is_delete=0 limit(n-1)*m,n
- select * from students where gender=2 limit 0,3;
复制代码
10.连接查询
- -- 先建立一个班级表
- -- 内连接查询班级表和学生表
- select * from students inner join classes on students.cls_id=classes.id;
- -- 左连接查询班级表和学生表
- select * from students as s left join classes as c on c.cls_id=c.id;
- -- 右连接查询班级表和学生表
- select * from students as s right join classes as c on s.cls_id=c.id;
复制代码
11.自关联
- -- 表中的某一列,关联表中的另一列,这就是自关联
复制代码
12.子查询
- -- 在一个select语句中,嵌入另外一个select语句,那么嵌入的select语句被称为子查询语句
- -- 子查询是嵌入到主查询中
- -- 子查询是辅助主查询的,要么充当条件,要么充当数据源
- -- 子查询是可以独立存在的语句,是一条完整的 select 语句
- -- 标准子查询
- select * from students where age > (select avg(age) from students);
- -- 列级子查询
- select name from classes where id in (select id from students);
- -- 行级子查询
- select * from students where (height,age) = (select max(height),max(age) from students);
复制代码
13.视图
- -- 有个查询结果,这个结果表作为创建视图基础,这个结果中不能出现同名字段
- select g.id, g.name, c.name as cate_name, b.name as brand_name, g.price
- from goods as g inner join goods_brands as b on g.brand_id=b.id inner join
- goods_cates as c on c.id=g.cate_id;
- -- 新建了一个视图,这个视图它是一个虚拟表,这个表字段是原表字段的引用,可以简单理解为软链接
- create view v_info as select g.id, g.name, c.name as cate_name, b.name as brand_name, g.price
- from goods as g inner join goods_brands as b on g.brand_id=b.id inner join
- goods_cates as c on c.id=g.cate_id;
- -- 删除视图
- drop view v_info
复制代码
14.事务
- 1.原子性
- 一个事务必须被视为一个不可分割的最小工作单元,整个事务中的所有操作要么全部提交成功,要么全部失败回滚,对于一个事务来说,不可能只执行其中的一部分操作,这就是事务的原子性
- 2.一致性
- 数据库总是从一个一致性的状态转换到另一个一致性的状态。(在前面的例子中,一致性确保了,即使在执行第三、四条语句之间时系统崩溃,支票账户中也不会损失200美元,因为事务最终没有提交,所以事务中所做的修改也
- 不会保存到数据库中。)
- 3.隔离性
- 通常来说,一个事务所做的修改在最终提交以前,对其他事务是不可见的。(在前面的例子中,当执行完第三条语句、第四条语句还未开始时,此时有另外的一个账户汇总程序开始运行,则其看到支票帐户的余额并没有被减去200美元。)
- 4.持久性
- 一旦事务提交,则其所做的修改会永久保存到数据库。(此时即使系统崩溃,修改的数据也不会丢失。)
- -- 开启事务
- begin;
- start transaction;
- -- 提交事务
- commit;
- -- 回滚事务
- rollback;
复制代码
15.索引
- 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。
- 更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度(创建索引会缩短执行的时间)
- -- 查看索引
- show index from 表名;
- --创建索引
- create index 索引名称 on 表名(字符段名称(长度))
- --删除索引:
- drop index 索引名称 on 表名;
- --查询
- --开启运行时间
- set profiling=1;
- --查看执行时间
- show profiles;
复制代码
|
|