常用MySQL命令
># 查看所有數(shù)據(jù)庫
mysql> SHOW DATABASES;
># 切換指定數(shù)據(jù)庫
mysql> USE nsd2021;
># 查看當(dāng)前庫中所有的表
mysql> SHOW TABLES;
># 查看表結(jié)構(gòu)
mysql> DESC departments;
># 查看當(dāng)前所處的數(shù)據(jù)庫
mysql> SELECT DATABASE();
># 查看當(dāng)前登陸用戶
mysql> SELECT USER();
># 查看版本
mysql> SELECT VERSION();
語法規(guī)范
- 不區(qū)分大小寫竞阐,但建議關(guān)鍵字大寫,表名暑劝、列名小寫
- 每條命令最好用分號結(jié)尾骆莹,當(dāng)然啦,你用\g結(jié)尾也可以
- 每條命令根據(jù)需要铃岔,可以進(jìn)行縮進(jìn)或換行(最好是關(guān)鍵字單獨(dú)占一行)汪疮,如:
mysql> SELECT
-> name, email
-> FROM
-> employees;
-
注釋
-
單行注釋
mysql> # select * from departments mysql> -- select * from departments
-
多行注釋
mysql> /* /*> SELECT /*> * /*> FROM /*> departments; /*> */
-
SQL語句分類
-
數(shù)據(jù)查詢語言(Data Query Language, DQL)
負(fù)責(zé)進(jìn)行數(shù)據(jù)查詢而不會(huì)對數(shù)據(jù)本身進(jìn)行修改的語句,這是最基本的SQL語句毁习。
-
數(shù)據(jù)定義語言 (Data Definition Language, DDL)
負(fù)責(zé)數(shù)據(jù)結(jié)構(gòu)定義與數(shù)據(jù)庫對象定義的語言智嚷,由CREATE、ALTER與DROP三個(gè)語法所組成
-
數(shù)據(jù)操縱語言(Data Manipulation Language, DML)
負(fù)責(zé)對數(shù)據(jù)庫對象運(yùn)行數(shù)據(jù)訪問工作的指令集纺且,以INSERT盏道、UPDATE、DELETE三種指令為核心载碌,分別代表插入猜嘱、更新與刪除。
-
數(shù)據(jù)控制語言 (Data Control Language)
它可以控制特定用戶賬戶對數(shù)據(jù)表嫁艇、查看表朗伶、預(yù)存程序、用戶自定義函數(shù)等數(shù)據(jù)庫對象的控制權(quán)步咪。由 GRANT 和 REVOKE 兩個(gè)指令組成论皆。
數(shù)據(jù)查詢語言DQL
基礎(chǔ)查詢
SELECT 查詢的字段列表 FROM 表;
- 查詢的字段列表可以是字段、常量猾漫、表達(dá)式点晴、函數(shù)等
# 查單個(gè)字段
mysql> select dept_name from departments;
# 查多個(gè)字段
mysql> select name, email from employees;
# 查所有字段
mysql> select * from departments;
# 使用表達(dá)式
mysql> select date, employee_id, basic+bonus from salary;
# 查詢常量
mysql> select 100;
# 查詢表達(dá)式
mysql> select 10+5;
# 查詢函數(shù)
mysql> select version();
# 查詢函數(shù),統(tǒng)計(jì)salary共有多少行記錄
mysql> select count(*) from salary;
使用別名悯周,字段名和別名之間可以用空格或關(guān)鍵字AS
mysql> select dept_id 部門編號, dept_name AS 部門名 from departments;
去重
mysql> select dept_id from employees;
mysql> select distinct dept_id from employees;
使用concat函數(shù)進(jìn)行字符串拼接
mysql> select concat(name, '-', phone_number) from employees;
條件查詢
SELECT 查詢的字段列表 FROM 表 WHERE 條件;
條件運(yùn)算符粒督,與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;
邏輯運(yùn)算符禽翼,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:為空闰挡,相當(dāng)于python的None
- is not null:非空
# %匹配0到多個(gè)任意字符
mysql> select name, email from employees where name like '張%';
# _匹配一個(gè)字符
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];
排序:默認(rèn)升序
mysql> select name, birth_date from employees where birth_date>'19980101';
# 默認(rèn)升序排列
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號員工工資情況锐墙,以基本工資進(jìn)行降序排列;如果基本工資相同解总,再以獎(jiǎng)金升序排列
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;