• MySQL的结构化语言 DDL DML DQL DCL


    一、SQL结构化语言介绍

    数据查询语言DQL:其语句称为“数据检索语言”,用以从库中获取数据,确定数据怎样在应用程序给出,保留select是dql(也是所有sql)用的最多的动词

    数据操作语言DML:其语句包括动词insert,update和delete,分别用于添加,修改和删除表中的行,也称为动词查询语言

    数据控制语言DCL:他的语句通过grant或者revoke获得许可,确定单个用户和用户组对数据库对象的访问,某些rdbms可用grant或者revoke控制对表单个列的访问。

    数据定义语言DDL:其语句包括动词create和drop,在数据库创建新表或者删除表,为表加入索引等,DDL包括许多人数据库目录中获得数据有关的保留字,他也是动作查询的一部分。

    二、数据定义语言DDL

    1.系统数据库的介绍

    information_schema        虚拟库,主要存储了系统中的一些数据库对象的信息,例如用户                                             表信息、列信息、权限信息、字符信息等
    performance_schema      主要存储数据库服务器的性能参数
    mysql                                 授权库,主要存储系统用户的权限信息
    sys                                     主要存储数据库服务器的性能参数(把 performance_schema的                                             复杂度降低)

    2.创建数据库:DDL

    (1)命令行创建数据库

    语法;mysql -u用户 -p密码 -e “create database 数据库名 default charset ‘utf8’ ”

    [root@localhost ~]# mysql -uroot -p123 -e "create database db2 default charset 'utf8'"

    (2)在mysql里面创建数据库

    语法:create database 数据库名  指定字符集 ;

    1. mysql> create database db3 default charset 'utf8';
    2. Query OK, 1 row affected (0.00 sec)

    数据库命名规则:

    区分大小写

    唯一性

    不能使用关键字 create select

    不能单独使用数字

    (3)查看所有数据库

    语法:show databases;

    1. mysql> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | information_schema |
    6. | db1 |
    7. | db2 |
    8. | db3 |
    9. | mysql |
    10. | performance_schema |
    11. | school |
    12. | sys |
    13. +--------------------+
    14. 8 rows in set (0.00 sec)

    (4)查看创建库的信息

      语法:show create database 库名;

    1. mysql> show create database db2;
    2. +----------+--------------------------------------------------------------+
    3. | Database | Create Database |
    4. +----------+--------------------------------------------------------------+
    5. | db2 | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
    6. +----------+--------------------------------------------------------------+
    7. 1 row in set (0.00 sec)

    (5)查看当前库(路径)

    语法:select database();

    1. mysql> select database();
    2. +------------+
    3. | database() |
    4. +------------+
    5. | db2 |
    6. +------------+
    7. 1 row in set (0.00 sec)

    (6)切换数据库

    语法:use 数据库名;

    1. mysql> use db2
    2. Database changed
    3. mysql> show tables;
    4. Empty set (0.00 sec)

    (7)删除数据库

    语法:drop database 库名;

    1. mysql> drop database db2;
    2. Query OK, 0 rows affected (0.01 sec)

    3.表的DDL操作

    (1)在命令行创建表

    语法:mysql -u用户 -p密码 -e “use 库名;create table 表名 (字段  数据类型 约束条件)”

    1. [root@localhost ~]# mysql -uroot -p123 -e"use db3;create table t1(name varchar(30),age int)"
    2. mysql: [Warning] Using a password on the command line interface can be insecure.

    (2)在数据库中创建表

    语法:create table 表名(字段名称 数据类型 约束条件)

    1. mysql> create table t2(
    2. -> id int primary key auto_increment not null,
    3. -> age int,
    4. -> name varchar(40)
    5. -> );
    6. Query OK, 0 rows affected (0.00 sec)

    (3)查看表结构

    语法:desc 表名;

    1. mysql> desc t2;
    2. +-------+-------------+------+-----+---------+----------------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-------+-------------+------+-----+---------+----------------+
    5. | id | int(11) | NO | PRI | NULL | auto_increment |
    6. | age | int(11) | YES | | NULL | |
    7. | name | varchar(40) | YES | | NULL | |
    8. +-------+-------------+------+-----+---------+----------------+
    9. 3 rows in set (0.00 sec)

    (4)显示数据的详细信息

    语法:show create 表名; 

               show table status like '表名';

    1. mysql> show create table t2;
    2. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    3. | Table | Create Table |
    4. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    5. | t2 | CREATE TABLE `t2` (
    6. `id` int(11) NOT NULL AUTO_INCREMENT,
    7. `age` int(11) DEFAULT NULL,
    8. `name` varchar(40) DEFAULT NULL,
    9. PRIMARY KEY (`id`)
    10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    11. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    12. 1 row in set (0.00 sec)
    1. mysql> show table status like 't2' \G
    2. *************************** 1. row ***************************
    3. Name: t2
    4. Engine: InnoDB
    5. Version: 10
    6. Row_format: Dynamic
    7. Rows: 0
    8. Avg_row_length: 0
    9. Data_length: 16384
    10. Max_data_length: 0
    11. Index_length: 0
    12. Data_free: 0
    13. Auto_increment: 1
    14. Create_time: 2023-09-28 08:33:56
    15. Update_time: NULL
    16. Check_time: NULL
    17. Collation: utf8_general_ci
    18. Checksum: NULL
    19. Create_options:
    20. Comment:
    21. 1 row in set (0.00 sec)

    (5)添加字段

    语法:alter table 表名 add 字段名称 字段类型 约束条件

    1. mysql> alter table t2 add math int not null;
    2. #新字段 数据类型 约束条件
    3. Query OK, 0 rows affected (0.02 sec)
    4. Records: 0 Duplicates: 0 Warnings: 0

    (6)修改数据类型

    语法:alter table  表名 modify  字段名 新的数据类型 新的约束条件;

    1. mysql> alter table t2 modify math varchar(20) null; #将数据类型改为varchar,约束条件改变
    2. Query OK, 0 rows affected (0.03 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0

    (7)修改字段名(列)和数据类型

    语法:alter table 表名 change 旧字段名 新字段名 新的数据类型 约束条件;

    1. mysql> alter table t2 change math chinese int null; #将math改名为Chinese,int ,null
    2. Query OK, 0 rows affected (0.03 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0

    (8)删除字段(列)

    语法:alter table 表名  drop 字段名

    1. mysql> alter table t2 drop chinese;
    2. Query OK, 0 rows affected (0.03 sec)
    3. Records: 0 Duplicates: 0 Warnings: 0

    (9)删除表

    语法:drop table 表名;

    1. mysql> drop table t2;
    2. Query OK, 0 rows affected (0.00 sec)

    扩展:约束

    表完整性约束
    作用:用于保证数据的完整性和一致性

    ==============================================================
    约束条件                                                     说明
    NOT NULL                         标识该字段不能为null
    UNIQUE KEY  (UK)           标识该字段的值是唯一的,可以为空,一个表中可以有多个UNIQUE KEY
    AUTO_INCREMENT         标识该字段的值自动增长(整数类型,而且为主键)
    DEFAULT                          为该字段设置默认值
    UNSIGNED                       无符号(正数)
    ZEROFILL                         使用0填充,例如0000001

    说明:
    1. 是否允许为空,默认NULL;可设置NOT NULL,字段不允许为空,必须赋值
    2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
        sex enum('male','female') not null default 'male'
        age int unsigned NOT NULL default 20         必须为正值(无符号) 不允许为空  默认是20

    三、数据操作语言DML

    1.插入数据insert

    语法:insert into  表名(字段1...字段n)values(值1....值n)

    1. mysql> insert into employee values (1,'qiancheng','man','20180314','hr','talk',7000,501,102);
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> insert into employee(id,name,hire_date,post,salary) values (50,'zxvb','20190423','hr',8000);
    4. Query OK, 1 row affected (0.00 sec)
    5. mysql> insert into employee(id,name,salary) values(51,'ab',6500), (52,'cd',7600), (53,'ef',8900);
    6. Query OK, 3 rows affected (0.01 sec)
    7. Records: 3 Duplicates: 0 Warnings: 0

    2.更新数据update

    语法:update 表名 set 字段1=值1,字段2=值2 where 条件;

    1. mysql> update employee6 set emp_name='ab';
    2. Query OK, 6 rows affected (0.00 sec)
    3. Rows matched: 6 Changed: 6 Warnings: 0
    4. mysql> select * from employee6;
    5. +--------+----------+------+---------+
    6. | emp_id | emp_name | age | dept_id |
    7. +--------+----------+------+---------+
    8. | 1 | ab | 19 | 200 |
    9. | 2 | ab | 26 | 201 |
    10. | 3 | ab | 30 | 201 |
    11. | 4 | ab | 24 | 202 |
    12. | 5 | ab | 40 | 200 |
    13. | 6 | ab | 28 | 204 |
    14. +--------+----------+------+---------+
    15. 6 rows in set (0.00 sec)
    16. mysql> update employee6 set emp_name='cd' where emp_id=3;
    17. Query OK, 1 row affected (0.00 sec)
    18. Rows matched: 1 Changed: 1 Warnings: 0
    19. mysql> select * from employee6;
    20. +--------+----------+------+---------+
    21. | emp_id | emp_name | age | dept_id |
    22. +--------+----------+------+---------+
    23. | 1 | ab | 19 | 200 |
    24. | 2 | ab | 26 | 201 |
    25. | 3 | cd | 30 | 201 |
    26. | 4 | ab | 24 | 202 |
    27. | 5 | ab | 40 | 200 |
    28. | 6 | ab | 28 | 204 |
    29. +--------+----------+------+---------+
    30. 6 rows in set (0.00 sec)

    3.删除数据delete

    语法:delete from 表名 where 条件;(删除一行)

               delete from 表名;(删除表中所有数据,硬盘数据还在)

               truncate 表名;(效率高,时间快,硬盘数据不在)

    1. mysql> select * from employee6;
    2. +--------+----------+------+---------+
    3. | emp_id | emp_name | age | dept_id |
    4. +--------+----------+------+---------+
    5. | 1 | ab | 19 | 200 |
    6. | 2 | ab | 26 | 201 |
    7. | 4 | ab | 24 | 202 |
    8. | 5 | ab | 40 | 200 |
    9. | 6 | ab | 28 | 204 |
    10. +--------+----------+------+---------+
    11. 5 rows in set (0.00 sec)
    12. mysql> delete from employee6;
    13. Query OK, 5 rows affected (0.01 sec)
    14. mysql> select * from employee6;
    15. Empty set (0.00 sec)
    16. mysql> desc employee6;
    17. +----------+-------------+------+-----+---------+----------------+
    18. | Field | Type | Null | Key | Default | Extra |
    19. +----------+-------------+------+-----+---------+----------------+
    20. | emp_id | int(11) | NO | PRI | NULL | auto_increment |
    21. | emp_name | varchar(50) | YES | | NULL | |
    22. | age | int(11) | YES | | NULL | |
    23. | dept_id | int(11) | YES | | NULL | |
    24. +----------+-------------+------+-----+---------+----------------+
    25. 4 rows in set (0.00 sec)

    四、数据查询语言DQL

    1.单表查询

    (1).基础查询

    a.查询所有信息

    语法 :select * from  表名;

    1. mysql> select * from employee;
    2. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    3. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    4. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    5. | 20 | tom | man | 2017-09-15 | instructor | teach | 8000.00 | 501 | 100 |
    6. | 21 | alince | woman | 2013-04-28 | instructor | teach | 5500.00 | 501 | 100 |
    7. | 22 | robin | man | 2020-09-18 | instructor | teach | 7200.00 | 501 | 100 |
    8. | 23 | zhuzhu | man | 2016-12-09 | hr | hrcc | 6000.00 | 502 | 101 |
    9. | 24 | gougou | woman | 2015-04-27 | hr | NULL | 6000.00 | 502 | 101 |
    10. | 30 | maomao | man | 2019-08-12 | sale | talk | 20000.00 | 503 | 102 |
    11. | 31 | yiyi | man | 2015-06-17 | talk | NULL | 8000.00 | NULL | NULL |
    12. | 40 | harry | woman | 2018-02-05 | hr | hrcc | 6900.00 | 502 | 102 |
    13. | 41 | tianyuan | man | 2018-02-05 | null | salecc | 9700.00 | 501 | 102 |
    14. | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
    15. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    16. 10 rows in set (0.00 sec)
    b.去除重复记录

    语法:select distinct 字段 from 表名;

    1. mysql> select distinct office from employee;
    2. +--------+
    3. | office |
    4. +--------+
    5. | 501 |
    6. | 502 |
    7. | 503 |
    8. | NULL |
    9. +--------+
    10. 4 rows in set (0.00 sec)

    (2).条件查询

    语法: select 字段 from 表名 where 条件列表;

    1. #单条件查询
    2. mysql> select name,salary from employee where salary = 8000
    3. ;
    4. +------+---------+
    5. | name | salary |
    6. +------+---------+
    7. | tom | 8000.00 |
    8. | yiyi | 8000.00 |
    9. +------+---------+
    10. 2 rows in set (0.00 sec)
    11. #多条件查询
    12. mysql> select name,salary from employee where salary between 4000 and 20000;
    13. +----------+----------+
    14. | name | salary |
    15. +----------+----------+
    16. | tom | 8000.00 |
    17. | alince | 5500.00 |
    18. | robin | 7200.00 |
    19. | zhuzhu | 6000.00 |
    20. | gougou | 6000.00 |
    21. | maomao | 20000.00 |
    22. | yiyi | 8000.00 |
    23. | harry | 6900.00 |
    24. | tianyuan | 9700.00 |
    25. | xiaoyi | 5700.00 |
    26. +----------+----------+
    27. 10 rows in set (0.00 sec)
    28. mysql> select name,salary from employee where salary=4000 or salary=6000;
    29. +--------+---------+
    30. | name | salary |
    31. +--------+---------+
    32. | zhuzhu | 6000.00 |
    33. | gougou | 6000.00 |
    34. +--------+---------+
    35. 2 rows in set (0.00 sec)

    关键字is 

    1. #查询employee中office为空的人
    2. mysql> select name,office from employee where office is null;
    3. +------+--------+
    4. | name | office |
    5. +------+--------+
    6. | yiyi | NULL |
    7. +------+--------+
    8. 1 row in set (0.00 sec)
    9. #查询employee表中office不为空的人
    10. mysql> select name,office from employee where office is not
    11. null;
    12. +----------+--------+
    13. | name | office |
    14. +----------+--------+
    15. | tom | 501 |
    16. | alince | 501 |
    17. | robin | 501 |
    18. | zhuzhu | 502 |
    19. | gougou | 502 |
    20. | maomao | 503 |
    21. | harry | 502 |
    22. | tianyuan | 501 |
    23. | xiaoyi | 501 |
    24. +----------+--------+
    25. 9 rows in set (0.00 sec)

    关键字in

    1. #查询employee表中工资为4000,800020000的员工
    2. mysql> select name,salary from employee where salary in (4000, 8000,20000);
    3. +--------+----------+
    4. | name | salary |
    5. +--------+----------+
    6. | tom | 8000.00 |
    7. | maomao | 20000.00 |
    8. | yiyi | 8000.00 |
    9. +--------+----------+
    10. 3 rows in set (0.00 sec)
    11. mysql> select name,salary from employee where salary in (4000,8000,20000);
    12. +--------+----------+
    13. | name | salary |
    14. +--------+----------+
    15. | tom | 8000.00 |
    16. | maomao | 20000.00 |
    17. | yiyi | 8000.00 |
    18. +--------+----------+
    19. 3 rows in set (0.01 sec)

    (3).排序查询

    语法:select 字段 from 表名 order by 排序字段1,排序字段2;

    ASC:升序排列(默认)

    DESC:降序排列

    1. #按名字升序排列
    2. mysql> select name from employee order by name;
    3. +----------+
    4. | name |
    5. +----------+
    6. | alince |
    7. | gougou |
    8. | harry |
    9. | maomao |
    10. | robin |
    11. | tianyuan |
    12. | tom |
    13. | xiaoyi |
    14. | yiyi |
    15. | zhuzhu |
    16. +----------+
    17. 10 rows in set (0.00 sec)
    18. #按名字倒叙排列
    19. mysql> select name from employee order by name desc;
    20. +----------+
    21. | name |
    22. +----------+
    23. | zhuzhu |
    24. | yiyi |
    25. | xiaoyi |
    26. | tom |
    27. | tianyuan |
    28. | robin |
    29. | maomao |
    30. | harry |
    31. | gougou |
    32. | alince |
    33. +----------+
    34. 10 rows in set (0.00 sec)
    35. 从第四行开始显示5
    36. mysql> select name from employee order by name desc limit 3, 5;
    37. +----------+
    38. | name |
    39. +----------+
    40. | tom |
    41. | tianyuan |
    42. | robin |
    43. | maomao |
    44. | harry |
    45. +----------+
    46. 5 rows in set (0.00 sec)
    1. #按多列排序,先按入职时间,再按工资
    2. mysql> select * from employee order by hire_date,salary asc;
    3. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    4. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    5. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    6. | 21 | alince | woman | 2013-04-28 | instructor | teach | 5500.00 | 501 | 100 |
    7. | 24 | gougou | woman | 2015-04-27 | hr | NULL | 6000.00 | 502 | 101 |
    8. | 31 | yiyi | man | 2015-06-17 | talk | NULL | 8000.00 | NULL | NULL |
    9. | 23 | zhuzhu | man | 2016-12-09 | hr | hrcc | 6000.00 | 502 | 101 |
    10. | 20 | tom | man | 2017-09-15 | instructor | teach | 8000.00 | 501 | 100 |
    11. | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
    12. | 40 | harry | woman | 2018-02-05 | hr | hrcc | 6900.00 | 502 | 102 |
    13. | 41 | tianyuan | man | 2018-02-05 | null | salecc | 9700.00 | 501 | 102 |
    14. | 30 | maomao | man | 2019-08-12 | sale | talk | 20000.00 | 503 | 102 |
    15. | 22 | robin | man | 2020-09-18 | instructor | teach | 7200.00 | 501 | 100 |
    16. +----+----------+-------+------------+------------+-----------------+----------+--------+--------+
    17. 10 rows in set (0.00 sec)

    (4)分组查询

    语法:select 字段,(group_concat(字段名)) from 表名 group by 字段名;

    GROUP_CONCAT 函数用于将一个分组内的多行数据合并成一个字符串,并以指定的分隔符进行分隔。

    1. mysql> select post from employee group by post;
    2. +------------+
    3. | post |
    4. +------------+
    5. | hr |
    6. | instructor |
    7. | null |
    8. | sale |
    9. | talk |
    10. +------------+
    1. mysql> select post,group_concat(name) from employee group by post;
    2. +------------+---------------------+
    3. | post | group_concat(name) |
    4. +------------+---------------------+
    5. | hr | zhuzhu,gougou,harry |
    6. | instructor | tom,alince,robin |
    7. | null | tianyuan,xiaoyi |
    8. | sale | maomao |
    9. | talk | yiyi |
    10. +------------+---------------------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select post,group_concat(id) from employee group by
    13. post;
    14. +------------+------------------+
    15. | post | group_concat(id) |
    16. +------------+------------------+
    17. | hr | 23,24,40 |
    18. | instructor | 20,21,22 |
    19. | null | 41,42 |
    20. | sale | 30 |
    21. | talk | 31 |
    22. +------------+------------------+
    23. 5 rows in set (0.00 sec)

    (5)模糊查询(通配符%)

    1. mysql> select * from employee where salary like "%20%";
    2. +----+--------+------+------------+------------+-----------------+----------+--------+--------+
    3. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    4. +----+--------+------+------------+------------+-----------------+----------+--------+--------+
    5. | 22 | robin | man | 2020-09-18 | instructor | teach | 7200.00 | 501 | 100 |
    6. | 30 | maomao | man | 2019-08-12 | sale | talk | 20000.00 | 503 | 102 |
    7. +----+--------+------+------------+------------+-----------------+----------+--------+--------+
    8. 2 rows in set (0.00 sec)

    (6)正则查询

    1. mysql> select * from employee where salary regexp '72+';
    2. +----+-------+------+------------+------------+-----------------+---------+--------+--------+
    3. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    4. +----+-------+------+------------+------------+-----------------+---------+--------+--------+
    5. | 22 | robin | man | 2020-09-18 | instructor | teach | 7200.00 | 501 | 100 |
    6. +----+-------+------+------------+------------+-----------------+---------+--------+--------+
    7. 1 row in set (0.00 sec)
    8. mysql> select * from employee where name regexp '^xiao';
    9. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    10. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    11. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    12. | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
    13. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    14. 1 row in set (0.00 sec)
    15. mysql> select * from employee where name regexp "yi$";
    16. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    17. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    18. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    19. | 31 | yiyi | man | 2015-06-17 | talk | NULL | 8000.00 | NULL | NULL |
    20. | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
    21. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    22. 2 rows in set (0.00 sec)
    23. mysql> select * from employee where name regexp "yi$";
    24. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    25. | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
    26. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    27. | 31 | yiyi | man | 2015-06-17 | talk | NULL | 8000.00 | NULL | NULL |
    28. | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
    29. +----+--------+------+------------+------+-----------------+---------+--------+--------+
    30. 2 rows in set (0.00 sec)

    2.多表查询

    a) 内连接

    只连接匹配的行

    1. mysql> select employee6.emp_id,employee6.emp_name,employee6.age,department.dept_name from employee6,department where employee6.dept_id = department.dept_id;
    2. +--------+----------+------+-----------+
    3. | emp_id | emp_name | age | dept_name |
    4. +--------+----------+------+-----------+
    5. | 1 | tianyun | 19 | hr |
    6. | 2 | tom | 26 | it |
    7. | 3 | jack | 30 | it |
    8. | 4 | alice | 24 | yunwei |
    9. | 5 | robin | 40 | hr |
    10. +--------+----------+------+-----------+
    11. 5 rows in set (0.00 sec)
    12. mysql> select a.emp_id,a.emp_name,a.age,b.dept_name from employee6 a,department b where a.dept_id=b.dept_id;
    13. +--------+----------+------+-----------+
    14. | emp_id | emp_name | age | dept_name |
    15. +--------+----------+------+-----------+
    16. | 1 | tianyun | 19 | hr |
    17. | 2 | tom | 26 | it |
    18. | 3 | jack | 30 | it |
    19. | 4 | alice | 24 | yunwei |
    20. | 5 | robin | 40 | hr |
    21. +--------+----------+------+-----------+
    22. 5 rows in set (0.00 sec)
    1. mysql> select a.emp_id,a.emp_name,a.age,b.dept_name from employee6 a inner join department b on a.dept_id=b.dept_id;
    2. +--------+----------+------+-----------+
    3. | emp_id | emp_name | age | dept_name |
    4. +--------+----------+------+-----------+
    5. | 1 | tianyun | 19 | hr |
    6. | 2 | tom | 26 | it |
    7. | 3 | jack | 30 | it |
    8. | 4 | alice | 24 | yunwei |
    9. | 5 | robin | 40 | hr |
    10. +--------+----------+------+-----------+
    11. 5 rows in set (0.00 sec)

    b)外连接

    外连接:(了解)
            左连接:    会显示左边表内所有的值,不论在右边表内匹不匹配
            右连接:    会显示右边表内所有的值,不论在左边表内匹不匹配

    语法:SELECT 字段 ROM 表1 LEFT|RIGHT JOIN 表2   ON 表1.字段 = 表2.字段;

    1. mysql> select emp_id,emp_name,age,dept_name from employee6
    2. left join department on employee6.dept_id = department.dep
    3. t_id;
    4. +--------+----------+------+-----------+
    5. | emp_id | emp_name | age | dept_name |
    6. +--------+----------+------+-----------+
    7. | 1 | tianyun | 19 | hr |
    8. | 5 | robin | 40 | hr |
    9. | 2 | tom | 26 | it |
    10. | 3 | jack | 30 | it |
    11. | 4 | alice | 24 | yunwei |
    12. | 6 | natasha | 28 | NULL |
    13. +--------+----------+------+-----------+
    14. 6 rows in set (0.00 sec)
    15. mysql> select emp_id,emp_name,age,dept_name from employee6
    16. right join department on employee6.dept_id = department.de
    17. pt_id;
    18. +--------+----------+------+-----------+
    19. | emp_id | emp_name | age | dept_name |
    20. +--------+----------+------+-----------+
    21. | 1 | tianyun | 19 | hr |
    22. | 2 | tom | 26 | it |
    23. | 3 | jack | 30 | it |
    24. | 4 | alice | 24 | yunwei |
    25. | 5 | robin | 40 | hr |
    26. | NULL | NULL | NULL | fd |
    27. +--------+----------+------+-----------+
    28. 6 rows in set (0.00 sec)

    c)全外连接

    全外连接:(了解)     包含左、右两个表的全部行

    1. mysql> select * from employee6 full join department;
    2. +--------+----------+------+---------+---------+-----------+
    3. | emp_id | emp_name | age | dept_id | dept_id | dept_name |
    4. +--------+----------+------+---------+---------+-----------+
    5. | 1 | tianyun | 19 | 200 | 200 | hr |
    6. | 1 | tianyun | 19 | 200 | 201 | it |
    7. | 1 | tianyun | 19 | 200 | 202 | yunwei |
    8. | 1 | tianyun | 19 | 200 | 203 | fd |
    9. | 2 | tom | 26 | 201 | 200 | hr |
    10. | 2 | tom | 26 | 201 | 201 | it |
    11. | 2 | tom | 26 | 201 | 202 | yunwei |
    12. | 2 | tom | 26 | 201 | 203 | fd |
    13. | 3 | jack | 30 | 201 | 200 | hr |
    14. | 3 | jack | 30 | 201 | 201 | it |
    15. | 3 | jack | 30 | 201 | 202 | yunwei |
    16. | 3 | jack | 30 | 201 | 203 | fd |
    17. | 4 | alice | 24 | 202 | 200 | hr |
    18. | 4 | alice | 24 | 202 | 201 | it |
    19. | 4 | alice | 24 | 202 | 202 | yunwei |
    20. | 4 | alice | 24 | 202 | 203 | fd |
    21. | 5 | robin | 40 | 200 | 200 | hr |
    22. | 5 | robin | 40 | 200 | 201 | it |
    23. | 5 | robin | 40 | 200 | 202 | yunwei |
    24. | 5 | robin | 40 | 200 | 203 | fd |
    25. | 6 | natasha | 28 | 204 | 200 | hr |
    26. | 6 | natasha | 28 | 204 | 201 | it |
    27. | 6 | natasha | 28 | 204 | 202 | yunwei |
    28. | 6 | natasha | 28 | 204 | 203 | fd |
    29. +--------+----------+------+---------+---------+-----------+
    30. 24 rows in set (0.00 sec)

    五、数据库控制语言DCL

    1.用户管理

    (1)创建用户

    语法:create user '用户名'@'客户端ip地址' identified by '密码';

    1. mysql> create user 'cd'@'10.36.192.%' identified by '123';
    2. Query OK, 0 rows affected (0.00 sec)

    (2)修改用户信息

     语法:rename user '用户名'@'客户端来源ip地址' to '新用户名'@'客户端来源IP地址'

    1. mysql> rename user 'cd'@'10.36.192.%' to 'ef'@'192.168.142.%';
    2. Query OK, 0 rows affected (0.00 sec)

    (3)修改普通用户的密码

    a)语法:set password for '用户名'@'ip地址'=password('新密码');

    b)语法:alter user '用户名'@'客户端来源IP地址'  identified by '新密码';

    c)语法:update mysql.user set authentication_string=password('密码') where user='root' and host='localhost';

    1. mysql> set password for 'ef'@'192.168.142.%'=password('111');
    2. Query OK, 0 rows affected, 1 warning (0.00 sec)
    3. mysql> alter user 'ef'@'192.168.142.%' identified by '123';
    4. Query OK, 0 rows affected (0.00 sec)

    (4)删除用户

    语法:drop user '用户名'@'客户端来源ip地址';

    1. mysql> drop user 'ef'@'192.168.142.%';
    2. Query OK, 0 rows affected (0.00 sec)

    2.权限管理

    (1).给普通用户授权并授权

    语法:grant 权限 on 数据库.表  to '用户'@'客户端来源IP地址' identified by '密码';

    1. mysql> grant all on *.* to 'ef'@'192.168.142.%' identified
    2. by '123';
    3. Query OK, 0 rows affected, 1 warning (0.00 sec)

    (2).取消普通用户的权限

    语法:revoke 权限 on 数据库.表 from '用户'@'客户端来源IP地址

    1. mysql> revoke all on *.* from 'cd'@'192.168.142.%';
    2. Query OK, 0 rows affected (0.00 sec)

    (3).刷新权限

    语法:flush privileges;

    1. mysql> flush privileges;
    2. Query OK, 0 rows affected (0.00 sec)

    (4).查看授权信息

    a.查看授权语句

    语法:show grants for '用户'@'客户端来源IP地址';

    1. mysql> show grants for 'ef'@'192.168.142.%';
    2. +-----------------------------------------------------+
    3. | Grants for ef@192.168.142.% |
    4. +-----------------------------------------------------+
    5. | GRANT ALL PRIVILEGES ON *.* TO 'ef'@'192.168.142.%' |
    6. +-----------------------------------------------------+
    7. 1 row in set (0.00 sec)
    b.查看生效的授权信息

    语法:select * from mysql.user where user='用户'\G

    1. mysql> select * from mysql.user where user='ef'\G
    2. *************************** 1. row ***************************
    3. Host: 192.168.142.%
    4. User: ef
    5. Select_priv: Y
    6. Insert_priv: Y
    7. Update_priv: Y
    8. Delete_priv: Y
    9. Create_priv: Y
    10. Drop_priv: Y
    11. Reload_priv: Y
    12. Shutdown_priv: Y
    13. Process_priv: Y
    14. File_priv: Y
    15. Grant_priv: N
    16. References_priv: Y
    17. Index_priv: Y
    18. Alter_priv: Y
    19. Show_db_priv: Y
    20. Super_priv: Y
    21. Create_tmp_table_priv: Y
    22. Lock_tables_priv: Y
    23. Execute_priv: Y
    24. Repl_slave_priv: Y
    25. Repl_client_priv: Y
    26. Create_view_priv: Y
    27. Show_view_priv: Y
    28. Create_routine_priv: Y
    29. Alter_routine_priv: Y
    30. Create_user_priv: Y
    31. Event_priv: Y
    32. Trigger_priv: Y
    33. Create_tablespace_priv: Y
    34. ssl_type:
    35. ssl_cipher:
    36. x509_issuer:
    37. x509_subject:
    38. max_questions: 0
    39. max_updates: 0
    40. max_connections: 0
    41. max_user_connections: 0
    42. plugin: mysql_native_password
    43. authentication_string: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257
    44. password_expired: N
    45. password_last_changed: 2023-09-28 15:06:11
    46. password_lifetime: NULL
    47. account_locked: N
    48. 1 row in set (0.00 sec)

  • 相关阅读:
    C语言练习题——分支和循环
    Spring Cloud Alibaba-02-Nacos Discovery服务治理及负载均衡
    颜值在线的 Keychron K8 实际上手体验
    Hystrix 服务熔断
    中国爱眼教育大会|2023山东眼健康产业展会|标展5500元
    ::before 和 :after中双冒号和单冒号有什么区别?解释一下这2个伪元素的作用
    PTA 7-6 数据类型判断(c++)
    开源网安解决方案荣获四川数实融合创新实践优秀案例
    kubernetesr进阶--Secret概述
    Java学习笔记(三)
  • 原文地址:https://blog.csdn.net/2301_78315274/article/details/133440944