Sub topic 2
Adding rows with `INSERT ... VALUES`
Assume you know basic SELECT queries, table definitions, constraints, and transaction commands. This chapter uses the dept_demo and emp_demo tables from this tutorial. emp_demo.status has a default value of ACTIVE.
An INSERT ... VALUES statement creates one new row from values that you provide. The safest form names every target column explicitly:
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (1, 'Ada', 20, 9000);The column list tells Oracle exactly where each value belongs. The values must appear in the same order as the columns.
Use explicit column lists
Avoid relying on the physical order of table columns. A table can change over time, and an omitted column may have a default or allow NULL.
For example, this statement omits status, so Oracle uses its default value:
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (1, 'Ada', 20, 9000);
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary, status)
VALUES (2, 'Grace', 10, 7000, 'INACTIVE');
COMMIT;The first row receives ACTIVE for status. The second row receives the explicitly supplied value INACTIVE.
| Situation | Result |
|---|---|
| Omit a column with a default | Oracle uses the default |
| Omit a nullable column without a default | Oracle stores `NULL` |
| Supply `NULL` explicitly | Oracle stores `NULL`, rather than using the default |
| Omit a `NOT NULL` column without a default | The statement fails |
A string literal uses single quotation marks. A number does not need quotation marks. Use an explicit date conversion, such as DATE '2026-11-28', when inserting a date value instead of depending on session settings.
Check constraints before inserting
Oracle checks primary keys, foreign keys, NOT NULL constraints, and other constraints when it processes the statement. The referenced department must exist before this employee can be inserted:
INSERT INTO dept_demo (dept_id, dept_name)
VALUES (20, 'Engineering');
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (3, 'Lin', 20, 8200);The second statement succeeds because department 20 exists. If it did not, Oracle would raise a foreign-key error.
An INSERT changes your transaction but does not permanently save the change. Use COMMIT after checking the result. Use ROLLBACK if you need to discard uncommitted work.
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_demo
WHERE emp_id = 3;
ROLLBACK;Here, ROLLBACK removes the uncommitted insert. A later COMMIT would make it permanent.
How one row is inserted
Exercise
Exercise
What happens to EMPDEMO.STATUS when this statement omits the column: INSERT INTO empdemo (empid, empname, dept_id, salary) VALUES (4, 'Mina', 10, 7500)?
Show answer
Oracle uses the column's DEFAULT value, so STATUS becomes ACTIVE, assuming the default definition is still in place.