[MySQL] Add, delete, check and modify the table

Add, delete, check

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);

Insert picture description here

1.2 Multi-row data + specified column insertion

插入两条记录,value_list 数量必须和指定列数量及顺序一致
INSERT INTO student (id, sn, name) VALUES
 (102, 20001, '曹孟德'),
 (103, 20002, '孙仲谋');

Insert picture description here

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);

Insert picture description here

2.2 Specify column query

Insert picture description here

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;

Insert picture description here

2.4 Deduplication: DISTINCT

Examples:

SELECT math FROM exam_result;

Insert picture description here
Deduplication:

select distinct math from exam_result;

Insert picture description here

2.5 Sorting: ORDER BY

grammar:

-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
 ORDER BY column [ASC|DESC], [...];
  1. For queries without an ORDER BY clause, the order returned is undefined. Never rely on this order
  2. 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;

Insert picture description here

2.6 Conditional query: WHERE

Comparison operators
Insert picture description here
Note:

  1. WHERE conditions can use expressions, but they cannot use aliases.
  2. 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;

Insert picture description here
– Query students whose language scores are better than English

SELECT name, chinese, english FROM exam_result WHERE chinese > english;

Insert picture description here
– Query students with a total score below 200

SELECT name, chinese + math + english 总分 FROM exam_result
 WHERE chinese + math + english < 200

Insert picture description here
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;

Insert picture description here
Range query:
1.BETWEEN ... AND ...

select * from exam_result where chinese between 60 and 90;

Note that range from small to large can only
Insert picture description here
2.IN

-- 查询数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);

Insert picture description here
Simulation query: LIKE

select id,name from exam_result where name like '孙%';

Insert picture description here
– _ Matches exactly one arbitrary character

select id,name from exam_result where name like '孙_';

Insert picture description here

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 = '孙悟空';

Insert picture description here
– 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 = '曹孟德';

Insert picture description here

4. Delete

grammar:

DELETE FROM  table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

– Delete the test scores of Zhu Wuneng

delete from exam_result where name = '猪悟能';

Insert picture description here

Published 42 original articles · Like 154 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_43676757/article/details/105436772