<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Oracle Database Tips blog &#187; sql insert</title>
	<atom:link href="http://oracle-database-tips.com/wp/tag/sql-insert/feed" rel="self" type="application/rss+xml" />
	<link>http://oracle-database-tips.com/wp</link>
	<description>News for Oracle professionals</description>
	<lastBuildDate>Fri, 11 May 2012 12:12:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Oracle Insert</title>
		<link>http://oracle-database-tips.com/wp/sql/oracle-insert</link>
		<comments>http://oracle-database-tips.com/wp/sql/oracle-insert#comments</comments>
		<pubDate>Fri, 23 Oct 2009 11:00:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[abbreviation]]></category>
		<category><![CDATA[column names]]></category>
		<category><![CDATA[desc]]></category>
		<category><![CDATA[insert syntax]]></category>
		<category><![CDATA[oracle insert]]></category>
		<category><![CDATA[sql insert]]></category>
		<category><![CDATA[subquery]]></category>
		<category><![CDATA[table contents]]></category>
		<category><![CDATA[varchar2]]></category>

		<guid isPermaLink="false">http://oracle-database-tips.com/wp/?p=68</guid>
		<description><![CDATA[The Oracle Insert statement allows you to add data to a table using the following syntax:]]></description>
			<content:encoded><![CDATA[<p><strong>Examples and explanation</strong></p>
<p>The Oracle Insert statement allows you to add data to a table.</p>
<p>Contents :</p>
<ul>
<li> <a href="#nocolumn"> Inserting without specifying column names</a></li>
<li><a href="#column"> Inserting by specifying column names</a></li>
<li> <a href="#default"> Insert a column&#8217;s default value</a></li>
<li> <a href="#null"> Inserting a null value</a></li>
<li> <a href="#subquery"> Inserting multiple rows by means of a subquery</a></li>
</ul>
<p>The syntax is : insert into &lt;table_name&gt; ( &lt;column list&gt;)          values (&lt;values list&gt;);</p>
<p>You do not have to specify the column names if your value list is in          the same order as the columns of the table, but it is best to always specify          the column names.</p>
<p><strong>Sample table for use in the Oracle Insert syntax tutorial:</strong><br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><a id="nocolumn" name="nocolumn"></a><strong>No column list</strong></p>
<p>When you do not specify the column list, you can use the <em>values</em> clause immediately after<br />
the <em>table</em> clause, but Oracle will expect the values to match the column datatypes,<br />
so you must make sure that the values are in the same order as the table&#8217;s columns.</p>
<p>To see what the order of a table&#8217;s columns is,<br />
use the &#8216;DESCRIBE&#8217; statement or its abbreviation &#8216;desc&#8217; :<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; describe dept
 Name                   Null?    Type
 ---------------------- -------- ---------------

 DEPTNO                          NUMBER(2)
 DNAME                           VARCHAR2(14)
 LOC                             VARCHAR2(13)</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; insert into dept values
     (50,'MARKETING','MELBOURNE');

1 row created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><a id="column" name="column"></a><strong>Using a column list </strong><br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; insert into dept (deptno,dname,loc)
     values (60,'IT','HARTFORD');

1 row created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTFORD

6 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   -->Using a column list allows you to insert values for selected columns only:<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; insert into dept (deptno,dname)
     values (70,'CALLCENTER');

1 row created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTER

7 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <a id="default" name="default"></a><strong>Default values</strong><br />
You can choose to insert a column&#8217;s default value like this:<br />
Let&#8217;s first change the DEPT table to have a default value of &#8216;NONE&#8217; for          the location column:<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; alter table dept modify (loc default 'NONE');

Table altered.

SQL&gt; insert into dept (deptno,dname,loc)
     values (80,'SUPPORT',DEFAULT);

1 row created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTER
        80 SUPPORT        NONE

8 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <a id="null" name="null"></a><strong>Inserting a null value</strong></p>
<p>Use the &#8216;null&#8217; identifier to insert a null value instead of a real value.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; insert into dept (deptno,dname,loc)
     values (90,NULL,'BOISE');

1 row created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTER
        80 SUPPORT        NONE
        90                BOISE

9 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <a id="subquery" name="subquery"></a><strong>Inserting multiple rows by means of a subquery</strong></p>
<p>Instead of adding one row at a time, here is an example of inserting          a number of rows by using a subquery:</p>
<p>We have another table called DEPT_MERGE with the same table structure          as DEPT and with rows which need to be added to DEPT.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept_merge order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        91 SHIPPING       DETROIT
        92 FINANCE        DETROIT

SQL&gt; insert into dept
  2  (select * from dept_merge);

2 rows created.

SQL&gt; commit;

Commit complete.

SQL&gt; select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTER
        80 SUPPORT        NONE
        90                BOISE
        91 SHIPPING       DETROIT
        92 FINANCE        DETROIT

11 rows selected.</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
Lastly: do not forget to commit to make your changes permanent after you use the Oracle Insert statement.</p>
<p><!-- ============================================================================================================================================================ --></p>
<h3>Recommended reading:</h3>
<div id="content">
<li><a title="opens new window" onclick="window.open('http://www.oracle-database-tips.com/cgi-bin/counter.pl?url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FB19306_01%2Fserver.102%2Fb14200%2Fstatements_9014.htm%23i2163698&amp;referrer=http%3A%2F%2Fwww.oracle-database-tips.com%2Foracle_insert.html'); return false;" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9014.htm#i2163698" target="_blank">Oracle Online Documentation: SQL Reference, Insert statement</a></li>
</div>
]]></content:encoded>
			<wfw:commentRss>http://oracle-database-tips.com/wp/sql/oracle-insert/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

