CRUD : Create(創(chuàng)建), Retrieve(讀取),Update(更新),Delete(刪除)
一、插入操作----insert
創(chuàng)建user表并設(shè)置四個字段
mysql> create table user(
-> id int unsigned primary key auto_increment,
-> sn int unsigned unique key,
-> name varchar(20) not null,
-> tel varchar(11) unique key
-> );
1.1 簡單插入
--指定列插入--
mysql> insert into user (id,sn,name) values(1,200,'劉邦');
Query OK, 1 row affected (0.01 sec)
--全列插入--
mysql> insert into user values(2,201,'劉倍','1111111');
Query OK, 1 row affected (0.01 sec)
--全列多行插入--
mysql> insert into user values(3,202,'劉某','000010'),(4,203,'王某','220');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
--指定列多行插入--
mysql> insert into user (sn,name,tel) values(300,'劉某','999'),(303,'王某','888');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
注意:insert into可以省略成 insert
1.2 插入時是否更新----ON DUPLICATE KEY UPDATE
由于主鍵或者唯一鍵對應(yīng)的值已經(jīng)存在而導致插入失敗,可以選擇性的進行同步更新操作
mysql> insert into user values(2,201,'劉倍','1110111');
ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
mysql> insert into user values(11,201,'劉倍','1111111');
ERROR 1062 (23000): Duplicate entry '201' for key 'sn'
mysql>
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
insert into user values(2,201,'劉倍','1110111')on duplicate key update sn=20,name='劉備',tel='2222';
Query OK, 2 rows affected (0.01 sec)
-- 0 row affected: 表中有沖突數(shù)據(jù),但沖突數(shù)據(jù)的值和 update 的值相等
-- 1 row affected: 表中沒有沖突數(shù)據(jù),數(shù)據(jù)被插入
-- 2 row affected: 表中有沖突數(shù)據(jù),并且數(shù)據(jù)已經(jīng)被更新
注意:更新的數(shù)據(jù)也不能和其他數(shù)據(jù)沖突
1.3 插入時替換----REPLACE
-- 主鍵 或者 唯一鍵 沒有沖突,則直接插入;
-- 主鍵 或者 唯一鍵 如果沖突,則刪除后再插入
mysql> replace into user(sn,name,tel)values(666,'xiaohaizi','1111111');
Query OK, 2 rows affected (0.00 sec)
-- 1 row affected: 表中沒有沖突數(shù)據(jù),數(shù)據(jù)被插入
-- 2 row affected: 表中有沖突數(shù)據(jù),刪除后重新插入
二、查詢----select
創(chuàng)建數(shù)據(jù)表并添加數(shù)據(jù)
mysql> CREATE TABLE exam_result (
-> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20) NOT NULL COMMENT '同學姓名',
-> chinese float DEFAULT 0.0 COMMENT '語文成績',
-> math float DEFAULT 0.0 COMMENT '數(shù)學成績',
-> english float DEFAULT 0.0 COMMENT '英語成績'
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO exam_result (name, chinese, math, english) VALUES
-> ('唐三藏', 67, 98, 56),
-> ('孫悟空', 87, 78, 77),
-> ('豬悟能', 88, 98, 90),
-> ('曹孟德', 82, 84, 67),
-> ('劉玄德', 55, 85, 45),
-> ('孫權(quán)', 70, 73, 78),
-> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
2.1 簡單查詢與去重
--全列查詢
mysql> select * from exam_result;
--指定列查詢
mysql> select name from exam_result;
查詢字段為表達式
-- 表達式不包含字段會在結(jié)果后面拼接上
SELECT id, name, 10 FROM exam_result;
對查詢結(jié)果取別名
SELECT column [AS] alias_name [...] FROM table_name;
如:
select chinese+math+english as total from exam_result;
結(jié)果去重
select distinct math from exam_result;
2.2 基本查詢----where條件
比較運算符:
運算符 | 說明 |
---|---|
>, >=, <, <= | 大于,大于等于,小于,小于等于 |
= | 等于,NULL 不安全,例如 NULL = NULL 的結(jié)果是 NULL |
<=> | 等于,NULL 安全,例如 NULL <=> NULL 的結(jié)果是 TRUE(1) |
!=, <> | 不等于 |
BETWEEN a0 AND a1 | 范圍匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) |
IN (option, …) | 如果是 option 中的任意一個,返回 TRUE(1) |
IS NULL | 是 NULL |
IS NOT NULL | 不是 NULL |
LIKE | 模糊匹配。% 表示任意多個(包括 0 個)任意字符;_ 表示任意一個字符 |
條件運算符:同與或非
運算符 | 說明 |
---|---|
AND | 多個條件必須都為 TRUE(1),結(jié)果才是 TRUE(1) |
OR | 任意一個條件為 TRUE(1), 結(jié)果為 TRUE(1) |
NOT | 條件為 TRUE(1),結(jié)果為 FALSE(0) |
2.2.3 案列演示
- 英語不及格的同學及英語成績 ( < 60 )
select name,english from exam_result where english<60;
- 語文成績在 [80, 90] 分的同學及語文成績
mysql> select name,chinese from exam_result where chinese>=80 and chinese<=90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孫悟空 | 87 |
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
或者
mysql> select name,chinese from exam_result where chinese between 80 and 90;
- 數(shù)學成績是 58 或者 59 或者 98 或者 99 分的同學及數(shù)學成績
select name,math from exam_result where math=58 or math=59 or math=98 or math=99;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+------+
或者:
select name,math from exam_result where math in(58,59,98,99);
- 姓孫的同學 及 孫某同學
mysql> select name from exam_result where name like '孫%';
+-----------+
| name |
+-----------+
| 孫悟空 |
| 孫權(quán) |
+-----------+
2 rows in set (0.00 sec)
mysql> select name from exam_result where name like '孫_';
+--------+
| name |
+--------+
| 孫權(quán) |
+--------+
- 語文成績好于英語成績的同學
select name,chinese,english from exam_result where chinese>english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67 | 56 |
| 孫悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 劉玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+---------+---------+
- 總分在 200 分以下的同學
select name,chinese+math+english as total from exam_result where chinese+math+english<200;
+-----------+-------+
| name | total |
+-----------+-------+
| 劉玄德 | 185 |
| 宋公明 | 170 |
+-----------+-------+
注意:由于執(zhí)行順序原因,寫成以下方式會報錯
select name,chinese+english+math total from exam_result where total<200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
- 語文成績 大于80并且不姓孫的同學
select name,chinese from exam_result where chinese>80 and name not like '孫%';
+-----------+---------+
| name | chinese |
+-----------+---------+
| 豬悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
- 孫某同學,否則要求總成績 > 200 并且 語文成績 < 數(shù)學成績 并且 英語成績 >80
select name,chinese,math,english,chinese+math+english total from exam_result where name like '孫_' or
+-----------+---------+------+---------+-------+
| name | chinese | math | english | total |
+-----------+---------+------+---------+-------+
| 豬悟能 | 88 | 98 | 90 | 276 |
| 孫權(quán) | 70 | 73 | 78 | 221 |
+-----------+---------+------+---------+-------+
- NULL查詢
mysql> select * from exam_result where name is null;
Empty set (0.00 sec)
2.4 排序----order by
-- ASC 為升序(從小到大)默認為升序
-- DESC 為降序(從大到小)
-- 默認為 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
同學姓名及數(shù)學成績,按數(shù)學成績升序顯示
select name,math from exam_result order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 宋公明 | 65 |
| 孫權(quán) | 73 |
| 孫悟空 | 78 |
| 曹孟德 | 84 |
| 劉玄德 | 85 |
| 唐三藏 | 98 |
| 豬悟能 | 98 |
+-----------+------+
查詢同學姓名各門成績,依次按 數(shù)學降序,英語升序,語文升序的方式顯示
mysql> select name,math,english,chinese from exam_result order by math desc,english desc,chinese asc;
+-----------+------+---------+---------+
| name | math | english | chinese |
+-----------+------+---------+---------+
| 豬悟能 | 98 | 90 | 88 |
| 唐三藏 | 98 | 56 | 67 |
| 劉玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孫悟空 | 78 | 77 | 87 |
| 孫權(quán) | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+------+---------+---------+
注意:這里的排序地位從大到小為數(shù)學降序>英語升序>語文升序
查詢姓孫的同學或者姓曹的同學數(shù)學成績,結(jié)果按數(shù)學成績由高到低顯示
mysql> select name,math from exam_result where name like '孫%' or name like '曹%' order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 曹孟德 | 84 |
| 孫悟空 | 78 |
| 孫權(quán) | 73 |
+-----------+------+
執(zhí)行順序
查詢同學及總分,由低到高
排序是在獲得數(shù)據(jù)之后才進行的,因此這里不同于where,可以使用別名
select name,math+chinese+english as total from exam_result order by total;
+-----------+-------+
| name | total |
+-----------+-------+
| 宋公明 | 170 |
| 劉玄德 | 185 |
| 唐三藏 | 221 |
| 孫權(quán) | 221 |
| 曹孟德 | 233 |
| 孫悟空 | 242 |
| 豬悟能 | 276 |
+-----------+-------+
limit篩選分頁結(jié)果
– 數(shù)據(jù)顯示起始下標為 0
– 從 s 開始(下標從0開始),篩選 n 條結(jié)果
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT s, n
–
– 從 0 開始,篩選 n 條結(jié)果
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n;
–
– 從 s 開始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n OFFSET s;
從位置s開始,偏移n個位置
mysql> select * from exam_result limit 3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孫悟空 | 87 | 78 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result limit 1,3;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 2 | 孫悟空 | 87 | 78 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
mysql> select * from exam_result limit 3 offset 0;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孫悟空 | 87 | 78 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
三、修改操作----update
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
將孫悟空同學的數(shù)學成績變更為 80 分
mysql> update exam_result set math=80 where name='孫悟空';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select name,math from exam_result;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 孫悟空 | 80 |
| 豬悟能 | 98 |
| 曹孟德 | 84 |
| 劉玄德 | 85 |
| 孫權(quán) | 73 |
| 宋公明 | 65 |
+-----------+------+
將曹孟德同學的數(shù)學成績變更為 60 分,語文成績變更為 70 分
update exam_result set math=60,chinese=70 where name='曹孟德';
mysql> select name,math,chinese from exam_result;
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 唐三藏 | 98 | 67 |
| 孫悟空 | 80 | 87 |
| 豬悟能 | 98 | 88 |
| 曹孟德 | 60 | 70 |
| 劉玄德 | 85 | 55 |
| 孫權(quán) | 73 | 70 |
| 宋公明 | 65 | 75 |
+-----------+------+---------+
將總成績倒數(shù)前三的 3 位同學的數(shù)學成績加上 30 分
注意:這里更新的時候,不能使用limit 0,3
mysql> select name,math+chinese+english total from exam_result order by total limit 0,3;
+-----------+-------+
| name | total |
+-----------+-------+
| 宋公明 | 170 |
| 劉玄德 | 185 |
| 曹孟德 | 197 |
+-----------+-------+
--update exam_result set math=math+30 order by math+chinese+english asc limit 0,3;
--ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3' at line 1
mysql> update exam_result set math=math+30 order by math+chinese+english asc limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select name,math+chinese+english total from exam_result order by total limit 0,3;
+-----------+-------+
| name | total |
+-----------+-------+
| 宋公明 | 200 |
| 劉玄德 | 215 |
| 唐三藏 | 221 |
+-----------+-------+
3 rows in set (0.00 sec)
mysql> select name,math+chinese+english total from exam_result order by total;
+-----------+-------+
| name | total |
+-----------+-------+
| 宋公明 | 200 |
| 劉玄德 | 215 |
| 唐三藏 | 221 |
| 孫權(quán) | 221 |
| 曹孟德 | 227 |
| 孫悟空 | 244 |
| 豬悟能 | 276 |
+-----------+-------+
7 rows in set (0.00 sec)
將所有同學的語文成績更新為原來的 2 倍
mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 2 | 孫悟空 | 174 | 80 | 77 |
| 3 | 豬悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 5 | 劉玄德 | 110 | 115 | 45 |
| 6 | 孫權(quán) | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
四、刪除----delete
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
刪除孫悟空同學的考試成績
mysql> delete from exam_result where name='孫悟空';
Query OK, 1 row affected (0.01 sec)
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 3 | 豬悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 5 | 劉玄德 | 110 | 115 | 45 |
| 6 | 孫權(quán) | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
刪除整張表
mysql> CREATE TABLE for_delete (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
mysql> delete from for_delete;
mysql> select * from for_delete;
mysql> INSERT INTO for_delete (name) VALUES ('D');
mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
注意:刪除整張表刪除的只是數(shù)據(jù),屬于DML語言,刪除表中數(shù)據(jù)之后,AUTO_INCREMENT的值不會被重置,沿著上一次的數(shù)據(jù)接著自增
4.1截斷表----TRUNCATE
- 只能對整表操作,不能像 DELETE 一樣針對部分數(shù)據(jù)操作;
- 實際上 MySQL 不對數(shù)據(jù)操作,所以比 DELETE 更快,但是TRUNCATE在刪除數(shù)據(jù)的時候,并不經(jīng)過真正的事物,所以無法回滾
- 會重置 AUTO_INCREMENT 項
TRUNCATE [TABLE] table_name
測試同上述delete
mysql> CREATE TABLE for_truncate (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
mysql> INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
mysql> TRUNCATE for_truncate;
mysql> show create table for_truncate\G
*************************** 1. row ***************************
Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> INSERT INTO for_truncate (name) VALUES ('D');
mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
mysql> show create table for_truncate\G
*************************** 1. row ***************************
Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
4.2 delete與truncate區(qū)別
刪除操作的區(qū)別:
- DELETE語句用于逐行地刪除表中的數(shù)據(jù)記錄。它是一個事務(wù)操作,可以在執(zhí)行時指定條件,并且只刪除滿足條件的行。DELETE操作可以回滾,因此可以撤消對表數(shù)據(jù)的刪除操作。
TRUNCATE語句用于快速刪除表中的所有數(shù)據(jù)。它是一個DDL(數(shù)據(jù)定義語言)操作,它會刪除表中所有的行,并且不返回被刪除的行。TRUNCATE操作無法回滾,因此一旦執(zhí)行,數(shù)據(jù)將無法恢復(fù)。
效率和性能:
-
TRUNCATE操作通常比DELETE操作更快,特別是在處理大型表時。TRUNCATE不會記錄在日志中的每個刪除操作,而是記錄一次DDL操作,因此它的執(zhí)行速度更快。
DELETE操作在數(shù)據(jù)庫日志中會生成相應(yīng)的日志記錄,并且可能會觸發(fā)相關(guān)的觸發(fā)器和約束檢查,因此相對而言會比TRUNCATE操作慢一些。
空間和索引:
DELETE操作僅刪除表中的數(shù)據(jù)行,但不釋放與這些行相關(guān)的存儲空間。這意味著如果表中存在大量被刪除的行,可能會導致存儲空間的碎片化。
TRUNCATE操作不僅會刪除表中的數(shù)據(jù)行,還會釋放與表相關(guān)的存儲空間,這會導致數(shù)據(jù)文件的大小減小,并且存儲空間被清空。
DELETE操作不會重置表的自增ID值,而TRUNCATE操作將重置表的自增ID值為初始值。文章來源:http://www.zghlxwxcb.cn/news/detail-581273.html
總結(jié)而言,DELETE和TRUNCATE都可以用于刪除數(shù)據(jù),但DELETE更加靈活和可控,可以根據(jù)需要指定刪除條件,并且可以回滾操作。而TRUNCATE更適合用于快速地刪除整個表的數(shù)據(jù),并且執(zhí)行速度更快、更有效,但無法回滾操作。選擇使用哪個操作取決于具體的需求和情況。文章來源地址http://www.zghlxwxcb.cn/news/detail-581273.html
到了這里,關(guān)于【MySQL】基本查詢之表的增刪改查的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!