General INSERT usage in MySQL

Source: Internet
Author: User
Tags mysql index

INSERT statements are one of the most common SQL statements, but the use of INSERT statements in MySQL is different from the standard usage. The following describes the general usage of INSERT statements in MySQL for your reference.

General INSERT usage in MySQL:

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 default value, for example, a default value or an auto-increment value, either of these fields can be omitted. 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 );

MySQL index type

How to modify the order of mysql Fields

How to add or delete a primary key in mysql

How to Set MySql access restrictions

Six Methods for changing the password of MySQL

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.