INSERT¶
Name
INSERT -- create new rows in a table
Synopsis
INSERT INTO <table>[@<dblink> ] [ ( <column> [, ...] ) ]
{ VALUES ( { <expression> | DEFAULT } [, ...] )
[ RETURNING <return_expression> [, ...]
{ INTO { <record> | <variable> [, ...] }
| BULK COLLECT INTO <collection> [, ...] } ]
| <query> }
Description
INSERT allows you to insert new rows into a table. You can insert a
single row at a time or several rows as a result of a query.
The columns in the target list may be listed in any order. Each column not present in the target list will be inserted using a default value, either its declared default value or null.
If the expression for each column is not of the correct data type, automatic type conversion will be attempted.
The RETURNING INTO { record | variable [, ...] } clause may only be
specified when the INSERT command is used within an SPL program and only
when the VALUES clause is used.
The RETURNING BULK COLLECT INTO collection [, ...] clause may only be
specified if the INSERT command is used within an SPL program. If more
than one collection is specified as the target of the BULK COLLECT
INTO clause, then each collection must consist of a single, scalar
field – i.e., collection must not be a record. return_expression
evaluated for each inserted row, becomes an element in collection
starting with the first element. Any existing rows in collection are
deleted. If the result set is empty, then collection will be empty.
You must have INSERT privilege to a table in order to insert into it. If
you use the query clause to insert rows from a query, you also need to
have SELECT privilege on any table used in the query.
Parameters
table
The name (optionally schema-qualified) of an existing table.
dblink
Database link name identifying a remote database. See the
CREATE DATABASE LINKcommand for information on database links.
column
The name of a column in
table.
expression
An expression or value to assign to
column.
DEFAULT
This column will be filled with its default value.
query
A query (
SELECTstatement) that supplies the rows to be inserted. Refer to theSELECTcommand for a description of the syntax.
return_expression
An expression that may include one or more columns from
table. If a column name fromtableis specified inreturn_expression, the value substituted for the column whenreturn_expressionis evaluated is determined as follows:If the column specified in
return_expressionis assigned a value in theINSERTcommand, then the assigned value is used in the evaluation ofreturn_expression.If the column specified in
return_expressionis not assigned a value in theINSERTcommand and there is no default value for the column in the table’s column definition, then null is used in the evaluation ofreturn_expression.If the column specified in
return_expressionis not assigned a value in theINSERTcommand and there is a default value for the column in the table’s column definition, then the default value is used in the evaluation ofreturn_expression.
record
A record whose field the evaluated
return_expressionis to be assigned. The firstreturn_expressionis assigned to the first field inrecord, the secondreturn_expressionis assigned to the second field inrecord, etc. The number of fields inrecordmust exactly match the number of expressions and the fields must be type-compatible with their assigned expressions.
variable
A variable to which the evaluated
return_expressionis to be assigned. If more than onereturn_expressionandvariableare specified, the firstreturn_expressionis assigned to the firstvariable, the secondreturn_expressionis assigned to the secondvariable, etc. The number of variables specified following theINTOkeyword must exactly match the number of expressions following theRETURNINGkeyword and the variables must be type-compatible with their assigned expressions.
collection
A collection in which an element is created from the evaluated
return_expression. There can be either a single collection which may be a collection of a single field or a collection of a record type, or there may be more than one collection in which case each collection must consist of a single field. The number of return expressions must match in number and order the number of fields in all specified collections. Each correspondingreturn_expressionandcollectionfield must be type-compatible.
Examples
Insert a single row into table emp:
INSERT INTO emp VALUES (8021,'JOHN','SALESMAN',7698,'22-FEB-07',1250,500,30);
In this second example, the column, comm, is omitted and therefore it
will have the default value of null:
INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, deptno)
VALUES (8022,'PETERS','CLERK',7698,'03-DEC-06',950,30);
The third example uses the DEFAULT clause for the hiredate and comm
columns rather than specifying a value:
INSERT INTO emp VALUES (8023,'FORD','ANALYST',7566,NULL,3000,NULL,20);
This example creates a table for the department names and then inserts
into the table by selecting from the dname column of the dept table:
CREATE TABLE deptnames (
deptname VARCHAR2(14)
);
INSERT INTO deptnames SELECT dname FROM dept;