Create duplicate & backup of table in SQL queries

Create duplicate & backup of table in SQL queries

Use of CTAS for creating the Duplicate table with data or only structure (by using where 1 = 0) in four main RDBMS database as follows:

SQL Server

-- Create structure with completed data
Select * into backup_employee from employees;

-- Create only structure of table
select * into backup_table from employees where 1=0;

Oracle

-- Create backup table with data
Create table backup_employee as select * from employees;

-- Create table with only structure
Create table backup_employee as select * from employees where 1=0;

MySQL

-- Create structure only
CREATE TABLE employees_new LIKE employees;

--Insert data into backup table
INSERT employees_new SELECT * FROM employees;

POSTGRESQL

-- create replica of table for backup
CREATE TABLE employees_new AS SELECT * FROM employees;

-- Create only structure
CREATE TABLE employees_new AS SELECT * FROM employees where 1=0;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.