Oracle database tips logo
Article categories | Blog and News Page | SQL | SQL*Plus | DBA Scripts | Standby dbs | Database Links | Techno webcasts | Oracle Magazine | Free IT Magazines | Oracle Database Hosting Companies | Site Search |
RSS
XML RSS
What is this?
AddThis Feed Button

Social Bookmarking


Free Linux Manuals !


A Newbie's Getting Started Guide to Linux



The GNU/Linux Advanced Administration
-- FREE --
IT Magazine
Subscriptions

Oracle Magazine Oracle Magazine Contains technology strategy articles, sample code, tips, Oracle and partner news, how-to articles for developers and DBAs

WebSite Magazine WebSite Magazine Practical advice, helpful tools and insights for website owners

Dr Dobb's Journal Dr Dobb's Journal enables coders to write the most efficient programs and help in daily programming quandaries

DM Review DM Review is recognized as the premier business intelligence, analytics and data warehousing publication
Various other Free IT magazine subscriptions
NoAdware Free Trial

NoAdware Remove
harmful
adware,
spyware,
trojans,
dialers
and worms!
- Featured ebook -

Database Normalization
by Alf Pedersen

Database Normalization ebook Understand and master how to normalize a database using methods richly documented with graphical ERD and server diagram examples

Oracle Insert
Examples and syntax explanation



Always test these 'oracle insert' commands on a test server
before implementing them on a production server.

The Oracle Insert statement allows you to add data to a table.

Contents :

The syntax is : insert into <table_name> ( <column list>) values (<values list>);

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.

Sample table for use in the Oracle Insert syntax tutorial:


SQL> select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON


No column list

When you do not specify the column list, you can use the values clause immediately after
the table clause, but Oracle will expect the values to match the column datatypes,
so you must make sure that the values are in the same order as the table's columns.

To see what the order of a table's columns is,
use the 'DESCRIBE' statement or its abbreviation 'desc' :


SQL> describe dept
 Name                   Null?    Type
 ---------------------- -------- ---------------

 DEPTNO                          NUMBER(2)
 DNAME                           VARCHAR2(14)
 LOC                             VARCHAR2(13)




SQL> insert into dept values 
(50,'MARKETING','MELBOURNE'); 1 row created. SQL> commit; Commit complete. SQL> 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


Using a column list


SQL> insert into dept (deptno,dname,loc) 
values (60,'IT','HARTFORD'); 1 row created. SQL> commit; Commit complete. SQL> 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.


Using a column list allows you to insert values for selected columns only:


SQL> insert into dept (deptno,dname) 
values (70,'CALLCENTER'); 1 row created. SQL> commit; Commit complete. SQL> 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.


Default values


You can choose to insert a column's default value like this:
Let's first change the DEPT table to have a default value of 'NONE' for the location column:


SQL> alter table dept modify (loc default 'NONE');

Table altered.

SQL> insert into dept (deptno,dname,loc) 
values (80,'SUPPORT',DEFAULT); 1 row created. SQL> commit; Commit complete. SQL> 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.


Inserting a null value

Use the 'null' identifier to insert a null value instead of a real value.


SQL> insert into dept (deptno,dname,loc) 
values (90,NULL,'BOISE'); 1 row created. SQL> commit; Commit complete. SQL> 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.


Inserting multiple rows by means of a subquery

Instead of adding one row at a time, here is an example of inserting a number of rows by using a subquery:

We have another table called DEPT_MERGE with the same table structure as DEPT and with rows which need to be added to DEPT.


SQL> select * from dept_merge order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        91 SHIPPING       DETROIT
        92 FINANCE        DETROIT
        
SQL> insert into dept
  2  (select * from dept_merge);

2 rows created.

SQL> commit;

Commit complete.


SQL> 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.        



Lastly: do not forget to commit to make your changes permanent after you use the Oracle Insert statement.

Recommended reading:


  • Oracle Online Documentation: SQL Reference, Insert statement


  • Return from 'oracle insert' back to 'oracle sql tutorials'