All can be done in SQL Server Management Studio.
The Script Table As option generates create code for primary and foreign keys and constraints. It doesn't create script for the indexes, so you have to do that in another step.
type, I suggest expanding all object type nodes in SSMS Object Explorer.
To create a script for table structure, primary and foreign keys, and constraints
- Run SQL Server Management Studio
- In Object Explorer, expand Databases
- Expand Tables
- Right click the table and select Script table as | CREATE TO | New Query Editor Window
The generated script will be shown in the Query Editor tab. Make sure you change the table, primary and foreign key , and constraint names, as these names have to be unique.
Otherwise, you'll get an error message saying something like 'There is already an object named 'Address' in the database.', or 'The operation failed because an index or statistics with name 'AK_Address_rowguid' already exists on table 'Person.Address'.'
To create a script for an index
Use the steps similar to the above:
To create an INSERT INTO script for data
- Right-click the database (not the table!)
- Open Tasks | Generate Scripts
- On the Choose Objects tab, select the table to script
4.In the Set Scripting Options tab, click Advanced and make sure the Types of data to script option is set to Data only
Note: If you select Schema & Data the generated schema script will be identical to the script generated in the first method in this answer.
5.Update the table name to the name of the new table before you execute the script