MySQL table of additions and deletions to change search

table of Contents

New data

Query data 

Full column inquiry

Specifies the query column

Query field for the expression 

Aliases

Deduplication 

Sequence

Criteria query: WHERE

Paging query: LIMIT 

change the data 

delete data 

Key summary 


Around a table, the four most important additions and deletions to check the operation is changed. That CRUD, represents the increase (the Create) , query (Retrieve) , update (Update) , delete the first letter (Delete) four words of the acronym.

New data

First, let's build a table:

DROP TABLE IF EXISTS student; 
CREATE TABLE student ( id INT,
	              sn INT comment '学号',
		     name VARCHAR(20) comment '姓名',
	            qq_mail VARCHAR(20) comment 'QQ邮箱'
  );

For the new column of data, into the full row insertion and specified column insert ; data for the new row into a single row insertion and bulk inserts.

If the whole columns into a single line + data: number and quantity must value_list order of columns and consistent definition table

INSERT INTO student VALUES (1, 100, '哪吒', NULL);
INSERT INTO student VALUES (2, 101, '孙悟空', '11111');

When multiple rows of data designated as + insert columns: Number value_list must specify the number of columns in the same order and

INSERT INTO student (id, sn, name) VALUES 
(3, 200, '胡图图'), 
(4, 201, '懒羊羊');

Query data 

First, establish a results table, and insert data.

CREATE DATABASE Example;
USE Example;
CREATE TABLE exam_result ( id INT, name VARCHAR(20), 
							chinese INT,
							math INT,
							english INT 
							);
-- 插入测试数据 
INSERT INTO exam_result (id,name, chinese, math, english) VALUES 
 (1,'A', 67, 98, 56),
 (2,'B', 87, 78, 77),
 (3,'C', 88, 98, 90), 
 (4,'D', 82, 84, 67), 
 (5,'E', 55, 85, 45),
 (6,'F', 70, 73, 78),
 (7,'G', 75, 65, 30);

Full column inquiry

SELECT * FROM exam_result; 

Full column data transmission of large query, * represents all fields in order to build a table display.

Specifies the query column

SELECT id, name, english FROM exam_result;

You can not specify the order of the columns in the order defined in the table, check out the order is the order of the specified column.

Query field for the expression 

 Expression does not contain field 

 SELECT id, name, 10 FROM exam_result;  //每列后面都加一个10
Expression contains a field 
 
SELECT id, name, english + 10 FROM exam_result;  //给英语加10分

Expression contains multiple fields

SELECT id, name, chinese + math + english FROM exam_result; //查询总分

Aliases

 SELECT id, name, chinese + math + english AS 总分 FROM exam_result;

AS write the keyword alias, AS can be omitted.

Deduplication 

Use DISTINCT keyword data to a column weight, i.e. keep only one of the repeating element.
 
SELECT DISTINCT math FROM exam_result;

Sequence

1. no ORDER BY clause of a query, the order returned is undefined, never rely on this order

2. NULL data sorting, are considered to be smaller than any value, appears at the top ascending, descending appear at the bottom

3. Use expressions and an alias sort

 SELECT name, chinese + english + math FROM exam_result ORDER BY chinese + english + math DESC;
 SELECT name, chinese + english + math total FROM exam_result ORDER BY total DESC;

4 can sort on multiple fields, with the writing order sort priority 

SELECT name, math, english, chinese FROM exam_result ORDER BY math DESC, english, chinese;

Criteria query: WHERE

Before the query conditions, we must first understand some of the operators:

Comparison operators:

Logical Operators: 

a. '=' represent equal, NULL unsafe. The result is NULL = NULL NULL; <=> represents equal, NULL security; NULL not sure do not know. NULL expression result of the participation of still NULL, WHERE are regarded as false.

b.! =, <> indicates not equal

c. BETWEEN a0 AND a1 represents a matching range [a0, a1]

d. IN (...) is represented by one of

e. IS NULL / IS NOT NULL is determined whether the air

. F LIKE represent fuzzy matching,% represents any number (including zero characters); represents any character _

g. AND OR NOT and NOR, and a higher priority than or

h. WHERE condition expression may be used, but not aliases

-- 查询英语不及格的同学及英语成绩 ( < 60 ) 
SELECT name, english FROM exam_result WHERE english < 60;

-- 查询语文成绩大于80分,且英语成绩大于80分的同学 
SELECT * FROM exam_result WHERE chinese > 80 and english > 80;

-- 查询语文成绩在 [80, 90] 分的同学及语文成绩
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;

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

-- % 匹配任意多个(包括 0 个)字符 
SELECT name FROM exam_result WHERE name LIKE 'A%';
-- _ 匹配严格的一个任意字符 
SELECT name FROM exam_result WHERE name LIKE 'A_';

-- 查询 qq_mail 已知的同学姓名 
SELECT name, qq_mail FROM student WHERE qq_mail IS NOT NULL;

Paging query: LIMIT 

Syntax: After attention must be sorted (ORDER BY) and then paging query (LIMIT)

-- 起始下标为 0 -- 从 0 开始,筛选 n 条结果 
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始,筛选 n 条结果 
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n; 
-- 从 s 开始,筛选 n 条结果,比第二种用法更明确
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

Examples: Press id paging, page . 3 records, respectively, show the first 1 , 2 , . 3 this page

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

change the data 

-- 将A的数学成绩变更为 80 分 
 UPDATE exam_result SET math = 80 WHERE name = 'A';
 
 -- 将总成绩倒数前三的 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 data 

-- 删除A同学的考试成绩 
 DELETE FROM exam_result WHERE name = 'A'; 
 -- 删除整张表数据
 DELETE FROM exam_result;

Key summary 

  • increase
-- 单行插入 
  INSERT INTO 表(字段1, ..., 字段N) VALUES (value1, ..., value N);
  -- 多行插入 
  INSERT INTO 表(字段1, ..., 字段N) VALUES (value1, ...), (value2, ...), (value3, ...);
  • check
  -- 全列查询 
  SELECT * FROM 表 
  -- 指定列查询 
  SELECT 字段1,字段2... FROM 表 
  -- 查询表达式字段 
  SELECT 字段1+100,字段2+字段3 FROM 表 
  -- 别名 
  SELECT 字段1 别名1, 字段2 别名2 FROM 表 
  -- 去重DISTINCT 
  SELECT distinct 字段 FROM 表 
  -- 排序
  ORDER BY SELECT * FROM 表 ORDER BY 排序字段 
  -- 条件查询WHERE: 
  -- (1)比较运算符 (2)BETWEEN ... AND ... (3)IN (4)IS NULL (5)LIKE (6)AND (7)OR (8)NOT 
  SELECT * FROM 表 WHERE 条件
 
  • change
UPDATE 表 SET 字段1=value1, 字段2=value2... WHERE 条件
  • delete 
DELETE FROM 表 WHERE 条件

 Additions and deletions to change search operation is the most basic, and most important operation. Continue in-depth understanding and practice!

 

 

 

 

 

 

发布了51 篇原创文章 · 获赞 14 · 访问量 2302

Guess you like

Origin blog.csdn.net/qq_41185460/article/details/104087436