This Oracle tutorial explains how to use the Oracle INSERT statement with syntax and examples. We've also added some practice exercises that you can try for yourself.
The Oracle INSERT statement is used to insert a single record or multiple records into a table in Oracle.
The syntax for the Oracle INSERT statement when inserting a single record using the VALUES keyword is:
Or the syntax for the Oracle INSERT statement when inserting multiple records using a SELECT statement is:
The table to insert the records into.
The columns in the table to insert values.
The values to assign to the columns in the table. So column1 would be assigned the value of expression1, column2 would be assigned the value of expression2, and so on.
The source table when inserting data from another table.
Optional. The conditions that must be met for the records to be inserted.
The simplest way to create an Oracle INSERT query to list the values using the VALUES keyword.
For example:
This Oracle INSERT statement would result in one record being inserted into the suppliers table. This new record would have a supplier_id of 5000 and a supplier_name of 'Apple'.
You can also create more complicated Oracle INSERT statements using SELECT statements.
For example:
By placing a SELECT statement within the INSERT statement, you can perform multiples inserts quickly.
With this type of insert, you may wish to check for the number of rows being inserted. You can determine the number of rows that will be inserted by running the following Oracle SELECT statement before performing the insert.
Question: I am setting up a database with clients. I know that you use the Oracle INSERT statement to insert information in the database, but how do I make sure that I do not enter the same client information again?
Answer: You can make sure that you do not insert duplicate information by using the EXISTS condition.
For example, if you had a table named clients with a primary key of client_id, you could use the following Oracle INSERT statement:
This Oracle INSERT statement inserts multiple records with a subselect.
If you wanted to insert a single record, you could use the following Oracle INSERT statement:
The use of the dual table allows you to enter your values in a select statement, even though the values are not currently stored in a table.
Question: How can I insert multiple rows of explicit data in one INSERT command in Oracle?
Answer: The following is an example of how you might insert 3 rows into the suppliers table in Oracle, using an Oracle INSERT statement:
Based on the contacts table, insert a contact record whose contact_id is 1000, last_name is Smith, first_name is Jane, and address is 10 Somewhere St.:
The following Oracle INSERT statement would insert this record into the employees table:
Based on the contacts and customers table, insert into the contacts table all customers who reside in the state of 'Florida'.
The following Oracle INSERT statement would insert this record into the suppliers table:
Since the number of fields in the contacts and customers table are the same and the fields are listed in the same order, you could write the solution as follows (though it is generally better practice to list the column names in case the table definitions change):