<s>
An	O
SQL	B-Language
INSERT	I-Language
statement	O
adds	O
one	O
or	O
more	O
records	O
to	O
any	O
single	O
table	B-Application
in	O
a	O
relational	B-Application
database	I-Application
.	O
</s>
<s>
Insert	B-Language
statements	O
have	O
the	O
following	O
form	O
:	O
</s>
<s>
The	O
number	O
of	O
columns	B-Application
and	O
values	O
must	O
be	O
the	O
same	O
.	O
</s>
<s>
The	O
values	O
specified	O
(	O
or	O
implied	O
)	O
by	O
the	O
statement	O
must	O
satisfy	O
all	O
the	O
applicable	O
constraints	O
(	O
such	O
as	O
primary	B-Application
keys	I-Application
,	O
constraints	O
,	O
and	O
constraints	O
)	O
.	O
</s>
<s>
If	O
a	O
syntax	O
error	O
occurs	O
or	O
if	O
any	O
constraints	O
are	O
violated	O
,	O
the	O
new	O
row	O
is	O
not	O
added	O
to	O
the	O
table	B-Application
and	O
an	O
error	O
returned	O
instead	O
.	O
</s>
<s>
Shorthand	O
may	O
also	O
be	O
used	O
,	O
taking	O
advantage	O
of	O
the	O
order	O
of	O
the	O
columns	B-Application
when	O
the	O
table	B-Application
was	O
created	O
.	O
</s>
<s>
It	O
is	O
not	O
required	O
to	O
specify	O
all	O
columns	B-Application
in	O
the	O
table	B-Application
since	O
any	O
other	O
columns	B-Application
will	O
take	O
their	O
default	O
value	O
or	O
remain	O
null	B-Language
:	O
</s>
<s>
Example	O
for	O
inserting	O
data	O
into	O
2	O
columns	B-Application
in	O
the	O
phone_book	O
table	B-Application
and	O
ignoring	O
any	O
other	O
columns	B-Application
which	O
may	O
be	O
after	O
the	O
first	O
2	O
in	O
the	O
table	B-Application
.	O
</s>
<s>
A	O
SQL	B-Language
feature	O
(	O
since	O
SQL-92	B-Language
)	O
is	O
the	O
use	O
of	O
row	O
value	O
constructors	O
to	O
insert	B-Language
multiple	O
rows	O
at	O
a	O
time	O
in	O
a	O
single	O
SQL	B-Language
statement	O
:	O
</s>
<s>
This	O
feature	O
is	O
supported	O
by	O
IBM	B-Application
Db2	I-Application
,	O
SQL	B-Application
Server	I-Application
(	O
since	O
version	O
10.0	O
-	O
i.e.	O
</s>
<s>
2008	O
)	O
,	O
PostgreSQL	B-Application
(	O
since	O
version	O
8.2	O
)	O
,	O
MySQL	B-Application
,	O
SQLite	B-Language
(	O
since	O
version	O
3.7.11	O
)	O
and	O
H2	B-Language
.	O
</s>
<s>
Example	O
(	O
assuming	O
that	O
'	O
name	O
 '	O
and	O
'	O
number	O
 '	O
are	O
the	O
only	O
columns	B-Application
in	O
the	O
'	O
phone_book	O
 '	O
table	B-Application
)	O
:	O
</s>
<s>
Note	O
that	O
the	O
two	O
separate	O
statements	O
may	O
have	O
different	O
semantics	O
(	O
especially	O
with	O
respect	O
to	O
statement	O
triggers	B-Language
)	O
and	O
may	O
not	O
provide	O
the	O
same	O
performance	O
as	O
a	O
single	O
multi-row	O
insert	B-Language
.	O
</s>
<s>
To	O
insert	B-Language
multiple	O
rows	O
in	O
MS	B-Application
SQL	I-Application
you	O
can	O
use	O
such	O
a	O
construction	O
:	O
</s>
<s>
Note	O
that	O
this	O
is	O
not	O
a	O
valid	O
SQL	B-Language
statement	O
according	O
to	O
the	O
SQL	B-Language
standard	O
(	O
SQL:2003	B-Language
)	O
due	O
to	O
the	O
incomplete	O
subselect	O
clause	O
.	O
</s>
<s>
To	O
do	O
the	O
same	O
in	O
Oracle	B-General_Concept
use	O
the	O
DUAL	B-Application
table	I-Application
,	O
which	O
always	O
consists	O
of	O
a	O
single	O
row	O
only	O
:	O
</s>
<s>
Oracle	B-General_Concept
PL/SQL	O
supports	O
the	O
statement	O
,	O
where	O
multiple	O
insert	B-Language
statements	O
are	O
terminated	O
by	O
a	O
:	O
</s>
<s>
In	O
Firebird	B-Application
inserting	O
multiple	O
rows	O
can	O
be	O
achieved	O
like	O
this	O
:	O
</s>
<s>
Firebird	B-Application
,	O
however	O
,	O
restricts	O
the	O
number	O
of	O
rows	O
than	O
can	O
be	O
inserted	O
in	O
this	O
way	O
,	O
since	O
there	O
is	O
a	O
limit	O
to	O
the	O
number	O
of	O
contexts	O
that	O
can	O
be	O
used	O
in	O
a	O
single	O
query	O
.	O
</s>
<s>
An	O
statement	O
can	O
also	O
be	O
used	O
to	O
retrieve	O
data	O
from	O
other	O
tables	O
,	O
modify	O
it	O
if	O
necessary	O
and	O
insert	B-Language
it	O
directly	O
into	O
the	O
table	B-Application
.	O
</s>
<s>
All	O
this	O
is	O
done	O
in	O
a	O
single	O
SQL	B-Language
statement	O
that	O
does	O
not	O
involve	O
any	O
intermediary	O
processing	O
in	O
the	O
client	O
application	O
.	O
</s>
<s>
The	O
subselect	O
can	O
contain	O
joins	O
,	O
function	O
calls	O
,	O
and	O
it	O
can	O
even	O
query	O
the	O
same	O
table	B-Application
into	O
which	O
the	O
data	O
is	O
inserted	O
.	O
</s>
<s>
Logically	O
,	O
the	O
select	B-Language
is	O
evaluated	O
before	O
the	O
actual	O
insert	B-Language
operation	O
is	O
started	O
.	O
</s>
<s>
A	O
variation	O
is	O
needed	O
when	O
some	O
of	O
the	O
data	O
from	O
the	O
source	O
table	B-Application
is	O
being	O
inserted	O
into	O
the	O
new	O
table	B-Application
,	O
but	O
not	O
the	O
whole	O
record	O
.	O
</s>
<s>
(	O
Or	O
when	O
the	O
tables	O
 '	O
schemas	B-Application
are	O
not	O
the	O
same	O
.	O
)	O
</s>
<s>
The	O
statement	O
produces	O
a	O
(	O
temporary	O
)	O
table	B-Application
,	O
and	O
the	O
schema	B-Application
of	O
that	O
temporary	O
table	B-Application
must	O
match	O
with	O
the	O
schema	B-Application
of	O
the	O
table	B-Application
where	O
the	O
data	O
is	O
inserted	O
into	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
insert	B-Language
a	O
new	O
row	O
without	O
specifying	O
any	O
data	O
,	O
using	O
default	O
values	O
for	O
all	O
columns	B-Application
.	O
</s>
<s>
However	O
,	O
some	O
databases	O
reject	O
the	O
statement	O
if	O
no	O
data	O
is	O
given	O
,	O
such	O
as	O
Microsoft	B-Application
SQL	I-Application
Server	I-Application
,	O
and	O
in	O
this	O
case	O
the	O
keyword	O
can	O
be	O
used	O
.	O
</s>
<s>
Sometimes	O
databases	O
also	O
support	O
alternative	O
syntax	O
for	O
this	O
;	O
for	O
example	O
,	O
MySQL	B-Application
allows	O
omitting	O
the	O
keyword	O
,	O
and	O
T-SQL	O
can	O
use	O
instead	O
of	O
.	O
</s>
<s>
For	O
example	O
,	O
MySQL	B-Application
and	O
SQLite	B-Language
will	O
fill	O
in	O
with	O
a	O
blank	O
value	O
(	O
except	O
when	O
in	O
strict	O
mode	O
)	O
,	O
while	O
many	O
other	O
databases	O
will	O
reject	O
the	O
statement	O
.	O
</s>
<s>
Database	O
designers	O
that	O
use	O
a	O
surrogate	B-Application
key	I-Application
as	O
the	O
primary	B-Application
key	I-Application
for	O
every	O
table	B-Application
will	O
run	O
into	O
the	O
occasional	O
scenario	O
where	O
they	O
need	O
to	O
automatically	O
retrieve	O
the	O
database-generated	O
primary	B-Application
key	I-Application
from	O
an	O
SQL	B-Language
statement	O
for	O
use	O
in	O
other	O
SQL	B-Language
statements	O
.	O
</s>
<s>
Most	O
systems	O
do	O
not	O
allow	O
SQL	B-Language
statements	O
to	O
return	O
row	O
data	O
.	O
</s>
<s>
Using	O
a	O
database-specific	O
stored	B-General_Concept
procedure	I-General_Concept
that	O
generates	O
the	O
surrogate	B-Application
key	I-Application
,	O
performs	O
the	O
operation	O
,	O
and	O
finally	O
returns	O
the	O
generated	O
key	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Microsoft	B-Application
SQL	I-Application
Server	I-Application
,	O
the	O
key	O
is	O
retrieved	O
via	O
the	O
special	O
function	O
,	O
while	O
in	O
SQLite	B-Language
the	O
function	O
is	O
named	O
.	O
</s>
<s>
Using	O
a	O
database-specific	O
statement	O
on	O
a	O
temporary	O
table	B-Application
containing	O
last	O
inserted	O
row(s )	O
.	O
</s>
<s>
Db2	B-Application
implements	O
this	O
feature	O
in	O
the	O
following	O
way	O
:	O
</s>
<s>
Db2	B-Application
for	O
z/OS	B-Application
implements	O
this	O
feature	O
in	O
the	O
following	O
way	O
.	O
</s>
<s>
Using	O
a	O
statement	O
after	O
the	O
statement	O
with	O
a	O
database-specific	O
function	O
that	O
returns	O
the	O
generated	O
primary	B-Application
key	I-Application
for	O
the	O
most	O
recently	O
inserted	O
row	O
.	O
</s>
<s>
For	O
example	O
,	O
for	O
MySQL	B-Application
.	O
</s>
<s>
Using	O
a	O
unique	O
combination	O
of	O
elements	O
from	O
the	O
original	O
SQL	B-Language
in	O
a	O
subsequent	O
statement	O
.	O
</s>
<s>
Using	O
a	O
GUID	O
in	O
the	O
SQL	B-Language
statement	O
and	O
retrieving	O
it	O
in	O
a	O
statement	O
.	O
</s>
<s>
Using	O
the	O
clause	O
in	O
the	O
SQL	B-Language
statement	O
for	O
MS-SQL	B-Application
Server	O
2005	O
and	O
MS-SQL	B-Application
Server	O
2008	O
.	O
</s>
<s>
Using	O
an	O
statement	O
with	O
clause	O
for	O
Oracle	B-General_Concept
.	O
</s>
<s>
Using	O
an	O
statement	O
with	O
clause	O
for	O
PostgreSQL	B-Application
(	O
since	O
8.2	O
)	O
.	O
</s>
<s>
Firebird	B-Application
has	O
the	O
same	O
syntax	O
in	O
Data	O
Modification	O
Language	O
statements	O
(	O
DSQL	O
)	O
;	O
the	O
statement	O
may	O
add	O
at	O
most	O
one	O
row	O
.	O
</s>
<s>
In	O
stored	B-General_Concept
procedures	I-General_Concept
,	O
triggers	B-Language
and	O
execution	O
blocks	O
(	O
PSQL	O
)	O
the	O
aforementioned	O
Oracle	B-General_Concept
syntax	O
is	O
used	O
.	O
</s>
<s>
Using	O
the	O
function	O
in	O
H2	B-Language
returns	O
the	O
last	O
identity	O
inserted	O
.	O
</s>
<s>
If	O
triggers	B-Language
are	O
defined	O
on	O
the	O
table	B-Application
on	O
which	O
the	O
statement	O
operates	O
,	O
those	O
triggers	B-Language
are	O
evaluated	O
in	O
the	O
context	O
of	O
the	O
operation	O
.	O
</s>
<s>
triggers	B-Language
allow	O
the	O
modification	O
of	O
the	O
values	O
that	O
shall	O
be	O
inserted	O
into	O
the	O
table	B-Application
.	O
</s>
<s>
triggers	B-Language
cannot	O
modify	O
the	O
data	O
anymore	O
,	O
but	O
can	O
be	O
used	O
to	O
initiate	O
actions	O
on	O
other	O
tables	O
,	O
for	O
example	O
,	O
to	O
implement	O
auditing	O
mechanism	O
.	O
</s>
