Copying table constraints from select * into

Reeya Oberoi picture Reeya Oberoi · Jul 30, 2014 · Viewed 8.1k times · Source

Does select * into B from A also copy constraints of A on B ? If not, then how can I copy constraints?

Answer

Chris Tramel picture Chris Tramel · Jul 30, 2014

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.

https://docs.microsoft.com/en-us/sql/t-sql/queries/select-into-clause-transact-sql?view=sql-server-ver15

Indexes, constraints, and triggers defined in the source table are not transferred to the new table, nor can they be specified in the SELECT...INTO statement. If these objects are required, you can create them after executing the SELECT...INTO statement.