Previous |
Next |
The INSERT
statement inserts rows into an existing table.
The simplest recommended form of the INSERT
statement has this syntax:
INSERT INTO table_name (list_of_columns) VALUES (list_of_values);
Every column in list_of_columns
must have a valid value in the corresponding position in list_of_values
. Therefore, before you insert a row into a table, you must know what columns the table has, and what their valid values are. To get this information using SQL Developer, see Tutorial: Viewing EMPLOYEES Table Properties and Data. To get this information using SQL*Plus, use this statement:
DESCRIBE table_name;
The INSERT
statement in the following example inserts a row into the EMPLOYEES
table for an employee for which all column values are known.
Using the INSERT Statement When All Information Is Available
INSERT INTO EMPLOYEES ( EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID ) VALUES ( 10, -- EMPLOYEE_ID 'George', -- FIRST_NAME 'Gordon', -- LAST_NAME 'GGORDON', -- EMAIL '650.506.2222', -- PHONE_NUMBER '01-JAN-07', -- HIRE_DATE 'SA_REP', -- JOB_ID 9000, -- SALARY .1, -- COMMISSION_PCT 148, -- MANAGER_ID 80 -- DEPARTMENT_ID );
Result:
1 row created.
You do not need to know all column values to insert a row into a table, but you must know the values of all NOT
NULL
columns. If you do not know the value of a column that can be NULL
, you can omit that column from list_of_columns
. Its value defaults to NULL
.
The INSERT
statement in the following example inserts a row into the EMPLOYEES
table for an employee for which all column values are known except SALARY
. For now, SALARY
can have the value NULL
. When you know the salary, you can change it with the UPDATE
statement (see About the UPDATE Statement).
Using the INSERT Statement When Not All Information Is Available
INSERT INTO EMPLOYEES (
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID, -- Omit SALARY; its value defaults to NULL.
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID
)
VALUES (
20, -- EMPLOYEE_ID
'John', -- FIRST_NAME
'Keats', -- LAST_NAME
'JKEATS', -- EMAIL
'650.506.3333', -- PHONE_NUMBER
'01-JAN-07', -- HIRE_DATE
'SA_REP', -- JOB_ID
.1, -- COMMISSION_PCT
148, -- MANAGER_ID
80 -- DEPARTMENT_ID
);
Result:
1 row created.
The INSERT
statement in the following example tries to insert a row into the EMPLOYEES
table for an employee for which LAST_NAME
is not known.
Using the INSERT Statement Incorrectly
INSERT INTO EMPLOYEES (
EMPLOYEE_ID,
FIRST_NAME, -- Omit LAST_NAME (error)
EMAIL,
PHONE_NUMBER,
HIRE_DATE,
JOB_ID,
COMMISSION_PCT,
MANAGER_ID,
DEPARTMENT_ID
)
VALUES (
20, -- EMPLOYEE_ID
'John', -- FIRST_NAME
'JOHN', -- EMAIL
'650.506.3333', -- PHONE_NUMBER
'01-JAN-07', -- HIRE_DATE
'SA_REP', -- JOB_ID
.1, -- COMMISSION_PCT
148, -- MANAGER_ID
80 -- DEPARTMENT_ID
);
Result:
ORA-01400: cannot insert NULL into ("HR"."EMPLOYEES"."LAST_NAME")
Oracle Database SQL Language Reference for information about the INSERT
statement
Oracle Database SQL Language Reference for information about data types