Opentopia Directory Encyclopedia Tools

Insert (SQL)

Encyclopedia : I : IN : INS : Insert (SQL)


An SQL INSERT statement adds a record to a table in a relational database.

Basic form

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.

Example:

INSERT INTO phone_book (name, number) VALUES ('John Doe', '555-1212');
When values for all columns in the table are specified, then a shorthand may be used, taking advantage of the order of the columns when the table was created:

Example (assuming that 'name' and 'number' are the only columns in the 'phone_book' table):
INSERT INTO phone_book VALUES ('John Doe', '555-1212');

Optional forms

An optional SQL feature (since SQL-92) is the use of row value constructors to insert multiple rows at a time: This optional feature is supported by DB2 and MySQL.

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

INSERT INTO phone_book VALUES ('John Doe', '555-1212'), ('Peter Doe', '555-2323');
- which may be seen as a shorthand for

INSERT INTO phone_book VALUES ('John Doe', '555-1212');
INSERT INTO phone_book VALUES ('Peter Doe', '555-2323');
Example (creating records in one table from another table / copying records using insert):

INSERT INTO phone_book2 SELECT * FROM phone_book
WHERE NAME IN ('John Doe', 'Peter Doe')

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 a SQL INSERT query for use in another SQL INSERT query. Since SQL INSERT does not return row data, it becomes necessary to implement a workaround to such scenarios. Common implementations include:

See also

Topics in database management systems (DBMS)[ view][ talk][ edit] )
Concepts
Database | Database model | Relational database | Relational model | Relational algebra | Primary key - Foreign key - Surrogate key - Superkey
Database normalization | Referential integrity | Relational DBMS | Distributed DBMS | ACID

Objects
Trigger | View | Table | Cursor | Log | Transaction | Index | Stored procedure | Partition
Topics in SQL
Select | Insert | Update | Merge | Delete | Join | Union | Create | Drop
Comparison of syntax

Implementations of database management systems
Types of implementations
Flat file | Deductive | Dimensional | Hierarchical | Object oriented | Temporal

Products
dBASE | Oracle | Sybase | MySQL | Microsoft SQL Server | PostgreSQL | DB2 | Comparison - relational | Comparison - object-relational
Components
Query language | Query optimizer | Query plan | ODBC | JDBC
Lists
List of object-oriented database management systems
List of relational database management systems
List of truly relational database management systems

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: