Addition, deletion, modification and query of tables in Chapter 4
We covered the creation and modification of libraries and tables in the previous two chapters. Then this chapter explains how to insert data, change data, delete data and query data into the table after we create the library and table.
One, create (increase)
Let's first create a student table.
There are three variables in this table, the student's student number (id), the student's name (name), and the student's mailbox (email).
1. Single row data + full column insert
(1) Grammar
insert into 表的名称 values (变量1的值,变量2的值...);
(2) Example
2. Multi-row data + full column insert
(1) Grammar
insert into 表的名称 values (变量1的值,变量2的值...),(变量1的值,变量2的值...),(变量1的值,变量2的值...)....;
(2) Example
3. Insert the specified column
(1) Grammar
insert into 表的名称(变量1,变量2...) values (变量1的值,变量2的值...),(变量1的值,变量2的值...)...;
(2) Example
4. Update the inserted data
When we insert data, the insertion often fails because the value corresponding to the primary key or the unique key already exists. At this point, we can use some grammatical operations to let our insert update the existing data.
(1) Grammar
在插入数据的语句后方加上:on duplicate key update 变量1 = 更新值1, 变量2 = 更新值2....;
(2) Example
When we enter the insert statement, if the insert is successful, mysql will give us feedback, ie Query OK, 2 rows affected (0.08 sec)
. Then the few rows here are updated, which can actually feedback whether our data is updated after a conflict or inserted directly.
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
5. Replace data
There are two categories to discuss here. If the data we insert does not conflict with the primary key and the unique key, the data is inserted directly. If our inserted data conflicts with the primary key and unique key, delete the original data and then insert it.
(1) Grammar
replace into 表的名称 (变量1,变量2...) values (变量值1,变量值2...);
(2) Content
in:
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,删除后重新插入
Two, Retrieve (query)
1、select
(1) Full column query
select * from 表的名称;
(2) Specified column query
select 变量1,变量2... from 表的名称;
(3) Expressions as query columns
select 变量1,变量2...表达式1,表达式2... from 表的名称;
(4) Specify an alias for the query result
select 变量1 别名1,变量2 别名2...from 表的名称;
(5) Deduplication of results
select distinct 变量1,变量2.... from 表的名称;
2、where
Before introducing the where statement, let's create a grade table and insert some data. The statement is as follows:
Create a table:
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 '数学成绩',
english float DEFAULT 0.0 COMMENT '英语成绩'
);
Insert data:
insert into exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);
(1) Operator
comparison operator
Logical Operators
(2) where purpose
Where is often followed by certain conditional statements to filter out specific data that meets the conditions.
(3) Grammar
select * from 表的名称 where 数据满足的条件;
(4) Examples
Students who fail in English and their English scores ( < 60 )
select * from exam_result where english < 60;
Students with Chinese scores in [80, 90] and their Chinese scores
方法1:
select * from exam_result where chinese >= 80 and chinese <= 90;
方法2:
select * from exam_result where chinese between 80 and 90;
Students whose math scores are 58 or 59 or 98 or 99 and their math scores
方法1:
select * from exam_result where math = 58 or math = 59 or math = 98 or math = 99;
方法2:
select * from exam_result where math in (58, 59, 98, 99);
classmate surnamed sun
select * from exam_result where name like '孙%';
classmate Sun
select * from exam_result where name like '孙_';
Students with a total score of 200 or less
select name, chinese+math+english 总分 from exam_result where chinese+math+english < 200;
query for NULL
The stu table created earlier is used here.
select * from stu where email is not null;
select * from stu where email is null;
3. Sorting results
(1) Grammar
ascending order
select * from 表的名称 order by 变量名(或者表达式) asc
sort descending
select * from 表的名称 order by 变量名(或者表达式) desc
If we don't write asc or desc, mysql defaults to ascending order.
(2) Example
select * from exam_result order by chinese asc;
select name 姓名, chinese+math+english 总分 from exam_result order by chinese+math+english desc;
4. Filter paging results
(1) Grammar
- Starting from 0, filter n results
select 语句后面加 limit n;
- Starting from s, filter n results
select 语句后面加 limit s, n;
- Starting from s, filter n results, which is more specific than the second usage, it is recommended to use
select 语句后面加 limit n offset s;
(2) Example
Select the student with the highest total score.
3. Update (update/modify)
1. Grammar
update 表的名称 set 变量1=更新值1,变量2=更新值2... [where...] [order by...] [limit ..]
[ ] brackets are optional options.
If we do not filter the data later, the statement will update the data in the entire table.
2. Examples
Change Cao Mengde's mathematics score to 60 points and Chinese score to 70 points
Four, Delete (delete)
1、delete
(1) Grammar
delete from 表的名称 [where...] [order by...] [limit...];
[ ] Brackets are optional options. If we do not add any conditions, the statement will delete the data of the entire table.
(2) Example
Delete the test scores of Sun Wukong
2、truncate
(1) Grammar
truncate 表的名称;
(2) The difference between truncate and delete
-
delete can delete specific filtered data, but truncate can only delete the entire table.
-
delete will not reset the value of auto_increment, but truncate will reset auto_increment
(3) Example
Let's prove the point here: whether to reset auto_increment.
Before the proof, we first create a table T with only id variables. As follows:
Then insert multiple data:
Let's look at the delete statement first:
first use delete to delete the data in the entire table.
Then we insert a data to see what is the value added by the system by default.
We found that 4 was inserted by default, not 1.
We can also check the self-increasing value by looking at the statement when creating the table.
4 should have been displayed here, but after we deleted it, we inserted another number, so 5 is displayed here.
Then we look at the truncate statement:
Let's create another table T2, which still has only one self-increasing variable id.
Then we delete the table with the truncate statement.
Then we delete this table, and then insert an empty data to see why the self-growth will assign a value to the data?
At this time, 1 is inserted, indicating that truncate has indeed reset the self-growth attribute.
5. Insert query results
We can use the insert statement to insert data, and we can also use select to query data. So can we insert the queried data?
The answer is yes.
1. Grammar
insert into 表的名称[变量1,变量2....] select ...
2. Examples
First create an ordinary table T3.
Then, insert several sets of data.
Then we insert 1 again.
Check to see if we inserted successfully: