Oracle INSERT

Summary: in this tutorial, you will learn how to use the Oracle INSERT statement to insert data into a table.

Introduction to Oracle INSERT statement #

To insert a new row into a table, you use the Oracle INSERT statement.

Here’s the basic syntax of the INSERT statement:

INSERT INTO table_name (column_list)
VALUES( value_list);Code language: SQL (Structured Query Language) (sql)

In this statement:

  • First, provide the name of the table into which you want to insert data.
  • Second, specify a list of comma-separated column names within parentheses.
  • Third, specify a list of comma-separated values that correspond to the column list.

If the value list has the same order as the table columns, you can skip the column list although this is not considered a good practice:

INSERT INTO table_name
VALUES (value_list);Code language: SQL (Structured Query Language) (sql)

If you exclude one or more columns from the Oracle INSERT statement, then you must specify the column list because Oracle needs it to match with values in the value list.

The column that you omit in the INSERT statement will use the default value if available or NULL if the column accepts NULL.

Oracle INSERT statement examples #

First, create a new table called discounts for inserting data:

CREATE TABLE discounts (
  discount_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
  discount_name VARCHAR2 (255) NOT NULL,
  amount NUMBER (3, 1) NOT NULL,
  start_date DATE NOT NULL,
  expired_date DATE NOT NULL
);Code language: SQL (Structured Query Language) (sql)

Try it

In the discounts table, the discount_id column is an identity column whose default value is automatically generated by the system. Therefore, you don’t have to specify the discount_id column in the INSERT statement.

The other columns discount_name, amount, start_date, and expired_dateare the NOT NULL columns, so you must supply the values for them.

Second, insert a new row into the discounts table:

INSERT INTO
  discounts (discount_name, amount, start_date, expired_date)
VALUES
  (
    'Summer Promotion',
    9.5,
    DATE '2017-05-01',
    DATE '2017-08-31'
  );Code language: SQL (Structured Query Language) (sql)

Try it

In this statement, we used the date literals DATE '2017-05-01' and DATE '2017-08-31' for the date columns start_date and expired_date.

Third, retrieve data from the discounts table to verify the insertion:

SELECT
  *
FROM
  discounts;Code language: SQL (Structured Query Language) (sql)

Try it

Oracle INSERT INTO example

Fourth, insert a new row into the discounts table:

INSERT INTO
  discounts (discount_name, amount, start_date, expired_date)
VALUES
  (
    'Winter Promotion 2017',
    10.5,
    CURRENT_DATE,
    DATE '2017-12-31'
  );Code language: SQL (Structured Query Language) (sql)

Try it

In this example, instead of using the date literal, we used the result of the CURRENT_DATE function for the start_date column.

Fifth, retrieve all rows from the discounts table:

SELECT
  *
FROM
  discounts;Code language: SQL (Structured Query Language) (sql)

Try it

Oracle INSERT date example

Inserting multiple rows #

Starting with Oracle 19c, you can use a single standard SQL INSERT statement to insert multiple rows into a table.

If you use Oracle with the version before 19c, you can use the INSERT ALL statement to insert multiple rows into a table.

Here’s the multi-row insert statement:

INSERT INTO table_name (column_list)
VALUES 
   ( value_list_1),
   ( value_list_2),
   ( value_list_3);Code language: SQL (Structured Query Language) (sql)

In this syntax, you specify multiple rows of data to insert in the VALUES clause.

For example:

First, insert three rows into the discounts table:

INSERT INTO
  discounts (discount_name, amount, start_date, expired_date)
VALUES
  (
    'Back to School',
    10.0,
    DATE '2017-08-01',
    DATE '2017-09-15'
  ),
  (
    'Black Friday',
    25.0,
    DATE '2017-11-28',
    DATE '2017-11-28'
  ),
  (
    'Holiday Season',
    15.0,
    DATE '2017-12-15',
    DATE '2017-12-31'
  );Code language: SQL (Structured Query Language) (sql)

Try it

Second, retrieve data from the discounts table:

SELECT * FROM discounts;Code language: SQL (Structured Query Language) (sql)

Try it

Summary #

  • Use the Oracle INSERT statement to insert a new row into a table.
  • Use the multi-insert statement to insert multiple rows into a table.
Was this tutorial helpful?