I. insert and replace Both insert and replace statements Insert new data into the table. The syntax of these two statements is similar. The main difference between them is how to process repeated data.
1. General insert usage The insert statement in MySQL is not the same as the standard insert statement. In the standard SQL statement, the insert statement that inserts a record at a time has only one form.
Insert into tablename (column name ...) Values (column value );
There is another form in MySQL.
Insert into tablename set column_name1 = value1, column_name2 = value2 ,...;
The first method separates the column name from the column value. during use, the column name must be consistent with the number of column values. The following statement inserts a record into the users table:
Insert into users (ID, name, age) values (123, 'Yao Ming ', 25 );
The second method allows the column names and column values to appear and use in pairs. The following statement will produce the same effect.
Insert into users set id = 123, name = 'Yao Ming ', age = 25;
If the set method is used, a value must be assigned to at least one column. If a field uses a missing value (such as default value or auto-increment value), you can omit these fields in either of the two methods. If auto-increment is used in the ID field, the preceding two statements can be written as follows:
Insert into 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 a new record using the default value of each column in the table.
Insert into users () values ();
If nothing is written after the table name, it indicates that all fields in the table are assigned a value. In this way, not only must the value in values be consistent with the number of columns, but the order cannot be reversed. Insert into users values (123, 'Yao ming', 25 );
If the insert statement is written as follows, MySQL will report an error.
Insert into users values ('Yao Ming ', 25 );
2. Insert multiple records If you see this title, you may ask, is there anything you can say about it? Won't you be able to insert multiple records after multiple insert statements are called! However, using this method increases the load on the server, because every SQL Server executes the same SQL Analysis and Optimization operations. Fortunately, MySQL provides another solution, that is, using an insert statement to insert multiple records. This is not a standard SQL syntax, so it can only be used in MySQL.
Insert into users (name, age) values ('Yao ming', 25), ('bill. Gates', 50), ('marker', 600 );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service