*** Welcome to piglix ***

Insert (SQL)


An SQL INSERT statement adds one or more records to any single table in a relational database.

Insert statements have the following form:

The number of columns and values must be the same. If a column is not specified, the default value for the column is used. The values specified (or implied) by the INSERT statement must satisfy all the applicable constraints (such as primary keys, CHECK constraints, and NOT NULL constraints). If a syntax error occurs or if any constraints are violated, the new row is not added to the table and an error returned instead.

Example:

Shorthand may also be used, taking advantage of the order of the columns when the table was created. It is not required to specify all columns in the table since any other columns will take their default value or remain null:

Example for inserting data into 2 columns in the phone_book table and ignoring any other columns which may be after the first 2 in the table.

A SQL feature (since SQL-92) is the use of row value constructors to insert multiple rows at a time in a single SQL statement:

This feature is supported by DB2, SQL Server (since version 10.0 - i.e. 2008), PostgreSQL (since version 8.2), MySQL, sqlite (since version 3.7.11) and H2.

Example (assuming that 'name' and 'number' are the only columns in the 'phone_book' table):

which may be seen as a shorthand for the two statements

Note that the two separate statements may have different semantics (especially with respect to statement triggers) and may not provide the same performance as a single multi-row insert.

To insert multiple rows in MS SQL you can use such a construction:

Note that this is not a valid SQL statement according to the SQL standard (SQL:2003) due to the incomplete subselect clause.


...
Wikipedia

...