Copying table constraints from select * into
-
21-12-2019 - |
Question
Does select * into B from A
also copy constraints of A on B ? If not, then how can I copy constraints?
Solution
No, not in SQL-Server. You would need to specify the constraints and indexes on the new table manually. If you're using SSMS, using the Script As... Create To
functionality can get you a sample script to create a new table with the same constraints and structure.
OTHER TIPS
You can not directly copy the constraint from one table to another table, first you should copy the table Structure with indexes and constraint, to do this
Please follow the instructions below:
In SSMS right click on the table, script create.
Change the name in the generated script to NewTable
insert into NewTable select * from OldTable -- note that it may be slow if the Old is big enough.
It will not copy constraints. If you want two tables to be set up with the same constraints, you have to do it manually by running the create table/constraint statements. You can have sql server create the sql statements from the existing table though. Using Sql Server Studio, in the Object Explorer right click the table and select Script As and select the options you want then change the table name as needed.