DDL:數據定義語言 註意:對錶操作的時候需要進入到對應的資料庫裡面去。 創建表:CREATE TABLE [IF NOT EXISTS] 'tbl_name' ( 欄位1 修飾符, col2 欄位2 修飾符, ...) 例如:創建一張名為stu1的表,表中包含的欄位有id,name和age,id的 ...
DDL:數據定義語言
註意:對錶操作的時候需要進入到對應的資料庫裡面去。
創建表:CREATE TABLE [IF NOT EXISTS] 'tbl_name' ( 欄位1 修飾符, col2 欄位2 修飾符, ...)
例如:創建一張名為stu1的表,表中包含的欄位有id,name和age,id的數據類型是int,且是主鍵並且自動增長。
mysql> create table stu1 (id int primary key auto_increment,name varchar(20) not null, age tinyint unsigned);
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| stu1 |
+-------------------+
1 row in set (0.00 sec)
查看表
-
查看表列表
-
查看創建表的命令
-
查看表結構(欄位)信息
-
查看表屬性信息
查看表:show tables [from db_name]
註意:不加db_name,預設查看的是當前資料庫裡面的所有表。
mysql> show tables from student;
+-------------------+
| Tables_in_student |
+-------------------+
| stu1 |
+-------------------+
1 row in set (0.00 sec)
查看創建表的命令:SHOW CREATE TABLE tbl_name
mysql> show create table stu1 \G
*************************** 1. row ***************************
Table: stu1
Create Table: CREATE TABLE `stu1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
查看表的結構(欄位)信息:
-
desc tb_name
-
SHOW COLUMNS FROM [db_name.]tb_name
mysql> desc stu1;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
查看表的屬性信息:show table status like 'tb_name'
註意:mysq客戶端的ego--(\G)命令可以垂直顯示結果
*************************** 1. row ***************************
Name: stu1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 6
Avg_row_length: 2730
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 10
Create_time: 2022-09-09 00:56:44
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
修改表:
#修改表名
ALTER TABLE students RENAME s1;
#添加欄位
ALTER TABLE s1 ADD phone varchar(11) AFTER name;
#修改欄位類型
ALTER TABLE s1 MODIFY phone int;
#修改欄位名稱和類型
ALTER TABLE s1 CHANGE COLUMN phone mobile char(11);
#刪除欄位
ALTER TABLE s1 DROP COLUMN mobile;
#修改字元集
ALTER TABLE s1 character set utf8;
#修改數據類型和字元集
ALTER TABLE s1 change name name varchar(20) character set utf8;
#添加欄位
ALTER TABLE students ADD gender ENUM('m','f');
alter table student modify is_del bool default false;
#修改欄位名和類型
ALETR TABLE students CHANGE id sid int UNSIGNED NOT NULL PRIMARY KEY;
#刪除欄位
ALTER TABLE students DROP age;
#查看表結構
DESC students;
#新建表無主鍵,添加和刪除主鍵
CREATE TABLE t1 SELECT * FROM students;
ALTER TABLE t1 add primary key (stuid);
ALTER TABLE t1 drop primary key ;
#添加外鍵
ALTER TABLE students add foreign key(TeacherID) references teachers(tid);
#刪除外鍵
SHOW CREATE TABLE students
#查看外鍵名
ALTER TABLE students drop foreign key <外鍵名>;
DML:數據操縱語言(insert、updete、delete)
註意:設計字元類型的數據類型,進行操作的時候要加上引號(單雙都可以)
INSERT 語句:insert tb_name(col1...coln) values (value1...valuen)
例如:
mysql> insert stu1(name,age) values('tom',10);
Query OK, 1 row affected (0.01 sec)
全值插入:不指定col
mysql> insert stu1 values(3,'BOB',20);
Query OK, 1 row affected (0.00 sec)
UPDATE 語句
註意:使用update語句的時候需要指定限制條件,不然將修改所有行的指定欄位
mysql> update stu1 set name='bob' where name='BOB';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from stu1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 3 | bob | 20 |
+----+------+------+
3 rows in set (0.00 sec)
可以通過在配置文件指定選項來避免這個錯誤。
[root@centos8 ~]#vim /etc/my.cnf
[mysql]
safe-updates
刪除指定的記錄:
ysql> delete from stu1 where id=3 ;
Query OK, 1 row affected (1.68 sec)
mysql> select * from stu1 ;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
+----+------+------+
2 rows in set (0.00 sec)
刪除數據: delete from tb_name where 限制條件
註意:不加限制條件會清空表裡面的所有數據。
mysql> delete from stu1 where id=5;
Query OK, 1 row affected (0.00 sec)
mysql> select * from stu1;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
+----+--------+------+
6 rows in set (0.00 sec)
清空數據表,保留表結構的方法;
-
delete from tb_name(不會縮減數據文件的大小)
-
TRUNCATE TABLE tbl_name(會自動縮減數據文件的大小)
-
縮減表的大小:OPTIMIZE TABLE tb_name
DQL:數據查詢語言(select)
select查詢
-
單表操作
-
多表操作
針對單表操作:
簡單查詢:select 需要查詢得欄位 from tb_name where 限制條件
- 指定欄位別名
範例:欄位顯示的時候使用別名
mysql> select id as '編號',name as '名字',age as '年齡' from stu1;
+--------+--------+--------+
| 編號 | 名字 | 年齡 |
+--------+--------+--------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 3 | bob | 20 |
+--------+--------+--------+
3 rows in set (0.00 sec)
- select可以實現加減乘除運算
mysql> select 1+2+3*4-5+9;
+-------------+
| 1+2+3*4-5+9 |
+-------------+
| 19 |
+-------------+
1 row in set (0.00 sec)
- select可以實現比較的操作(大於、小於、等於等)
mysql> select 1>99;
+------+
| 1>99 |
+------+
| 0 |
+------+
1 row in set (0.00 sec)
mysql> select 1<99;
+------+
| 1<99 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
- BETWEEN:查詢一個範圍: BETWEEN min_num AND max_num
mysql> select * from stu1 where age between 20 and 25;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
+----+--------+------+
4 rows in set (0.00 sec)
- IN:實現不連續的查詢: IN (element1, element2, ...)
mysql> select * from stu1 where age in(10,20,25);
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 6 | bob5=3 | 25 |
+----+--------+------+
3 rows in set (0.00 sec)
- 空查詢: IS NULL, IS NOT NULL
mysql> select * from stu1 where age is null;
+----+------+------+
| id | name | age |
+----+------+------+
| 8 | tom2 | NULL |
+----+------+------+
1 row in set (0.01 sec)
mysql> select * from stu1 where age is NOT null;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
+----+--------+------+
6 rows in set (0.00 sec)
- DISTINCT: 去除重覆行
mysql> select * from stu1;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 9 | bob | 20 |
+----+--------+------+
8 rows in set (0.00 sec)
mysql> select distinct * from stu1;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 9 | bob | 20 |
+----+--------+------+
8 rows in set (0.00 sec)
- like: 模糊查詢: LIKE 使用 % 表示任意長度的任意字元 _ 表示任意單個字元
#like 後面的字元需要用引號括起來,可以是單引號,也可以是雙引號
mysql> SELECT * from stu1 where age like '1%';
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 10 |
+----+------+------+
1 row in set (0.00 sec)
- 邏輯操作符:NOT,AND,OR,XOR
group by:根據指定的條件把查詢結果進行"分組"以用於做"聚合"運算
group by通常結合聚合函數來使用。常用聚合函數: count(), sum(), max(), min(), avg(),註意:聚合函數不對null統計
註意:
-
一旦對錶進行分組以後,select後面的欄位要麼是聚合函數要麼就是分組的欄位。
-
group by(分組後)的後面加條件必須用having
-
gtoup by(分組前)的前面加條件可以用where
例如:按照姓名來進行分組,統計每個姓名都有多少人。
mysql> select * from stu1;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 9 | bob | 20 |
+----+--------+------+
8 rows in set (0.00 sec)
mysql> select name , count(*) from stu1 group by name;
+--------+----------+
| name | count(*) |
+--------+----------+
| bob | 2 |
| bob2 | 1 |
| bob2=3 | 1 |
| bob5 | 1 |
| bob5=3 | 1 |
| tom | 1 |
| tom2 | 1 |
+--------+----------+
ORDER BY: 根據指定的欄位對查詢結果進行排序
-
升序:ASC
-
降序:DESC
mysql> select * from stu1 order by age desc;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 7 | bob5 | 26 |
| 6 | bob5=3 | 25 |
| 5 | bob2=3 | 24 |
| 4 | bob2 | 22 |
| 2 | bob | 20 |
| 9 | bob | 20 |
| 1 | tom | 10 |
| 8 | tom2 | NULL |
+----+--------+------+
8 rows in set (0.00 sec)
mysql> select * from stu1 order by age asc;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 8 | tom2 | NULL |
| 1 | tom | 10 |
| 2 | bob | 20 |
| 9 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
+----+--------+------+
8 rows in set (0.00 sec)
LIMIT [[offset,]row_count]:對查詢的結果進行輸出行數數量限制,跳過offset,顯示row_count行,offset默為值為0
例如:limit 3,5表示的就是跳過前三個,只顯示五條記錄。實現分頁顯示。
mysql> select * from stu1;
+----+---------+------+
| id | name | age |
+----+---------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 10 | liyi | 25 |
| 11 | lier | 26 |
| 12 | zhangwu | 22 |
| 13 | xiaosi | 30 |
| 14 | wuad | 40 |
+----+---------+------+
11 rows in set (0.00 sec)
mysql> select * from stu1 limit 3,5;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 10 | liyi | 25 |
| 11 | lier | 26 |
+----+--------+------+
5 rows in set (0.00 sec)
例如:顯示年齡最小的五個(會自動去掉重覆的)
mysql> select * from stu1 order by age;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 8 | tom2 | NULL |
| 1 | tom | 10 |
| 2 | bob | 20 |
| 9 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
+----+--------+------+
8 rows in set (0.00 sec)
mysql> select * from stu1 order by age limit 5;
+----+------+------+
| id | name | age |
+----+------+------+
| 8 | tom2 | NULL |
| 1 | tom | 10 |
| 2 | bob | 20 |
| 9 | bob | 20 |
| 4 | bob2 | 22 |
+----+------+------+
5 rows in set (0.00 sec)
多表查詢:查詢的結果來自於多張表。
多表查詢的方法:
-
子查詢:在SQL語句嵌套著查詢語句,性能較差,基於某語句的查詢結果再次進行的查詢
-
聯合查詢:UNION ,兩張表縱向合併形成一個大表
-
交叉連接:笛卡爾乘積 CROSS JOIN ,橫向連接,把第一個表的每條記錄都和第二張表進行組合,從而形成一個大表
-
內連接:取兩張表得交集(都符合條件得那一部分)
-
外連接:outer inner
左外連接:左邊表的全部內容+交集部分,FROM tb1 LEFT JOIN tb2 ON tb1.col=tb2.col
右外連接:右邊表的全部內容+交集部分,FROM tb1 RIGHT JOIN tb2 ON tb1.col=tb2.col
子查詢:一個查詢結果作為另一個查詢的條件。
例如:
mysql> select * from stu1 where age >(select avg(age) from stu1);
+----+--------+------+
| id | name | age |
+----+--------+------+
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
+----+--------+------+
4 rows in set (0.00 sec)
union:聯合查詢 ,將兩張表縱向合併,合成一個新的大表
前提:
-
欄位(列)需要保持一致。
-
數據類型要匹配
mysql> select * from stu1 union select * from teach;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 10 |
| 2 | bob | 20 |
| 4 | bob2 | 22 |
| 5 | bob2=3 | 24 |
| 6 | bob5=3 | 25 |
| 7 | bob5 | 26 |
| 8 | tom2 | NULL |
| 1 | zhang san | 40 |
| 2 | li si | 45 |
| 3 | wang wu | 46 |
+----+-----------+------+
10 rows in set (0.00 sec)
交叉連接: cross join (橫向笛卡爾積)
-
橫向合併:把第一個表的每條記錄都和第二張表進行組合,從而形成一個大表。(笛卡爾乘積)
-
最終生成的記錄數:A表的記錄數*B表的記錄數
註意:交叉連接慎用,同意造成資料庫死機
mysql> select * from stu1
-> cross join
-> teach;
+----+--------+------+-----+-----------+------+
| id | name | age | TID | NAME | age |
+----+--------+------+-----+-----------+------+
| 1 | tom | 10 | 1 | zhang san | 40 |
| 1 | tom | 10 | 2 | li si | 45 |
| 1 | tom | 10 | 3 | wang wu | 46 |
| 2 | bob | 20 | 1 | zhang san | 40 |
| 2 | bob | 20 | 2 | li si | 45 |
| 2 | bob | 20 | 3 | wang wu | 46 |
| 4 | bob2 | 22 | 1 | zhang san | 40 |
| 4 | bob2 | 22 | 2 | li si | 45 |
| 4 | bob2 | 22 | 3 | wang wu | 46 |
| 5 | bob2=3 | 24 | 1 | zhang san | 40 |
| 5 | bob2=3 | 24 | 2 | li si | 45 |
| 5 | bob2=3 | 24 | 3 | wang wu | 46 |
| 6 | bob5=3 | 25 | 1 | zhang san | 40 |
| 6 | bob5=3 | 25 | 2 | li si | 45 |
| 6 | bob5=3 | 25 | 3 | wang wu | 46 |
| 7 | bob5 | 26 | 1 | zhang san | 40 |
| 7 | bob5 | 26 | 2 | li si | 45 |
| 7 | bob5 | 26 | 3 | wang wu | 46 |
| 8 | tom2 | NULL | 1 | zhang san | 40 |
| 8 | tom2 | NULL | 2 | li si | 45 |
| 8 | tom2 | NULL | 3 | wang wu | 46 |
+----+--------+------+-----+-----------+------+
21 rows in set (0.00 sec)
內連接:inner join 取兩張表橫向合併交集(兩張表都符合條件的部分)
註意:內連接的條件要使用on來進行連接。
mysql> select * from stu1 inner join teach on stu1.id=teach.TID;
+----+------+------+-----+-----------+------+
| id | name | age | TID | NAME | age |
+----+------+------+-----+-----------+------+
| 1 | tom | 10 | 1 | zhang san | 40 |
| 2 | bob | 20 | 2 | li si | 45 |
+----+------+------+-----+-----------+------+
2 rows in set (0.00 sec)
#挑選對應想要的欄位
mysql> select stu1.id,stu1.name,teach.name from stu1 inner join teach on stu1.id=teach.TID;
+----+------+-----------+
| id | name | name |
+----+------+-----------+
| 1 | tom | zhang san |
| 2 | bob | li si |
+----+------+-----------+
2 rows in set (0.00 sec)
對錶起別名:直接在表名後面加別名
mysql> select s.id,s.name,t.name from stu1 s inner join teach t on s.id=t.TID;
+----+------+-----------+
| id | name | name |
+----+------+-----------+
| 1 | tom | zhang san |
| 2 | bob | li si |
+----+------+-----------+
2 rows in set (0.00 sec)
外連接:outer join
-
左外連接:left join
-
右外連接:right join
左外連接:left join 左邊表的全部內容+交集部分
mysql> select stu1.id,stu1.name,teach.name from stu1 left join teach on stu1.id=teach.TID;
+----+--------+-----------+
| id | name | name |
+----+--------+-----------+
| 1 | tom | zhang san |
| 2 | bob | li si |
| 4 | bob2 | NULL |
| 5 | bob2=3 | NULL |
| 6 | bob5=3 | NULL |
| 7 | bob5 | NULL |
| 8 | tom2 | NULL |
+----+--------+-----------+
7 rows in set (0.00 sec)
右外連接: right join 右邊表的全部內容+交集部分
mysql> select stu1.id,stu1.name,teach.name from stu1 right join teach on stu1.id=teach.TID;
+------+------+-----------+
| id | name | name |
+------+------+-----------+
| 1 | tom | zhang san |
| 2 | bob | li si |
| NULL | NULL | wang wu |
+------+------+-----------+
3 rows in set (0.00 sec)