-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql
119 lines (81 loc) · 6.53 KB
/
mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
浅谈 mysql
一.
1.SQL 分类
sql语句主要可以划分为一下3个类别
1.1 DDL (data definition language)语句:数据定义语言,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象的定义。
常用的语句关键字主要包括 create 、drop 、alter等。
1.2 DML (data manipulation language)语句:数据库操纵语句,用于添加、删除、更新和查询数据记录,并检查数据完整性,常用的语句
关键字主要包括 insert、delete、uodate和select等。
1.3 DCL (data control language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句。这些语句定义了数据库、表、
字段、用户的访问权限和安全级别。主要的语句关键字包括 grant、revoke等。
2.DDL 语句
2.1.mysql的表名是以目录的形式保存在磁盘上,所以表名的字符可以用任何目录名允许的字符。
2.2查看表的结构
show create table table_name \G;
"\G" 选项的含义是使得记录能够按照字段竖着排序,对于内容比较长的记录更容易显示。
2.3 修改字段的排列顺序
修改表(alter table add/change/modify)中,都有哦一个可选项 first/after column_name,这个选项可以用来修改字段在表中的位置,默认
add 增加的新字段是加在表的最后位置,而change/modify 默认不会改变字段的位置。
alter table emp add birth date after ename; //将生日字段添加到 ename 字段后边
2.4 修改字段 age ,将它放在最前面
alter table emp modify age int(3) first; // modify age 表示 age 字段类型等信息都不变 first 是将 age 字段放置到最前面
2.5 修改表名,语法如下:
alter table table_name rename [to] new_table_name;
2. DML 语句
DML 操作是指对数据库中表记录的曹组,主要包括表记录的插入 (insert)、更新(update)、删除(delete)和查询(select),是开发人员
日常使用最频繁的操作。
基于表:
create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2));
(1) 查询不重复的记录
有时候需要将表中的记录去掉重复后显示出来,可以用distinct关键字来实现:
select ename,hiredate,sal,deptno from emp;
(2) 排序和限制
我们经常会有这样的需求,取出按照某个字段进行排序后的记录结果集,这就用到了数据库的排序操作,用关键字order by 来实现,语法如下:
select * from table_name [where condition] [order by field1 [desc|asc],field2 [desc|asc],...fieldn [desc|asc]]
其中,desc 和 asc 是排序顺序关键字,desc 表示按照字段进行降序排列,asc 则表示升序排序,如果不写此关键字默认是升序排列。order by
后面可以跟多个不同的排序字段,并且每个排序字段可以有不同的排序顺序。
例如,把 emp 表中的记录按照工资高低进行显示:
select * from emp order by sal;
如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序,以此类推。如果只有一个排序字段,则这些字段相同的记录将会无序排列。
例如,把 emp 表中的记录按照部门编号 deptno 字段排序:
select * from emp order by deptno;
对于 deptno 相同的前两条记录,如果要按照工资由高到低排序,可以使用以下命令:
select * from emp order by deptno,sal desc;
对于排序后的记录,如果希望只显示一部分,而不是全部,这时,就可以使用 LIMIT 关键字
来实现,LIMIT 的语法如下:
SELECT ……[LIMIT offset_start,row_count]
其中 offset_start 表示记录的起始偏移量,row_count 表示显示的行数。
在默认情况下,起始偏移量为 0,只需要写记录行数就可以,这时候,显示的实际就是前 n条记录,看下面例子:
例如,显示 emp 表中按照 sal 排序后的前 3 条记录:
select * from emp order by sal limit 3;
如果要显示 emp 表中按照 sal 排序后从第二条记录开始,显示 3 条记录:
select * from emp order by sal limit 1,3;
limit 经常和 order by 一起配合使用来进行记录的分页显示。
注意:limit 属于 MySQL 扩展 SQL92 后的语法,在其他数据库上并不能通用。
(3)聚合
很多情况下,我们需要进行一些汇总操作,比如统计整个公司的人数或者统计每个部门的人数,这个时就要用到 SQL 的聚合操作。
聚合操作的语法如下:
SELECT [field1,field2,……fieldn] fun_name
FROM tablename
[WHERE where_contition]
[GROUP BY field1,field2,……fieldn
[WITH ROLLUP]]
[HAVING where_contition]
对其参数进行以下说明。
fun_name 表示要做的聚合操作,也就是聚合函数,常用的有 sum(求和)、count(*)(记录数)、max(最大值)、min(最小值)。
GROUP BY 关键字表示要进行分类聚合的字段,比如要按照部门分类统计员工数量,部门就应该写在 group by 后面。
WITH ROLLUP 是可选语法,表明是否对分类聚合后的结果进行再汇总。
HAVING 关键字表示对分类后的结果再进行条件的过滤。
注意:having 和 where 的区别在于 having 是对聚合后的结果进行条件的过滤,而 where 是在聚
合前就对记录进行过滤,如果逻辑允许,我们尽可能用 where 先过滤记录,这样因为结果
集减小,将对聚合的效率大大提高,最后再根据逻辑看是否用 having 进行再过滤。
例如,要 emp 表中统计公司的总人数:
select count(1) from emp;
在此基础上,要统计各个部门的人数:
select deptno,count(1) from emp group by deptno;
更细一些,既要统计各部门人数,又要统计总人数:
select deptno,count(1) from emp group by deptno with rollup;
统计人数大于 1 人的部门:
select deptno , count(1) as total from emp group by deptno having total > 1;
最后统计公司所有员工的薪水总额、最高和最低薪水:
select sum(sal),max(sal),min(sal) from emp;