Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Management Studio - Adding/Moving Columns require drop and re-create?

Why do I get message that the table needs to dropped and re-created when I add/move columns? I believe this happens after adding foreign key constraints.

What can I do to add new columns without dropping table?

like image 626
tvr Avatar asked Oct 09 '10 09:10

tvr


People also ask

How do you add a new column in SQL without dropping a table?

You can add a column without dropping the table. If you want the column NOT NULL then you'll have to make it accept NULL first, then set the values through an update, and lastly alter the column to NOT NULL .

How do I add a new column in SQL Server Management Studio?

Use SQL Server Management StudioIn Object Explorer, right-click the table to which you want to add columns and choose Design. Select the first blank cell in the Column Name column. Type the column name in the cell. The column name is a required value.

Can you reorder columns in SQL Server?

Using SQL Server Management StudioIn Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.


2 Answers

"Prevent saving changes that require table re-creation"

If you're more interested in simply getting SSMS to stop nagging, you can uncheck the "Prevent saving changes that require table re-creation" setting in Options->Designers->Table And Database Designers. The table(s) will still be dropped and re-created, but at least SSMS won't pester you quite as much about it.

(This assumes you're working in an dev/test environment or in a production environment where a brief lapse in the existence of the table won't screw anything up)

like image 126
John Rose Avatar answered Oct 12 '22 17:10

John Rose


Because that's how SQL Server Management Studio does it (sometimes)!

Use TSQL's ALTER TABLE instead:

ALTER TABLE
    ADD myCol int NOT NULL
like image 30
Mitch Wheat Avatar answered Oct 12 '22 18:10

Mitch Wheat