First, insert and replace
The INSERT and replace statements have the ability to insert new data into a table. The syntax for these two statements is similar. The main difference between them is how to handle duplicate data.
1. General usage of Inserts
The INSERT statement in the MySQL tutorial is not the same as the standard insert, and in a standard SQL statement, there is only one form of INSERT statement that inserts one record at a time.
INSERT INTO tablename (column name ...) VALUES (column value);
And there's another form in MySQL.
INSERT INTO tablename set column_name1 = value1, column_name2 = value2, ...;
The first method separates the column names from the column values, and when used, the column names must be the same as the number of column values. As the following statement inserts a record into the users table:
Insert into the users (ID, name, age) VALUES (123, ' Yao ', 25);
The second method allows column names and column values to appear and be used in pairs, such as the following statement, which produces a medium-like effect.
INSERT INTO users set id = 123, name = ' Yao Ming ', age = 25;
If you use the Set method, you must assign at least one column. If a field uses a saved value (such as Default or self-increment), these fields can be omitted from either of these methods. The above two statements can be written in the following form, such as using the self-increment on the ID field:
Insert into the users (name, age) VALUES (' Yao Ming ', 25);
Insert INTO uses set name = ' Yao Ming ', age = 25;
MySQL has also made some changes in values. If nothing is written in values, MySQL inserts the new record using the default values for each column in the table.