Insert (SQL)

Last updated

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

Contents

Basic form

Insert statements have the following form:

INSERT INTO ''table'' (''column1'' [, ''column2'', ''column3'' ... ]) VALUES (''value1'' [, ''value2'', ''value3'' ... ])

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:

INSERTINTOphone_book(name,number)VALUES('John Doe','555-1212');

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:

INSERT INTO ''table'' VALUES (''value1'', [''value2'', ... ])

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.

INSERTINTOphone_bookVALUES('John Doe','555-1212');

Advanced forms

Multirow inserts

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:

INSERTINTOtablename(column-a,[column-b,...])VALUES('value-1a',['value-1b',...]),('value-2a',['value-2b',...]),...

This feature is supported by IBM 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):

INSERTINTOphone_bookVALUES('John Doe','555-1212'),('Peter Doe','555-2323');

which may be seen as a shorthand for the two statements

INSERTINTOphone_bookVALUES('John Doe','555-1212');INSERTINTOphone_bookVALUES('Peter Doe','555-2323');

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:

INSERTINTOphone_bookSELECT'John Doe','555-1212'UNIONALLSELECT'Peter Doe','555-2323';

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

To do the same in Oracle use the DUAL table, which always consists of a single row only:

INSERTINTOphone_bookSELECT'John Doe','555-1212'FROMDUALUNIONALLSELECT'Peter Doe','555-2323'FROMDUAL

A standard-conforming implementation of this logic shows the following example, or as shown above:

INSERTINTOphone_bookSELECT'John Doe','555-1212'FROMLATERAL(VALUES(1))ASt(c)UNIONALLSELECT'Peter Doe','555-2323'FROMLATERAL(VALUES(1))ASt(c)

Oracle PL/SQL supports the INSERT ALL statement, where multiple insert statements are terminated by a SELECT: [1]

INSERTALLINTOphone_bookVALUES('John Doe','555-1212')INTOphone_bookVALUES('Peter Doe','555-2323')SELECT*FROMDUAL;

In Firebird inserting multiple rows can be achieved like this:

INSERTINTOphone_book(name,number)SELECT'John Doe','555-1212'FROMRDB$DATABASEUNIONALLSELECT'Peter Doe','555-2323'FROMRDB$DATABASE;

Firebird, however, restricts the number of rows than can be inserted in this way, since there is a limit to the number of contexts that can be used in a single query.

Copying rows from other tables

An INSERT statement can also be used to retrieve data from other tables, modify it if necessary and insert it directly into the table. All this is done in a single SQL statement that does not involve any intermediary processing in the client application. A subselect is used instead of the VALUES clause. The subselect can contain joins, function calls, and it can even query the same table into which the data is inserted. Logically, the select is evaluated before the actual insert operation is started. An example is given below.

INSERTINTOphone_book2SELECT*FROMphone_bookWHEREnameIN('John Doe','Peter Doe')

A variation is needed when some of the data from the source table is being inserted into the new table, but not the whole record. (Or when the tables' schemas are not the same.)

INSERTINTOphone_book2(name,number)SELECTname,numberFROMphone_bookWHEREnameIN('John Doe','Peter Doe')

The SELECT statement produces a (temporary) table, and the schema of that temporary table must match with the schema of the table where the data is inserted into.

Default Values

It is possible to insert a new row without specifying any data, using default values for all columns. However, some databases reject the statement if no data is given, such as Microsoft SQL Server, and in this case the DEFAULT keyword can be used.

INSERTINTOphone_bookVALUES(DEFAULT)

Sometimes databases also support alternative syntax for this; for example, MySQL allows omitting the DEFAULT keyword, and T-SQL can use DEFAULT VALUES instead of VALUES(DEFAULT). The DEFAULT keyword can also be used in normal insertion to explicitly fill a column using that column's default value:

INSERTINTOphone_bookVALUES(DEFAULT,'555-1212')

What happens when a column does not specify a default value is database dependent. For example, MySQL and SQLite will fill in with a blank value (except when in strict mode), while many other databases will reject the statement.

Retrieving the key

Database designers that use a surrogate key as the primary key for every table will run into the occasional scenario where they need to automatically retrieve the database-generated primary key from an SQL INSERT statement for use in other SQL statements. Most systems do not allow SQL INSERT statements to return row data. Therefore, it becomes necessary to implement a workaround in such scenarios. Common implementations include:

Triggers

If triggers are defined on the table on which the INSERT statement operates, those triggers are evaluated in the context of the operation. BEFORE INSERT triggers allow the modification of the values that shall be inserted into the table. AFTER INSERT triggers cannot modify the data anymore, but can be used to initiate actions on other tables, for example, to implement auditing mechanism.

Related Research Articles

A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key links these two tables. Another way to put it: In the context of relational databases, a foreign key is a set of attributes subject to a certain kind of inclusion dependency constraints, specifically a constraint that the tuples consisting of the foreign key attributes in one relation, R, must also exist in some other relation, S, and furthermore that those attributes must also be a candidate key in S. In simpler words, a foreign key is a set of attributes that references a candidate key. For example, a table called TEAM may have an attribute, MEMBER_NAME, which is a foreign key referencing a candidate key, PERSON_NAME, in the PERSON table. Since MEMBER_NAME is a foreign key, any value existing as the name of a member in TEAM must also exist as a person's name in the PERSON table; in other words, every member of a TEAM is also a PERSON.

In the context of SQL, data definition or data description language (DDL) is a syntax for creating and modifying database objects such as tables, indices, and users. DDL statements are similar to a computer programming language for defining data structures, especially database schemas. Common examples of DDL statements include CREATE, ALTER, and DROP.

A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. ANSI-standard SQL specifies five types of JOIN: INNER, LEFT OUTER, RIGHT OUTER, FULL OUTER and CROSS.

The SQL SELECT statement returns a result set of records, from one or more tables.

A data manipulation language (DML) is a computer programming language used for adding (inserting), deleting, and modifying (updating) data in a database. A DML is often a sublanguage of a broader database language such as SQL, with the DML comprising some of the operators in the language. Read-only selecting of data is sometimes distinguished as being part of a separate data query language (DQL), but it is closely related and sometimes also considered a component of a DML; some operators may perform both selecting (reading) and writing.

A data control language (DCL) is a syntax similar to a computer programming language used to control access to data stored in a database (authorization). In particular, it is a component of Structured Query Language (SQL). Data Control Language is one of the logical group in SQL Commands. SQL is the standard language for relational database management systems. SQL statements are used to perform tasks such as insert data to a database, delete or update data in a database, or retrieve data from a database.

A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for maintaining the integrity of the information on the database. For example, when a new record is added to the employees table, new records should also be created in the tables of the taxes, vacations and salaries. Triggers can also be used to log historical data, for example to keep track of employees' previous salaries.

The following tables compare general and technical information for a number of relational database management systems. Please see the individual products' articles for further information. Unless otherwise specified in footnotes, comparisons are based on the stable versions without any add-ons, extensions or external programs.

A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.

A user-defined function (UDF) is a function provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment. UDFs are usually written for the requirement of its creator.

Null (SQL) Marker used in SQL databases to indicate a value does not exist

Null or NULL is a special marker used in Structured Query Language to indicate that a data value does not exist in the database. Introduced by the creator of the relational database model, E. F. Codd, SQL Null serves to fulfil the requirement that all true relational database management systems (RDMS) support a representation of "missing information and inapplicable information". Codd also introduced the use of the lowercase Greek omega (ω) symbol to represent Null in database theory. In SQL, NULL is a reserved word used to identify this marker.

A relational database management system uses SQL MERGE statements to INSERT new records or UPDATE existing records depending on whether condition matches. It was officially introduced in the SQL:2003 standard, and expanded in the SQL:2008 standard.

The DUAL table is a special one-row, one-column table present by default in Oracle and other database installations. In Oracle, the table has a single VARCHAR2(1) column called DUMMY that has a value of 'X'. It is suitable for use in selecting a pseudo column such as SYSDATE or USER.

This Comparison of programming languages compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages.

A hierarchical query is a type of SQL query that handles hierarchical model data. They are special cases of more general recursive fixpoint queries, which compute transitive closures.

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

In database management systems (DBMS), a prepared statement or parameterized statement is a feature used to pre-compile SQL code, separating it from data. Benefits of prepared statements are:

PL/SQL is Oracle Corporation's procedural extension for SQL and the Oracle relational database. PL/SQL is available in Oracle Database, Times Ten in-memory database, and IBM Db2. Oracle Corporation usually extends PL/SQL functionality with each successive release of the Oracle Database.

In relational databases a virtual column is a table column whose value is automatically computed using other columns values, or another deterministic expression. Virtual columns are defined of SQL:2003 as Generated Column, and are only implemented by some DBMSs, like MariaDB, SQL Server, Oracle, PostgreSQL, SQLite and Firebird.

The syntax of the SQL programming language is defined and maintained by ISO/IEC SC 32 as part of ISO/IEC 9075. This standard is not freely available. Despite the existence of the standard, SQL code is not completely portable among different database systems without adjustments.

References

  1. "Oracle PL/SQL: INSERT ALL". psoug.org. Retrieved 2010-09-02.
  2. "Firebird 2.5 Language Reference Update" . Retrieved 2011-10-24.
  3. "Firebird SQL Language Dictionary".