Adding MySQL or insert statements (Insert) use several ways

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_40194399/article/details/94554191

Adding MySQL or insert statements (Insert) use several ways

1.INSERT a shorthand way of inserting data (not recommended)

1. look at those data table

Here Insert Picture Description

2. Use Inset into table values (value 1, value 2) insert, and see if the data is successfully inserted

Here Insert Picture Description
  Note : insert this shorthand way although very simple, but the following values must Values tables corresponding to the order of the classes, and the type has to maintain, even if a column does not require a value in the table must also be assigned null, such as our incrementing is not actually set the primary key value of the id, it is used in this way must be assigned null
  reason not recommended : if the insertion data used in the actual development of this method, the back table of the changes (such as changing the order of the fields a) the entire statement will be error, scalability and poor, and more difficult to maintain

2.INSERT complete wording (recommended)

1. Inset into the table name (field 1, field 2) values ​​(value 1, value 2) is inserted, and to check whether the data is successfully inserted

Here Insert Picture Description
  Recommend the use of reason : This time we set the id is not assigned to any value, including null, and do not care about the order of fields in the table, such as the following without the addition of normal order, we will put the first age, name on the second also can be added successfully; Note that the field names following the table name must be consistent with the assigned value following values; the actual development programs than in terms of a better maintenance and expansion. 
Here Insert Picture Description

3.REPLACE INSERT statement

The effect of this statement is that when we insert a piece of data, if the entry already exists, then delete the original data exists to add data insertion, so if there is no direct insert new data. Note: but whether there are points determined by the primary key

Here Insert Picture Description

4.INSERT IGNORE INTO statement

The effect of this statement is ignored then insert the data if the inserted data already exists (that is, without changing the original data), if there is no insert new data.
Note: but whether there are points determined by the primary key

Here Insert Picture Description

5.INSERT bulk insert

1. Inset into the table name (field 1, field 2, field 3) values ​​(value 1, value 2 and value 3); Inset into the table name (field 1, field 2, field 3) values ​​(value 1, value 2 The value 3) insert, and insert the data to see whether the success of

Here Insert Picture Description

2. Inset into shorthand way to use the table name (field 1, field 2, field 3) values ​​(value 1, value 2 and value 3), (value 1, value 2 and value 3) insert, and insert the data view if successful, must ensure that the value of both values ​​and corresponding fields.

Here Insert Picture Description

6.INSERT SELECT statement

1. The effect of this statement is the result of the SELECT statement into a table, the data migration
2. Syntax: insert into the inserted table (field 1, field 2, field 3) select field 1 being queried is queried field 2 from the table name being queried;
3. need to insert the first to see all the data table

Here Insert Picture Description

See table 4 is inserted into all the data

Here Insert Picture Description

5. Execute INSERT SELECT statement and view the results

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_40194399/article/details/94554191