MySQL database - DML basic operation

insert image description here

foreword

Earlier we learned MySQL——DDL operation, the operation on the structure of the database and the table, so today I will share with you MySQL——DML operation, the operation on the table data.

MySQL DML operations are as follows:

  1. Insert operation (INSERT): used to insert new data rows into the database. You can insert a single row of data at a time, or use a single SQL statement to insert multiple rows of data at a time.

  2. Update operation (UPDATE): used to modify existing data rows in the database. Data rows that meet the conditions can be updated according to the specified conditions.

  3. Delete operation (DELETE): Used to delete data rows from the database. Data rows that meet the conditions can be deleted according to the specified conditions.

insert data

In MySQL, there are two ways to insert data into table 1: insert into 表名 values (值1,值2,值3);andinsert into 表名 (列名1,列名2) values (值1,值2);

Insert all columns

The first insertion method is to insert the data of all columns insert into 表名 values (值1,值2,值3);, and the data of the column needs to correspond to the position of the column.

create table student(
	id int,
	name varchar(20),
	chinese decimal(3,1),
	math decimal(3,1),
	english decimal(3,1));
	
insert into student values(01,'张三',85,95.5,90);

insert image description here

If there is a lot of data to be added, can we only add it one by one? Of course not, we can add multiple data at once. Use a comma, to separate each set of data.

insert into student values(02,'李四',87,97.5,87.5),
						(03,'王五',88,90,96);

insert image description here

Specify column to insert

Sometimes, when inserting data, it is not necessary to insert the data of all columns, but you can specify the column to insertinsert into 表名 (列名1,列名2) values (值1,值2);

insert into student(name,math) values('李华',98);

insert image description here
Columns with no data inserted will have default values.

The specified column insertion can also insert multiple sets of data at one time.

insert into student(name,chinese) values('小美',96),
										('小帅',92);

insert image description here

change the data

After we have added data, we can modify the added data. Modify the data of all columns: update 表名 set 列名 值;, modify the data of the specified row:update 表名 set 列名 值 where 条件;

insert image description here

Add 3 points to everyone's Chinese scores.

update student set chinese = chinese + 3;

insert image description here
Subtract 2 points from Zhang San's Chinese score.

update student set english = english - 2 where name = '张三';

insert image description here

delete data

MySQL deletes data to delete a row of data. There are two main ways to delete data in MySQL:
delete from 表名 [where 条件];andtruncate table 表名 / truncate 表名;

delete from 表名 [where 条件];Deleting data is usually followed by wherea conditional statement. If there is no subsequent whereconditional statement, the entire table will be deleted.

delete from student;

insert image description here

delete from 表名 where 条件;Delete the row data that meets the condition.

delete from student where name = '小美';

insert image description here

truncate table 表名;/ truncate 表名;Delete all the data in the table, it is a bit different from delete to delete data, when delete deletes all the data in the table, it just deletes all the data in the table, and truncate deletes data similarly, it will first delete the drop table 表名table To delete, and then create an empty table without data.

truncate table student;
truncate student;

insert image description here

Guess you like

Origin blog.csdn.net/m0_73888323/article/details/131888749