Thursday, December 8, 2016

Create new database table from existing one

Create new database table from existing one

For example you want to create a duplicate table called "NewEmployees" of an existing table called "Employees". Use the following script:

--syntax--
CREATE TABLE newTable
AS 
(SELECT * FROM oldTable);
 
example script:
CREATE TABLE NewEmployees
AS
(SELECT * FROM Employees)
 
Note: you may specify exact fields names instead of *. Here * denotes to all fields.

There are chances to get an error in SELECT statement if you have any primary key 
auto incremental field in the existing table. In such situation you may go for
another script (given here under)

--syntax--
SELECT * INTO newTable
FROM oldTable 
 
example script: 
SELECT * INTO NewEmployees
FROM Employees



No comments:

Post a Comment