常用MySQL命令
># 查看所有数据库
mysql> SHOW DATABASES;
># 切换指定数据库
mysql> USE nsd2021;
># 查看当前库中所有的表
mysql> SHOW TABLES;
># 查看表结构
mysql> DESC departments;
># 查看当前所处的数据库
mysql> SELECT DATABASE();
># 查看当前登陆用户
mysql> SELECT USER();
># 查看版本
mysql> SELECT VERSION();
语法规范
- 不区分大小写,但建议关键字大写,表名、列名小写
- 每条命令最好用分号结尾,当然啦,你用\g结尾也可以
- 每条命令根据需要,可以进行缩进或换行(最好是关键字单独占一行),如:
mysql> SELECT
-> name, email
-> FROM
-> employees;
-
注释
-
单行注释
mysql> # select * from departments mysql> -- select * from departments
-
多行注释
mysql> /* /*> SELECT /*> * /*> FROM /*> departments; /*> */
-
SQL语句分类
-
数据查询语言(Data Query Language, DQL)
负责进行数据查询而不会对数据本身进行修改的语句,这是最基本的SQL语句。
-
数据定义语言 (Data Definition Language, DDL)
负责数据结构定义与数据库对象定义的语言,由CREATE、ALTER与DROP三个语法所组成
-
数据操纵语言(Data Manipulation Language, DML)
负责对数据库对象运行数据访问工作的指令集,以INSERT、UPDATE、DELETE三种指令为核心,分别代表插入、更新与删除。
-
数据控制语言 (Data Control Language)
它可以控制特定用户账户对数据表、查看表、预存程序、用户自定义函数等数据库对象的控制权。由 GRANT 和 REVOKE 两个指令组成。
数据查询语言DQL
基础查询
SELECT 查询的字段列表 FROM 表;
- 查询的字段列表可以是字段、常量、表达式、函数等
# 查单个字段
mysql> select dept_name from departments;
# 查多个字段
mysql> select name, email from employees;
# 查所有字段
mysql> select * from departments;
# 使用表达式
mysql> select date, employee_id, basic+bonus from salary;
# 查询常量
mysql> select 100;
# 查询表达式
mysql> select 10+5;
# 查询函数
mysql> select version();
# 查询函数,统计salary共有多少行记录
mysql> select count(*) from salary;
使用别名,字段名和别名之间可以用空格或关键字AS
mysql> select dept_id 部门编号, dept_name AS 部门名 from departments;
去重
mysql> select dept_id from employees;
mysql> select distinct dept_id from employees;
使用concat函数进行字符串拼接
mysql> select concat(name, '-', phone_number) from employees;
条件查询
SELECT 查询的字段列表 FROM 表 WHERE 条件;
条件运算符,与python类似,使用
-
>
: 大于 -
<
: 小于 -
=
: 等于 -
>=
: 大于等于 -
<=
: 小于等于 -
!=
: 不等于
mysql> select * from departments where dept_id>3;
mysql> select * from departments where dept_id<3;
mysql> select * from departments where dept_id=3;
mysql> select * from departments where dept_id!=3;
mysql> select * from departments where dept_id>=3;
mysql> select * from departments where dept_id<=3;
逻辑运算符,and(&&)、or(||)、not(!)
mysql> select * from departments where dept_id>1 and dept_id<5;
mysql> select * from departments where dept_id<3 or dept_id>6;
mysql> select * from departments where not dept_id<=6;
模糊查询
- like: 包含
- between xxx and yyy: 在xxx和yyy之间的
- in:在列表中的
- is null:为空,相当于python的None
- is not null:非空
# %匹配0到多个任意字符
mysql> select name, email from employees where name like '张%';
# _匹配一个字符
mysql> select name, email from employees where name like '张_';
mysql> select name, email from employees where name like '张__';
mysql> select * from departments where dept_id between 3 and 5;
mysql> select * from departments where dept_id in (1, 3, 5, 8);
# 匹配部门名为空的记录
mysql> select * from departments where dept_name is null;
# 查询部门名不为空的记录
mysql> select * from departments where dept_name is not null;
排序
SELECT 查询的字段列表 FROM 表 ORDER BY 排序列表 [asc|desc];
排序:默认升序
mysql> select name, birth_date from employees where birth_date>'19980101';
# 默认升序排列
mysql> select name, birth_date from employees where birth_date>'19980101' order by birth_date;
# 降序排列
mysql> select name, birth_date from employees where birth_date>'19980101' order by birth_date desc;
# 查询2015年1月10号员工工资情况
mysql> select date, employee_id, basic, bonus from salary where date='20150110';
# 查询2015年1月10号员工工资情况,以基本工资进行降序排列;如果基本工资相同,再以奖金升序排列
mysql> select date, employee_id, basic, bonus from salary where date='20150110' order by basic desc, bonus;
# 查询2015年1月10号员工工资情况,以工资总额为排序条件
mysql> select date, employee_id, basic, bonus, basic+bonus as total from salary where date='20150110' order by total;