Home > Database > SQL > body text

Usage of insert statement in SQL

小老鼠
Release: 2024-05-06 15:45:25
Original
1115 people have browsed it

The INSERT statement is used to insert new records into a database table. The syntax is: INSERT INTO table name (column 1, column 2, ...) VALUES (value 1, value 2, ...); the INSERT statement has the following variants: select data insertion from other tables or subqueries, If the primary key conflicts, the existing record will be updated. If the primary key conflicts, the insertion will be ignored. When no column name is specified, values ​​are inserted in the order defined by the table; no value is required for auto-increment columns; the number of affected rows is 1 on successful insertion; an error is thrown on failure.

Usage of insert statement in SQL

INSERT statement usage

The INSERT statement is used in SQL (Structured Query Language) to enter database tables Statement to insert new records. Its syntax is as follows:

<code>INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)</code>
Copy after login

Usage instructions:

  • table_name: The name of the table into which records are to be inserted.
  • column1, column2, ...: Column names to insert data into. If omitted, inserts will go into all non-auto-increment columns.
  • value1, value2, ...: The value to be inserted into the specified column. The order of values ​​must be the same as the order of column names.

Syntax variations:

In addition to the basic syntax, the INSERT statement has the following variations:

  • INSERT ... SELECT: Select data from another table or subquery and insert it.
  • INSERT ... ON DUPLICATE KEY UPDATE: If the primary key value of the record to be inserted matches an existing record in the table, update the existing record.
  • INSERT ... IGNORE: If the primary key value of the record to be inserted matches an existing record in the table, the insertion is ignored.

Additional Notes:

  • If no column name is specified, the inserted values ​​will be inserted in the order defined by the table.
  • If the column to be inserted is an auto-increment column, there is no need to specify its value.
  • The INSERT statement affects the number of records in the table. If the insert is successful, the number of rows affected is 1.
  • If the insertion fails (for example, due to a data type or constraint conflict), an error is raised.

The above is the detailed content of Usage of insert statement in SQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template