Add, delete, check
Table of Contents
New
grammar:
INSERT [INTO] table_name
[(column [, column] ...)] VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
Case:
– Create a student form
CREATE TABLE student (
id INT,
sn INT comment '学号',
name VARCHAR(20) comment '姓名',
qq_mail VARCHAR(20) comment 'QQ邮箱'
);
1.1 Single row data + full column insertion
INSERT INTO student VALUES (100, 10000, '唐三藏', NULL);
1.2 Multi-row data + specified column insertion
插入两条记录,value_list 数量必须和指定列数量及顺序一致
INSERT INTO student (id, sn, name) VALUES
(102, 20001, '曹孟德'),
(103, 20002, '孙仲谋');
2. Inquiry
grammar:
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
Examples:
2.1 Full column query
grammar:
select * from 表名(student);
2.2 Specify column query
2.3 Alias
grammar:
SELECT column [AS] alias_name [...] FROM table_name
Examples:
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);
In the result set, the column name of the table header = alias
select id,name,chinese + math +english from exam_result;
2.4 Deduplication: DISTINCT
Examples:
SELECT math FROM exam_result;
Deduplication:
select distinct math from exam_result;
2.5 Sorting: ORDER BY
grammar:
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
- For queries without an ORDER BY clause, the order returned is undefined. Never rely on this order
- NULL data sorting is considered to be smaller than any value, ascending order appears at the top, descending order appears at the bottom
查询ID和姓名,按ID进行排序
select id,name from student order by id;
2.6 Conditional query: WHERE
Comparison operators
Note:
- WHERE conditions can use expressions, but they cannot use aliases.
- AND takes precedence over OR, when used at the same time, we need to use parentheses () part of the implementation of priority parcels
Case:
basic query:
-- 查询英语不及格的同学及英语成绩 ( < 60 )
SELECT name, english FROM exam_result WHERE english < 60;
– Query students whose language scores are better than English
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
– Query students with a total score below 200
SELECT name, chinese + math + english 总分 FROM exam_result
WHERE chinese + math + english < 200
AND and OR:
– Query students with a Chinese score greater than 80 points and an English score greater than 80 points
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;
Range query:
1.BETWEEN ... AND ...
select * from exam_result where chinese between 60 and 90;
Note that range from small to large can only
2.IN
-- 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);
Simulation query: LIKE
select id,name from exam_result where name like '孙%';
– _ Matches exactly one arbitrary character
select id,name from exam_result where name like '孙_';
3. Modify (Update)
grammar:
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
Case:
– Change the math score of Sun Wukong to 80 points
UPDATE exam_result SET math = 80 WHERE name = '孙悟空';
– Changed Cao Mengde ’s math score to 60 points and Chinese score to 70 points
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
4. Delete
grammar:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
– Delete the test scores of Zhu Wuneng
delete from exam_result where name = '猪悟能';