(8) DML operation in MySQL

Table of contents

1. Insert data (INSERT) 

1. Select Insert:

 2. Fully inserted

 Second, the default value processing (DEFAULT)

1. Specify the default value of the column when creating the table

 2. Modify the table to add a new column and specify a default value

 3. Default value processing when inserting data

3. Update data (UPDATE)

Fourth, delete data (DELETE)

1. DELETE to delete data

2. TRUNCATE clears the table


 


1. Insert data (INSERT) 

1. Select Insert:

Add data to the specified column according to our needs 

INSERT INTO  表名(列名 1 ,列名 2 ,列名 3.....) VALUES(值 1 ,值 2 ,值 3......);

 Example: Add a piece of data to the departments table, the department name is market, and the workplace ID is 1.

insert into departments(department_name,location_id) values("market", 1);

 2. Fully inserted

INSERT INTO  表名 VALUES(值 1 ,值 2 ,值 3......);

Note: If the primary key is auto-growth, you need to use default or null or 0 placeholder.

Example 1: Add a piece of data to the departments table, the department name is development, and the workplace ID is 2. Use the default placeholder.

insert into departments values(default,"development",2);

Example 2: Add a piece of data to the departments table, the department name is human, and the workplace ID is 3. Use a null placeholder.

insert into departments values(null,"human",3);

Example 3: Add a piece of data to the departments table, the department name is teaching, and the workplace ID is 4. Use a 0 placeholder.

insert into departments values(0,"teaching",4);


 Second, the default value processing (DEFAULT)

You can use DEFAULT to set a default value for a column in MySQL. If the value of the column is not specified when inserting data, MySQL will add the default value to the column.

1. Specify the default value of the column when creating the table

CREATE TABLE 表名(列名 类型 default 默认值,...);

Example: Create emp3 table, which contains emp_id primary key and auto-growth, contains name, and contains address. The default value of this column is "unknown".

create table emp3(emp_id int primary key auto_increment,name varchar(10),address  varchar(50) default 'unknown');

 2. Modify the table to add a new column and specify a default value

ALTER TABLE 表名 ADD COLUMN 列名 类型 DEFAULT 默认值;

Example: modify emp3 table, add job_id, the default value of this column is 0.

alter table emp3 add column job_id int default 0;

 3. Default value processing when inserting data

If the value of the column is not specified when inserting data, MySQL will add the default value to the column. If it is a complete item insertion, you need to use default to occupy the place.

Example: To add data to the emp3 table, require the address column and job_id column to use the default value as the value of the column.

insert into emp3(name) values("admin");
insert into emp3 values(default,"oldlu",default,default);


3. Update data (UPDATE)

UPDATE 表名 SET 列名=值,列名=值 WHERE 条件;

Note: The update condition must be given in the update statement, otherwise all the data in the table will be updated.

Example: update the data whose id is 1 in the emp3 table, and add address as BeiJing.

update emp3 set address = "BeiJing" where emp_id = 1;


Fourth, delete data (DELETE)

1. DELETE to delete data

DELETE FROM 表名 WHERE 条件;

Note: In the DELETE statement, if no deletion condition is given, all data in the table will be deleted.

Example: Delete the employee information whose emp_id is 1 in the emp3 table.

delete from emp3 where emp_id = 1;

2. TRUNCATE clears the table

TRUNCATE TABLE 表名;

Example: Delete all data in the emp3 table.

truncate table emp3;

The difference between DELETE and TRUNCATE when emptying the table

  • truncate is to delete as a whole (faster) , delete is to delete one by one (slower);
  • Truncate does not write server log , delete writes server log, which is why truncate is more efficient than delete;
  • Truncate will reset the self-increment value , which means that the self-increment column will be set to the initial value and start recording from 1 again instead of following the original value. After delete, the self-increment value will still continue to accumulate .

Guess you like

Origin blog.csdn.net/m0_62735081/article/details/126707991