MySQL表的增刪改查(1)

CRUD
增加(Create)、查詢(Retrieve)、更新(Update)、刪除(Delete)
新增(Create)

-- 創建學生表
DROP TABLE IF EXISTS student;
CREATE TABLE student (
 	id INT,
	sn INT comment '學號',
   	name VARCHAR(20) comment '姓名',
   	qq_mail VARCHAR(20) comment 'QQ郵箱'
);

-- 插入兩條記錄,value_list 數量必須和定義表的列的數量及順序一致
INSERT INTO student VALUES (100, 10000, '唐三藏', NULL);
INSERT INTO student VALUES (101, 10001, '孫悟空', '11111');

-- 插入兩條記錄,value_list 數量必須和指定列數量及順序一致
INSERT INTO student (id, sn, name) VALUES
 (102, 20001, '曹孟德'),
 (103, 20002, '孫仲謀');

** 查詢(Retrieve)**

-- 語法
SELECT
 	[DISTINCT] {* | {column [, column] ...} 
 	[FROM table_name]
 	[WHERE ...]
 	[ORDER BY column [ASC | DESC], ...]
 	LIMIT ...
 
-- 創建考試成績表
DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
 	id INT,
 	name VARCHAR(20),
 	chinese DECIMAL(3,1),
 	math DECIMAL(3,1),
 	english DECIMAL(3,1)
);
-- 插入測試數據
INSERT INTO exam_result (id,name, chinese, math, english) VALUES
 	(1,'唐三藏', 67, 98, 56),
 	(2,'孫悟空', 87.5, 78, 77),
 	(3,'豬悟能', 88, 98.5, 90),
 	(4,'曹孟德', 82, 84, 67),
	 (5,'劉玄德', 55.5, 85, 45),
 	(6,'孫權', 70, 73, 78.5),
 	(7,'宋公',75,65,30);

-- 全列查詢
SELECT * FORM exam_result;

-- 指定列查詢
SELECT id,name,english FROM exam_result;

-- 查詢字段爲表達式
-- 表達式不包含字段
SELECT id, name, 10 FROM exam_result;
-- 表達式包含一個字段
SELECT id, name, english + 10 FROM exam_result;
-- 表達式包含多個字段
SELECT id, name, chinese + math + english FROM exam_result;

-- 結果集中,表頭的列名=別名
SELECT id, name, chinese + math + english 總分 FROM exam_result;

-- 去重 DISTINCT
SELECT DISTINCT math FROM exam_result;

-- 排序 ORDER BY
-- 查詢同學姓名和 qq_mail,按 qq_mail 排序顯示
SELECT name, qq_mail FROM student ORDER BY qq_mail;
SELECT name, qq_mail FROM student  ORDER BY qq_mail DESC;

-- 條件查詢 WHERE
-- 查詢英語不及格的同學及英語成績 ( < 60 )
SELECT name, english FROM exam_result WHERE english < 60;
-- 查詢語文成績好於英語成績的同學
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
-- 查詢總分在 200 分以下的同學
SELECT name, chinese + math + english 總分 FROM exam_result 
 WHERE chinese + math + english < 200;
-- 查詢語文成績大於80分,且英語成績大於80分的同學
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
-- 查詢語文成績大於80分,或英語成績大於80分的同學
SELECT * FROM exam_result WHERE chinese > 80 or english > 80;
-- 觀察AND 和 OR 的優先級:
SELECT * FROM exam_result WHERE chinese > 80 or math>70 and english > 70;
SELECT * FROM exam_result WHERE (chinese > 80 or math>70) and english > 70;

-- 範圍查詢
-- 查詢語文成績在 [80, 90] 分的同學及語文成績
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;
-- 使用 AND 也可以實現
SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese <= 90;
-- 查詢數學成績是 58 或者 59 或者 98 或者 99 分的同學及數學成績
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);
-- 使用 OR 也可以實現
SELECT name, math FROM exam_result WHERE math = 58 OR math = 59 OR math = 98 OR math = 99;

-- 模糊查詢
-- % 匹配任意多個(包括 0 個)字符
SELECT name FROM exam_result WHERE name LIKE '孫%';
-- _ 匹配嚴格的一個任意字符
SELECT name FROM exam_result WHERE name LIKE '孫_';

-- NULL查詢
-- 查詢 qq_mail 已知的同學姓名
SELECT name, qq_mail FROM student WHERE qq_mail IS NOT NULL;
-- 查詢 qq_mail 未知的同學姓名
SELECT name, qq_mail FROM student WHERE qq_mail IS NULL;

-- 分頁查詢 LIMIT
-- 第 1 頁
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 0;
-- 第 2 頁
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 3;
-- 第 3 頁,如果結果不足 3 個,不會有影響
SELECT id, name, math, english, chinese FROM exam_result ORDER BY id LIMIT 3 OFFSET 6;

修改(Update)

-- 語法
UPDATE table_name SET column = expr [, column = expr ...]
 [WHERE ...] [ORDER BY ...] [LIMIT ...]

-- 示例:
-- 將孫悟空同學的數學成績變更爲 80 分
UPDATE exam_result SET math = 80 WHERE name = '孫悟空';
-- 將曹孟德同學的數學成績變更爲 60 分,語文成績變更爲 70 分
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
-- 將總成績倒數前三的 3 位同學的數學成績加上 30 分
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT
3;
-- 將所有同學的語文成績更新爲原來的 2 倍
UPDATE exam_result SET chinese = chinese * 2;

刪除(Delete)

-- 語法
DELETE FROM  table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

-- 示例
-- 刪除孫悟空同學的考試成績
DELETE FROM exam_result WHERE name = '孫悟空';
-- 刪除整張表數據
-- 準備測試表
DROP TABLE IF EXISTS for_delete;
CREATE TABLE for_delete (
 id INT,
 name VARCHAR(20)
);
-- 插入測試數據
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 刪除整表數據
DELETE FROM for_delete;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章