3. Data manipulation

Grammatical format hints in SQL sentences:

1. The content in square brackets ([]) is optional;

2. [, ...] means that the previous content can be repeated;

3. Curly brackets ({}) and vertical bars (|) indicate options, and only one of the options needs to be selected;

(1) Insert data: INSERT

After the data table is created, you can insert data into the data table. In MySQL, you can use the INSERT statement to insert single/multiple data into the data table at one time.

1.1: Insert a single piece of data into the data table:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

 Note: If a field is defined with a non-null constraint but no default value constraint, then the field must be assigned a value when inserting new data, otherwise the database system will prompt an error.

extension:

The INSERT statement also has a syntax that uses the SET clause to add data to specified fields or all fields in the table:

INSERT INTO table_name SET column1=value1, column2=value2, ...;

1.2: Insert multiple pieces of data into the data table at one time:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...), (value1, value2, value3, ...), ...;

(2) Update data: UPDATE

The SQL language update data table statement is as follows:

UPDATE table_name SET column1=value1, column2=value2, ... WHERE condition;

(3) Delete data: DELETE

The SQL language delete data table statement is as follows:

DELETE FROM table_name WHERE condition;

 extension:

In the MySQL database, there is another way to delete all the data in the table. This way uses the keyword TRUNCATE, and the syntax is as follows:

TRUNCATE [TABLE] data table name;

TRUNCATE TABLEThe statement is used to clear all the data in the specified MySQL table. It should be noted that TRUNCATE TABLEwhen the statement is used to clear the data, all the data in the table will be deleted, and DELETE FROMthe transaction log will not be created like the statement. Therefore, using TRUNCATE TABLEa statement may be faster and easier to use, but it also needs to be aware that it will delete all data in the table, so it should be used with caution.

 

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/131195645