Add, update, and delete data

Add, update, and delete data

First, add data

1, add all the fields in the data table

A method, add the specified field data table

INSERT INTO table name (field 1, field names, .......)

VALUES (value 1, value 2, ......);

The second method to add all the fields in the data table

INSERT INTO table name VALUES (value 1, value 2, .......); (To be consistent with the value of the field name in the table)

2. while adding many records INSERT INTO table VALUES

(Value 1, value 2 and value 3),

(Value 1, value 2 and value 3),

……

(Value 1, value 2, ......);

INSERT INTO table for the specified field name (field 1, field 2) VALUES

(Value 1, value 2),

(Value 1, value 2),

……

(Value 1, value 2);

update data

MySQL use an UPDATE statement to update records in the table, the basic syntax:

SET UPDATE table field name value 1 = 1 [, field name value of 2 = 2, ...] [the WHERE Conditional Expression];

/ * Field names 1 to specify the name of the field to be updated, the value of the new data field updated 1 is used to represent, WHERE conditional expression is optional and is used to specify the conditions to be met to update data * /

  1. UPDATE update some data

First, use the View statement, execution results are as follows:

Use the following UPDATE statement to update this record

If there are many records satisfy the conditional expression in the WHERE clause, the records meet the conditions will occur updates.

From the query results, grade field values ​​are changed to 100, indicating that the records that satisfy the WHERE clause conditional expressions are updated successfully.

  1. UPDATE update all data

The field values ​​are updated to grade 80, UPDATE statement is as follows, performs data update statement in the table, and then view the updated records.

delete data

Use of MySQL DELETE statement to delete records in the table, its syntax is as follows:

DELETE FROM table [the WHERE Conditional Expression];

/ * Specify the table name table to perform the delete operation, WHERE optional parameter to specify the conditions to delete, record satisfies the criteria will be deleted * /

  1. DELETE delete some data

It refers to delete part of the data according to specified conditions to delete a table or a certain number of records, the WHERE clause need to delete the specified record condition.

首先使用查询语句查看,执行结果如下:

下面使用删除记录,语句如下:

从查询结果可以看到记录为空,说明id字段为11的记录被成功删除。

在执行删除操作的表中,如果有多条记录满足WHERE子句中的条件表达式,则满足条件的记录都会被删除。

2、DELETE删除全部数据

在DELETE语句中如果没有使用WHERE子句,则会将表中的所有记录都删除。

在删除数据之前首先使用查询语句查看student表中的所有记录,执行结果如下所示:

删除全部数据,然后再次查看语句,执行结果如下:

从查询结果可以看到记录为空,说明表中所有的记录成功删除。

  1. 使用关键字TRUNCATE删除表中数据

语法格式:TRUNCATE[TABLE]表名;

通过查询结果可以看到记录为空,说明tab_表中的记录被全部删除了。

TRUNCATE语句和DETELE语句都能实现删除表中的所有数据的功能,但两者也有一定的区别

DELETE语句是DML语句,TRUNCATE语句通常被认为是DDL语句。

DELETE语句后面可以跟WHERE子句,通过指定WHERE子句中的条件表达式只删除满足条件的部分记录,而TRUNCATE语句只能用于删除表中的所有记录

使用TRUNCATE语句删除表中的数据后,再次向表中添加记录时,自动增加字段的默认初始值重新由1开始,而使用DELETE语句删除表中所有记录后,再次向表中添加记录时,自动增加字段的值为删除时该字段的最大值加1。


 从查询结果可以看出,系统为tab_truncate表中id字段默认添加了值,初始值从1开始。

执行INSERT语句向tab_truncate表中添加一条记录,再次使用查询语句查看表中的记录。

使用DELETE语句时,每删除一条记录都会在日志中记录,而使用TRUNCATE语句时,不会在日志中记录删除的内容,因此TRUNCATE语句的执行效率比DELETE语

Guess you like

Origin www.cnblogs.com/232124jxp/p/12049902.html