COPYING TABLE STRUCTURE

  • I know how to create a structure of a table from another table without copying data but how do you create a structure of a table with all constraints from another table?

    To Create NEW Table structure without copying data but no constraints:

    Select * into New.Table from Old.Table where 1 =2

    Want to know how do you create a table structure from another table with all constraints?

    Any answer would be appreciated.

    Thanks

  • In Management Studio, right-click the table name, click Script Table as -> CREATE to -> New Query Editor window.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • In SSMS you can right-click the table and and use script as create and it includes all the contraints, indexes, etc...

  • Thanks for the quick reply.

  • GSquared (3/17/2008)


    In Management Studio, right-click the table name, click Script Table as -> CREATE to -> New Query Editor window.

    Darn, you beat me by seconds!:crying:

  • Jack Corbett (3/17/2008)


    In SSMS you can right-click the table and and use script as create and it includes all the contraints, indexes, etc...

    Actually, it doesn't include indexes (except the primary key and/or clustered index). You have to script those separately.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I don't have sql on my laptop so I could not look and I haven't done that in a while.

    You could also right click on the database - all tasks - generate scripts and just select the table you want and include the indexes.

  • Actually you can click on Tool --> --> options --> scripting. From here you can set the default to script does not exists, script indexes, foreign keys etc....

    Then when you right-click script to you will have everything 😉

  • Adam Haines (3/17/2008)


    Actually you can click on Tool --> --> options --> scripting. From here you can set the default to script does not exists, script indexes, foreign keys etc....

    Then when you right-click script to you will have everything 😉

    Cool. I didn't know that option existed.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Yeah. I just found out about it a few weeks ago :). It has the equivalent options to Tasks --> generate scripts.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply