Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Insert Values Into Table SQL Oracle: Step-by-Step

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert values into a table in Oracle SQL, use the INSERT INTO statement: INSERT INTO table_name (column1, column2) VALUES (value1, value2);. This adds one row. For multiple rows, use INSERT ALL or a SELECT statement. Before you start, you need the table to exist, you need to know its column names and data types, and you need the right privileges (at minimum, INSERT on the table and SELECT on any tables you read from). You also need a SQL client such as SQL*Plus, SQL Developer, or any tool that connects to Oracle Database. The steps below assume you are connected to the database as a user with those privileges.

What You'll Need

  • Access to an Oracle Database instance (any recent version).
  • A SQL client: SQL*Plus, SQL Developer, or a similar tool.
  • The name of the target table and its column definitions (names, data types, nullability, defaults).
  • Privileges: INSERT on the table, and SELECT on any table you query for values.
  • Knowledge of the data you want to insert, matching the column data types (for example, dates in the correct format, strings quoted with single quotes).

Step-by-Step

  1. Connect to the database. Open your SQL client and connect with your username, password, and the service name or SID. In SQL*Plus you might run sqlplus username/password@service; in SQL Developer you create a connection and click Connect. Once connected, you can run queries.
  2. Inspect the table structure. Run DESCRIBE table_name; (or DESC table_name;) to list columns, data types, and whether nulls are allowed. This tells you which columns are mandatory and what kind of values they accept. For example, a NUMBER column will not accept a string unless it can be implicitly converted.
  3. Write a single-row INSERT. The safest form lists the columns explicitly: INSERT INTO employees (employee_id, first_name, last_name, hire_date) VALUES (101, 'Jane', 'Doe', DATE '2024-01-15');. Listing columns avoids problems if the table has columns you do not want to fill or if the column order changes. Use single quotes for string and date literals; for dates, either use the DATE literal syntax shown or TO_DATE('15-JAN-2024','DD-MON-YYYY').
  4. Run the statement. Execute it in your client. In SQL*Plus, end the statement with a semicolon and press Enter. In SQL Developer, press F9 (Run Statement) or Ctrl+Enter. You should see a confirmation such as 1 row inserted..
  5. Commit the change. By default, Oracle does not auto-commit. Your insert is visible only in your session until you run COMMIT;. If you are using SQL*Plus with AUTOCOMMIT ON, it commits automatically, but otherwise you must commit. Until you commit, other sessions do not see the new row, and you can undo with ROLLBACK;.
  6. Verify the row. Run a SELECT to check: SELECT * FROM employees WHERE employee_id = 101;. Confirm the values are correct and that any defaults or triggers produced the expected results.
  7. Insert multiple rows. Oracle offers several ways. The INSERT ALL statement inserts multiple rows in one command: INSERT ALL INTO employees (employee_id, first_name, last_name) VALUES (102, 'John', 'Smith') INTO employees (employee_id, first_name, last_name) VALUES (103, 'Mary', 'Jones') SELECT 1 FROM dual;. Alternatively, you can run several single-row INSERT statements in a script, or use INSERT INTO ... SELECT ... to copy rows from another table or a set of literals: INSERT INTO employees (employee_id, first_name, last_name) SELECT 104, 'Alice', 'Brown' FROM dual;.
  8. Handle special cases. If a column has a default value and you omit it from the column list, Oracle uses the default. If a column is an identity column (generated always), you cannot insert into it unless you use OVERRIDING SYSTEM VALUE (available in newer Oracle versions). For nulls, either omit the column or specify NULL. For strings containing a single quote, escape it by doubling: 'O''Brien'.
  9. Commit again after multiple inserts. If you inserted several rows, commit once at the end to make all changes permanent. If something looks wrong, ROLLBACK; undoes all uncommitted changes in the current transaction.

Common Mistakes

  • Forgetting to commit. The data appears in your session but not to others, and it is lost if you disconnect without committing. Always run COMMIT; when you are satisfied.
  • Column count mismatch. If you omit the column list, you must supply a value for every column in the table's order, including nulls. Listing columns avoids this.
  • Data type errors. Inserting a string into a NUMBER column or a malformed date string causes ORA-01722: invalid number or ORA-01861: literal does not match format string. Use explicit conversions like TO_DATE or TO_NUMBER.
  • String quoting. Using double quotes for string literals (they are for identifiers) or forgetting to escape single quotes leads to errors.
  • Violating constraints. Inserting a duplicate value into a primary key or unique column, or a null into a NOT NULL column, raises errors like ORA-00001: unique constraint violated or ORA-01400: cannot insert NULL.

Tips

  • Always list columns explicitly in INSERT statements; it makes your code robust to table changes.
  • Use bind variables in application code (for example, INSERT INTO t (c1) VALUES (:1)) to improve performance and avoid SQL injection.
  • Test your insert in a development environment first, especially when using triggers or foreign keys.
  • For large data loads, consider SQL*Loader or external tables instead of many individual inserts.
  • Check the table's triggers and constraints with SELECT * FROM USER_TRIGGERS WHERE TABLE_NAME = 'YOUR_TABLE'; to understand side effects.

Troubleshooting

If the insert fails, read the error message carefully. ORA-00904: invalid identifier means a column name is misspelled or does not exist. ORA-00947: not enough values means the number of values does not match the number of columns. ORA-12899: value too large for column means a string exceeds the column's length; shorten it or alter the column. ORA-02291: integrity constraint violated - parent key not found means a foreign key references a value that does not exist in the parent table; insert the parent row first. If you cannot commit because of a constraint, fix the data and retry. If you are unsure what went wrong, run ROLLBACK; to discard the failed transaction and start over. For permission errors like ORA-01031: insufficient privileges, ask your DBA to grant INSERT on the table. If the table is locked by another session, you may need to wait or ask the DBA to resolve it.

Frequently Asked Questions

Can I insert multiple rows in one statement?

Yes. Use INSERT ALL with multiple INTO clauses followed by a SELECT from dual, or use INSERT INTO ... SELECT ... with UNION ALL to combine rows.

Do I have to commit after every insert?

No. You can insert many rows and commit once. However, uncommitted changes hold locks and consume undo space, so commit at logical points.

How do I insert the current date and time?

Use SYSDATE for the current date and time, or CURRENT_DATE for the session time zone. For example: INSERT INTO t (created) VALUES (SYSDATE);.

What if I don't know the column names?

Run DESCRIBE table_name; or query USER_TAB_COLUMNS to list them. You can also omit the column list and supply values for all columns in order, but that is risky.

Can I insert into a view?

Only if the view is updatable (for example, based on a single table without certain constructs). Otherwise, you must insert into the underlying table.

How do I insert a null value?

Either omit the column from the column list (if it allows nulls and has no default) or specify NULL explicitly: INSERT INTO t (c1, c2) VALUES (1, NULL);.