DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Creating a backup table in ORACLE SQL

In Oracle SQL, creating a backup table is typically done by copying the structure and data of the original table into a new table. You can use the CREATE TABLE AS SELECT (CTAS) statement to create a backup of the table.

Syntax:

CREATE TABLE backup_table AS
SELECT *
FROM original_table;

This statement creates a new table (backup_table) with the same data as the original table but without any constraints (like primary keys, indexes, foreign keys, etc.).

Example:

Assume you have a table called employees, and you want to create a backup of this table.

CREATE TABLE employees_backup AS
SELECT *
FROM employees;

This will create a table employees_backup with the same structure and data as the employees table.

If you want to copy just the structure (without data):

You can use the WHERE clause with a condition that always evaluates to false.

CREATE TABLE employees_backup AS
SELECT *
FROM employees
WHERE 1=0;

This will create the employees_backup table with the same structure as employees but without any rows.

Additional Steps:

If you want to back up constraints (indexes, foreign keys, etc.), you will need to manually recreate them after creating the backup table.

-- Example of adding a primary key to the backup table
ALTER TABLE employees_backup ADD CONSTRAINT pk_emp_backup PRIMARY KEY (employee_id);

You can also export the table's structure and data using Oracle's export utilities if needed.

Top comments (0)