logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 How to copy table structure

Posted on
2/18/2001
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
All
Product:
N/A



Say you want to copy the structure of the table without copying data. Then, use this simple SQL statement.
SELECT *
INTO my_new_table
FROM my_old_table
WHERE 1=2
This code copies the structure of table my_old_table and creates a new table called my_new_table with identical structure.

Note, that the INTO clause is what is known as a non-logged operation, so your database (Oracle, MS SQL Server, Sybase), although unlikely, may not allow it (depends on the administrator). However, this method always works in Access or on .MDB files.



Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By Geetha. Posted on 2/6/2007 12:36:11 PM
Its was very useful.

#2. By lalitha. Posted on 2/15/2007 5:10:37 AM
It is giving an error as missing keyword at new_table_name.
do i need to cretae a new table before copying

#3. By lalitha. Posted on 2/15/2007 5:16:01 AM
It is giving an error as missing keyword at new_table_name.
do i need to cretae a new table before copying

#4. By Dipan Patel. Posted on 2/19/2007 9:53:48 AM
This is not working..becoz no reference or primary key is generated if parent table has.

#5. By MJ. Posted on 4/13/2007 1:23:11 PM
This works but only to a certain extent.

Constraints and indexs on original table are not created in new table :(

#6. By Vinod. Posted on 5/10/2007 8:46:55 AM
it works .. i wanted to create dummy (staging) table .. this worked fine!

#7. By siva. Posted on 5/26/2007 2:39:00 AM
thanks

#8. By Indriatno. Posted on 7/5/2007 10:57:54 AM
Thanks...!!

#9. By Ed Hitchcock. Posted on 8/2/2007 3:54:56 AM
I wanted to copy a table to get empty table with autonumber set to 1.
That method worked perfectly.

#10. By Rajiv Gandhi. Posted on 8/16/2007 11:26:29 AM
Its Very nice one

#11. By Ganesan.R. Posted on 9/21/2007 7:08:52 AM
Super...

#12. By Sunil Naik. Posted on 11/12/2007 11:49:31 AM
SELECT *
INTO my_new_table
FROM my_old_table
WHERE 1=2

The above command justs creates the strucute. It doesnot create indexs and constraints
please help

#13. By Anonymous. Posted on 11/12/2007 5:38:22 PM
To #12. Indexes and constraints you have to do manually.

#14. By Anonymous. Posted on 1/17/2008 8:03:56 AM
The above string will copy the structure of the original table, but it WILL NOT copy attributes as pimary key, indexes, relationships.

#15. By Yura. Posted on 2/19/2008 3:04:18 PM
This looks like an SQL Server statement.

In oracle it would look like this:

create table my_new_table as
select * from my_old_table where 1=2;

#16. By sandeep. Posted on 2/20/2008 10:20:08 AM
can any one please tell why " WHERE 1=2 "

#17. By Anonymous. Posted on 2/22/2008 8:56:35 PM
To #16. WHERE 1=2 ensures that no rows will be returned, only structure.

#18. By Naod Adnew. Posted on 4/3/2008 9:53:15 AM
I AM A NEW USER FOR YOUR APPLICATION THANKS FOR HELP ME

#19. By Dori Lal. Posted on 4/3/2008 4:54:40 PM
Very Very good friend and instant teacher

#20. By RAMASHANKAR. Posted on 4/11/2008 11:13:24 AM
thank's to giving such explation where 1=2

#21. By Dei. Posted on 5/19/2008 8:58:50 AM
Thanks,simple and it pass.

#22. By Rathessh R. Posted on 7/15/2008 4:00:03 AM
I want to copy the content of table on Access to Word or Exel to avoiding the extra time to type those data. pls help.