记录相关操作

1 介绍

MySQL数据操作: DML

在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括

  1. 使用INSERT实现数据的插入
  2. UPDATE实现数据的更新
  3. 使用DELETE实现数据的删除
  4. 使用SELECT查询数据以及

 

2 插入数据INSERT

1. 插入完整数据(顺序插入)
    语法一:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);

    语法二:
    INSERT INTO 表名 VALUES (值1,值2,值3…值n);

2. 指定字段插入数据
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);

3. 插入多条记录
    语法:
    INSERT INTO 表名 VALUES
        (值1,值2,值3…值n),
        (值1,值2,值3…值n),
        (值1,值2,值3…值n);

4. 插入查询结果
    语法:
    INSERT INTO 表名(字段1,字段2,字段3…字段n) 
                    SELECT (字段1,字段2,字段3…字段n) FROM 表2
                    WHERE …;

 

3 更新数据UPDATE

语法:
UPDATE 表名 SET
    字段1=值1,
    字段2=值2,
    WHERE CONDITION;

示例:
UPDATE mysql.user SET password=password(‘123’) where user=’root’and host=’localhost’;

 

4 删除数据DELETE

语法:
DELETE FROM 表名 
    WHERE CONITION;

示例:
DELETE FROM mysql.user WHERE password=’’;

 

5 查询数据SELECT

5.1 单表查询

5.1.1 单表查询的语法

SELECT DISTINCT 字段1,字段2... FROM 表名
        WHERE 条件
        GROUP BY field 分组
        HAVING 筛选         
        ORDER BY field 排序
        LIMIT 限制条数

5.1.2 关键字的执行优先级(重点)

重点中的重点:关键字的执行优先级
from       #找到表:from
where      #拿着where指定的约束条件,去文件/表中取出一条条记录
group by   #将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组
having     #将分组的结果进行having过滤
select     #执行select
distinct   #去重
order by   #将结果按条件排序
limit      #限制结果的显示条数

虽然执行顺序和书写顺序不一致 ,按照书写顺序的方式写sql,select * 先用*号占位,之后去补全后面的sql语句,最后将*号替换后你想要的具体字段。


5.1.3 where筛选条件

作用:是对整体数据的一个筛选操作

# where字句中可以使用:

1. 比较运算符:> < >= <= <> !=
2. between 80 and 100 值在80到100之间
3. in(80,90,100) 值是80或90或100
4. like 'egon%'
    pattern可以是%或_,
    %表示任意多字符
    _表示一个字符
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not

示例

#1:单条件查询
    SELECT name FROM employee
        WHERE post='sale';
        
#2:多条件查询
    SELECT name,salary FROM employee
        WHERE post='teacher' AND salary>10000;

#3:关键字BETWEEN AND
    SELECT name,salary FROM employee 
        WHERE salary BETWEEN 10000 AND 20000;

    SELECT name,salary FROM employee 
        WHERE salary NOT BETWEEN 10000 AND 20000;
    
#4:关键字IS NULL(判断某个字段是否为NULL不能用等号,需要用IS)
    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NULL;

    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NOT NULL;
        
    SELECT name,post_comment FROM employee 
        WHERE post_comment=''; 注意''是空字符串,不是null
    ps:
        执行
        update employee set post_comment='' where id=2;
        再用上条查看,就会有结果了

#5:关键字IN集合查询
    SELECT name,salary FROM employee 
        WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ;
    
    SELECT name,salary FROM employee 
        WHERE salary IN (3000,3500,4000,9000) ;

    SELECT name,salary FROM employee 
        WHERE salary NOT IN (3000,3500,4000,9000) ;

#6:关键字LIKE模糊查询
    通配符’%’ 匹配任意多个字符
    SELECT * FROM employee 
        WHERE name LIKE 'eg%';

    通配符’_’ 匹配任意单个字符
    SELECT * FROM employee 
        WHERE name LIKE 'al__';
        
# 查询员工姓名是由四个字符组成的 姓名和薪资  char_length() 和 通配符'_' 
select name,salary from employee where name like '____';
select name,salary from employee where char_length(name) = 4;

5.1.4 分组查询 GROUP BY

一、什么是分组?为什么要分组?

#1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的

#2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等

#3、为何要分组呢?
    取每个部门的最高工资
    取每个部门的员工数
    取男人数和女人数
    取每个国家之间数据统计

小窍门:‘每’这个字后面的字段,就是我们分组的依据

#4、大前提:
可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数
比如:按照部门分组
select * from emp group by post;
"""
分组之后 最小可操作单位应该是组 不再是组内的单个数据
上述命令在你没有设置严格模式的时候是可正常执行的,返回的是分组之后每个组的第一条数据,但是这不符合分组的规范
分组之后不应该考虑单个数据,而应该以组为操作单位(分组之后 没办法直接获取组内单个数据)
如果设置了严格模式 那么上述命令会直接报错 
"""
set global sql_mode = 'only_full_group_by';

设置严格模式之后,分组默认只能拿到分组的依据
select post from emp group by post;  
按照什么分组就只能拿到分组,其他字段不能直接获取,需要借助于一些方法(聚合函数)

 二、聚合函数

#强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组

示例:
    SELECT COUNT(*) FROM employee;
    SELECT COUNT(*) FROM employee WHERE depart_id=1;
    SELECT MAX(salary) FROM employee;
    SELECT MIN(salary) FROM employee;
    SELECT AVG(salary) FROM employee;
    SELECT SUM(salary) FROM employee;
    SELECT SUM(salary) FROM employee WHERE depart_id=3;

示例

# 1.获取每个部门的最高薪资  
# 先按post分组,再在每个小组中,统计salary的最大值
select post,max(salary) from emp group by post;
select post as '部门',max(salary) as '最高薪资' from emp group by post;
select post '部门',max(salary) '最高薪资' from emp group by post;
# as可以给字段起别名 也可以直接省略不写 但是不推荐 因为省略的话语意不明确 容易错乱

# 2.获取每个部门的最低薪资
select post,min(salary) from emp group by post;
# 3.获取每个部门的平均薪资
select post,avg(salary) from emp group by post;
# 4.获取每个部门的工资总和
select post,sum(salary) from emp group by post;

# 5.获取每个部门的人数
# count 是计数,这里放的字段只要能对应到每一个人,都能统计
select post,count(id) from emp group by post;      # 统计id常用 符合逻辑
select post,count(salary) from emp group by post;
select post,count(age) from emp group by post;
select post,count(post_comment) from emp group by post;  null不行

# 6.查询分组之后的部门名称和每个部门下所有的员工姓名 
# group_concat 不单单可以支持你获取分组之后的其他字段值 还支持拼接操作
select post,group_concat(name) from emp group by post;
select post,group_concat(name,'_DSB') from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;
# concat不分组的时候用 
select concat('NAME:',name),concat('SAL:',salary) from emp;
# 如果多个字段的连接符号相同的情况下,可以直接使用concat_ws来完成拼接
select conca_ws(";", name, age, sex) from emp;


# 补充 as语法不单单可以给字段起别名 还可以给表临时起别名
# 查询结果本来也是虚拟表,括号起来as t1,临时起别名为t1
(select post, count(id) from emp group by post) as t1;

# 查询每个人的年薪,字段可以直接做数学运算
select name,salary*12 from emp;

三、分组注意事项

# 关键字where和group by同时出现的时候group by必须在where的后面
where先对整体数据进行过滤之后再分组操作
where筛选条件不能使用聚合函数
select id,name,age from emp where max(salary) > 3000;  #报错

select max(salary) from emp;  #不分组 默认整体就是一组

# 统计各部门年龄在30岁以上的员工平均薪资
# 1 先求所有年龄大于30岁的员工
select * from emp where age>30;
# 2 再对结果进行分组
select * from emp where age>30 group by post;
# 3 最后用要求的字段替代*
select post,avg(salary) from emp where age>30 group by post;

# 如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
# 多条记录之间的某个字段值相同,该字段通常用来作为分组的依据

 5.1.5  HAVING过滤

HAVING与WHERE不一样的地方在于!!!!!!

# 执行优先级从高到低:where > group by > having 
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。

#2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数。
"""
having的语法根where是一致的
只不过having是在分组之后进行的过滤操作
即having是可以直接使用聚合函数的
"""

# 统计各部门年龄在30岁以上的员工平均工资,并且保留平均薪资大于10000的部门
select post,avg(salary) from emp 
    where age>30 
    group by post
    having avg(salary) > 10000
    ;

 5.1.6 distinct去重

"""
一定要注意 必须是完全一样的数据才可以去重!!!
一定不要将逐渐忽视了 有逐渐存在的情况下 是不可能去重的

[
{'id':1,'name':'jason','age':18},
{'id':2,'name':'jason','age':18},
{'id':3,'name':'egon','age':18}
]

"""
select distinct id,age from emp;  #id字段是不同的,不能去重
select distinct age from emp;     #可以去重

#因为表的主键肯定不同,查询时有主键存在的情况下是不可能去重的,要去重在取数据时不要拿主键

5.1.7 查询排序 ORDER BY

按单列排序
    SELECT * FROM employee ORDER BY salary;
    SELECT * FROM employee ORDER BY salary ASC;
    SELECT * FROM employee ORDER BY salary DESC;
# order by默认是升序,asc可以省略不写,也可以修改为降序desc


# 按多列排序:先按照age升序,如果年纪相同,则按照薪资排序
SELECT * from employee
    ORDER BY age,
    salary DESC;
    
# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均薪资大于1000的部门,然后对平均工资降序排序
select post,avg(salary) from emp 
    where age>10 
    group by post
    having avg(salary) > 1000
    order by avg(salary) desc
        ;

5.1.8 限制查询的记录数 LIMIT

作用:针对数据过多的情况,我们通常都是做分页处理,减缓数据量过大造成程序卡死的现象,limit就是一个分页处理的效果

示例:
SELECT * FROM employee ORDER BY salary DESC LIMIT 3;     #默认初始位置为0 ,只拿3条(只展示3条)
    
SELECT * FROM employee ORDER BY salary DESC LIMIT 0,5;   #从第0开始,只展示第一条到第五条数据

SELECT * FROM employee ORDER BY salary DESC LIMIT 5,5;   #从第6条数据开始,往后取5条数据展示

# limit后跟两个参数,第一个参数代表起始索引位置,第二个参数代表展示条数

5.1.9 使用正则表达式查询

MySQL支持正则,用regexp 代表正则

SELECT * FROM employee WHERE name REGEXP '^ale';

SELECT * FROM employee WHERE name REGEXP 'on$';

SELECT * FROM employee WHERE name REGEXP 'm{2}';

select * from emp where name regexp "^j.*(n|y)$";  # 以j开头,n或y结尾,中间无所谓


小结:对字符串匹配的方式
WHERE name = 'egon';
WHERE name LIKE 'yua%';
WHERE name REGEXP 'on$';

5.2 多表查询 

5.2.1 多表连接查询

目的:当初为了解耦合,把一张表拆分成两张,为了方便存储,但查询时我们希望合为一张表,查起来方便。

#重点:外链接语法

SELECT 字段列表 FROM 表1 INNER|LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;

一、 交叉连接:不适用任何匹配条件。生成笛卡尔积

mysql> select * from emp,dep;
+----+------------+--------+------+--------+------+--------------+
| id | name       | sex    | age  | dep_id | id   | name         |
+----+------------+--------+------+--------+------+--------------+
|  1 | egon       | male   |   18 |    200 |  200 | 技术         |
|  1 | egon       | male   |   18 |    200 |  201 | 人力资源     |
|  1 | egon       | male   |   18 |    200 |  202 | 销售         |
|  1 | egon       | male   |   18 |    200 |  203 | 运营         |
|  2 | alex       | female |   48 |    201 |  200 | 技术         |
|  2 | alex       | female |   48 |    201 |  201 | 人力资源     |
|  2 | alex       | female |   48 |    201 |  202 | 销售         |
|  2 | alex       | female |   48 |    201 |  203 | 运营         |
|  3 | wupeiqi    | male   |   38 |    201 |  200 | 技术         |
|  3 | wupeiqi    | male   |   38 |    201 |  201 | 人力资源     |
|  3 | wupeiqi    | male   |   38 |    201 |  202 | 销售         |
|  3 | wupeiqi    | male   |   38 |    201 |  203 | 运营         |
|  4 | yuanhao    | female |   28 |    202 |  200 | 技术         |
|  4 | yuanhao    | female |   28 |    202 |  201 | 人力资源     |
|  4 | yuanhao    | female |   28 |    202 |  202 | 销售         |
|  4 | yuanhao    | female |   28 |    202 |  203 | 运营         |
|  5 | liwenzhou  | male   |   18 |    200 |  200 | 技术         |
|  5 | liwenzhou  | male   |   18 |    200 |  201 | 人力资源     |
|  5 | liwenzhou  | male   |   18 |    200 |  202 | 销售         |
|  5 | liwenzhou  | male   |   18 |    200 |  203 | 运营         |
|  6 | jingliyang | female |   18 |    204 |  200 | 技术         |
|  6 | jingliyang | female |   18 |    204 |  201 | 人力资源     |
|  6 | jingliyang | female |   18 |    204 |  202 | 销售         |
|  6 | jingliyang | female |   18 |    204 |  203 | 运营         |
+----+------------+--------+------+--------+------+--------------+

二、内连接:只连接匹配的行

#找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
#dep没有204这个部门,因而emp表中关于204这条员工信息没有匹配出来
mysql> select emp.id,emp.name,emp.age,emp.sex,dep.name from emp inner join dep on emp.dep_id=dep.id; 
+----+-----------+------+--------+--------------+
| id | name      | age  | sex    | name         |
+----+-----------+------+--------+--------------+
|  1 | egon      |   18 | male   | 技术         |
|  2 | alex      |   48 | female | 人力资源     |
|  3 | wupeiqi   |   38 | male   | 人力资源     |
|  4 | yuanhao   |   28 | female | 销售         |
|  5 | liwenzhou |   18 | male   | 技术         |
+----+-----------+------+--------+--------------+

#上述sql等同于
mysql> select emp.id,emp.name,emp.age,emp.sex,dep.name from emp,dep where emp.dep_id=dep.id;

三、左连接:优先显示左表全部记录

#以左表为准,即找出所有员工信息,当然包括没有部门的员工
#本质就是:在内连接的基础上增加左边有右边没有的结果,没有对应的项就用NULL
mysql> select emp.id,emp.name,dep.name as depart_name from emp left join dep on emp.dep_id=dep.id;
+----+------------+--------------+
| id | name       | depart_name  |
+----+------------+--------------+
|  1 | egon       | 技术         |
|  5 | liwenzhou  | 技术         |
|  2 | alex       | 人力资源     |
|  3 | wupeiqi    | 人力资源     |
|  4 | yuanhao    | 销售         |
|  6 | jingliyang | NULL         |
+----+------------+--------------+

四、右连接:优先显示右表全部记录

#以右表为准,即找出所有部门信息,包括没有员工的部门
#本质就是:在内连接的基础上增加右边有左边没有的结果,没有对应的项就用NULL
mysql> select emp.id,emp.name,dep.name as depart_name from emp right join dep on emp.dep_id=dep.id;
+------+-----------+--------------+
| id   | name      | depart_name  |
+------+-----------+--------------+
|    1 | egon      | 技术         |
|    2 | alex      | 人力资源     |
|    3 | wupeiqi   | 人力资源     |
|    4 | yuanhao   | 销售         |
|    5 | liwenzhou | 技术         |
| NULL | NULL      | 运营         |
+------+-----------+--------------+

五、全连接:显示左右两个表全部记录 

全连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果
#注意:mysql不支持全连接 full JOIN
#强调:mysql可以使用此种方式间接实现全连接
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id
;
#查看结果
+------+------------+--------+------+--------+------+--------------+
| id   | name       | sex    | age  | dep_id | id   | name         |
+------+------------+--------+------+--------+------+--------------+
|    1 | egon       | male   |   18 |    200 |  200 | 技术         |
|    5 | liwenzhou  | male   |   18 |    200 |  200 | 技术         |
|    2 | alex       | female |   48 |    201 |  201 | 人力资源     |
|    3 | wupeiqi    | male   |   38 |    201 |  201 | 人力资源     |
|    4 | yuanhao    | female |   28 |    202 |  202 | 销售         |
|    6 | jingliyang | female |   18 |    204 | NULL | NULL         |
| NULL | NULL       | NULL   | NULL |   NULL |  203 | 运营         |
+------+------------+--------+------+--------+------+--------------+

#注意 union与union all的区别:union会去掉相同的纪录

六、符合条件连接查询

#示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门
select employee.name,department.name from employee inner join department
    on employee.dep_id = department.id
    where age > 25;

#示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显示
select employee.id,employee.name,employee.age,department.name from employee,department
    where employee.dep_id = department.id
    and age > 25
    order by age asc;

 5.2.2 子查询

#1:子查询是将一个查询语句嵌套在另一个查询语句中。就是我们平时解决问题的思路:分步骤解决问题,第一步-->第二步....
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。将一个查询语句的结果当做另外一个查询语句的条件去用
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<等

一、带IN关键字的子查询

#查询平均年龄在25岁以上的部门名
select id,name from department
    where id in 
        (select dep_id from employee group by dep_id having avg(age) > 25);

#查看技术部员工姓名
select name from employee
    where dep_id in 
        (select id from department where name='技术');

#查看不足1人的部门名(子查询得到的是有人的部门id,相当于部门id与员工表外键dep_id没有关联,这个部门下没有员工)
select name from department where id not in (select distinct dep_id from employee);
#not in 无法处理null的值,即子查询中如果存在null的值,not in将无法处理,解决方法如下:
select * from department where id not in (select distinct dep_id from employee where dep_id is not null);

二、带ANY关键字的子查询 

#在 SQL 中 ANY 和 SOME 是同义词,SOME 的用法和功能和 ANY 一模一样。

# ANY 和 IN 运算符不同之处1
ANY 必须和其他的比较运算符共同使用,而且ANY必须将比较运算符放在 ANY 关键字之前,所比较的值需要匹配子查询中的任意一个值,这也就是 ANY 在英文中所表示的意义

例如:使用 IN 和使用 ANY运算符得到的结果是一致的
select * from employee where salary = any (
select max(salary) from employee group by depart_id);

select * from employee where salary in (
select max(salary) from employee group by depart_id);

结论:也就是说“=ANY”等价于 IN 运算符,而“<>ANY”则等价于 NOT IN 运算符

# ANY和 IN 运算符不同之处2
ANY 运算符不能与固定的集合相匹配,比如下面的 SQL 语句是错误的
SELECT * FROM T_Book WHERE FYearPublished < ANY (2001, 2003, 2005) 

三、带ALL关键字的子查询

# all同any类似,只不过all表示的是所有,any表示任一
# 查询出那些薪资比所有部门的平均薪资都高的员工=》薪资在所有部门平均线以上的狗币资本家
select * from employee where salary > all (
select avg(salary) from employee group by depart_id);

# 查询出那些薪资比所有部门的平均薪资都低的员工=》薪资在所有部门平均线以下的无产阶级劳苦大众
select * from employee where salary < all (
select avg(salary) from employee group by depart_id);

# 查询出那些薪资比任意一个部门的平均薪资低的员工=》薪资在任一部门平均线以下的员工
select * from employee where salary < any (
select avg(salary) from employee group by depart_id); 

四、带比较运算符的子查询

#比较运算符:=、!=、>、>=、<、<=、<>
#查询大于所有人平均年龄的员工名与年龄
mysql> select name,age from emp where age > (select avg(age) from emp);
+---------+------+
| name | age |
+---------+------+
| alex | 48 |
| wupeiqi | 38 |
+---------+------+
2 rows in set (0.00 sec)

#查询大于部门内平均年龄的员工名、年龄
select t1.name,t1.age from emp t1
inner join 
(select dep_id,avg(age) avg_age from emp group by dep_id) t2
on t1.dep_id = t2.dep_id
where t1.age > t2.avg_age; 

五、带EXISTS关键字的子查询

EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。而是返回一个布尔值,True或False。
当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询。

#department表中存在dept_id=203,Ture
mysql> select * from employee
    ->     where exists
    ->         (select id from department where id=200);
+----+------------+--------+------+--------+
| id | name       | sex    | age  | dep_id |
+----+------------+--------+------+--------+
|  1 | egon       | male   |   18 |    200 |
|  2 | alex       | female |   48 |    201 |
|  3 | wupeiqi    | male   |   38 |    201 |
|  4 | yuanhao    | female |   28 |    202 |
|  5 | liwenzhou  | male   |   18 |    200 |
|  6 | jingliyang | female |   18 |    204 |
+----+------------+--------+------+--------+

#department表中存在dept_id=205,False
mysql> select * from employee
    ->     where exists
    ->         (select id from department where id=204);
Empty set (0.00 sec)

in与exists

!!!!!!当in和exists在查询效率上比较时,in查询的效率快于exists的查询效率!!!!!!

==============================exists==============================
# exists
exists后面一般都是子查询,后面的子查询被称做相关子查询(即与主语句相关),当子查询返回行数时,exists条件返回true,
否则返回false,exists是不返回列表的值的,exists只在乎括号里的数据能不能查找出来,是否存在这样的记录。

# 例
查询出那些班级里有学生的班级
select * from class where exists (select * from stu where stu.cid=class.id)

# exists的执行原理为:
1、依次执行外部查询:即select * from class 
2、然后为外部查询返回的每一行分别执行一次子查询:即(select * from stu where stu.cid=class.cid)
3、子查询如果返回行,则exists条件成立,条件成立则输出外部查询取出的那条记录

==============================in==============================
# in
in后跟的都是子查询,in()后面的子查询 是返回结果集的

# 例
查询和所有女生年龄相同的男生
select * from stu where sex='男' and age in(select age from stu where sex='女')

# in的执行原理为:
in()的执行次序和exists()不一样,in()的子查询会先产生结果集,
然后主查询再去结果集里去找符合要求的字段列表去.符合要求的输出,反之则不输出.

not in与 not exists

!!!!!!not exists查询的效率远远高与not in查询的效率。!!!!!!

==============================not in==============================
not in()子查询的执行顺序是:
为了证明not in成立,即找不到,需要一条一条地查询表,符合要求才返回子查询的结果集,不符合的就继续查询下一条记录,直到把表中的记录查询完,只能查询全部记录才能证明,并没有用到索引。
                
==============================not exists==============================
not exists:
如果主查询表中记录少,子查询表中记录多,并有索引。
例如:查询那些班级中没有学生的班级
select * from class

where not exists

(select * from student where student.cid = class.cid)

not exists的执行顺序是:
在表中查询,是根据索引查询的,如果存在就返回true,如果不存在就返回false,不会每条记录都去查询。 

 

6 权限管理

#授权表
user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段
db   #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段
tables_priv  #该表放行的权限。针对:某一张表,以及该表下的所有字段
columns_priv #该表放行的权限,针对:某一个字段

#按图解释:
user:放行db1,db2及其包含的所有
db:放行db1,及其db1包含的所有
tables_priv:放行db1.table1,及其该表包含的所有
columns_prive:放行db1.table1.column1,只放行该字段

权限相关操作

#创建用户
create user 'egon'@'1.1.1.1' identified by '123';
create user 'egon'@'192.168.1.%' identified by '123';
create user 'egon'@'%' identified by '123';


#授权:对文件夹,对文件,对文件某一字段的权限
查看帮助:help grant
常用权限有:select,update,alter,delete
all可以代表除了grant之外的所有权限

#针对所有库的授权:*.*
grant select on *.* to 'egon1'@'localhost' identified by '123'; #只在user表中可以查到egon1用户的select权限被设置为Y

#针对某一数据库:db1.*
grant select on db1.* to 'egon2'@'%' identified by '123'; #只在db表中可以查到egon2用户的select权限被设置为Y

#针对某一个表:db1.t1
grant select on db1.t1 to 'egon3'@'%' identified by '123';  #只在tables_priv表中可以查到egon3用户的select权限

#针对某一个字段:
mysql> select * from t3;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | egon1 |   18 |
|    2 | egon2 |   19 |
|    3 | egon3 |   29 |
+------+-------+------+

grant select (id,name),update (age) on db1.t3 to 'egon4'@'localhost' identified by '123'; 
#可以在tables_priv和columns_priv中看到相应的权限
mysql> select * from tables_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
    Grantor: root@localhost
  Timestamp: 0000-00-00 00:00:00
 Table_priv:
Column_priv: Select,Update
row in set (0.00 sec)

mysql> select * from columns_priv where user='egon4'\G
*************************** 1. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: id
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 2. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: name
  Timestamp: 0000-00-00 00:00:00
Column_priv: Select
*************************** 3. row ***************************
       Host: localhost
         Db: db1
       User: egon4
 Table_name: t3
Column_name: age
  Timestamp: 0000-00-00 00:00:00
Column_priv: Update
rows in set (0.00 sec)

#删除权限
revoke select on db1.* from 'egon'@'%';ji

 

posted @ 2022-12-14 00:57  不会钓鱼的猫  阅读(17)  评论(0编辑  收藏  举报