Search code examples
sql-serverdatabasealter-table

Cannot add new column to SQL Server table in Management Studio


I already have a table created with some columns. Now I am trying to add a column with a primary key. I right click the columns, add new column. It lets me add column name, data type as int. Right click on that and setting it to primary key.

When I save this, I get an error

Unable to modify table.
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT definition specified, or the column being added is an identity or timestamp column.

Can someone let me know how to alter an already created table? I need to add a primary key to it with auto increment.


Solution

  • You need to do 4 steps to get this done:

    Step 1 - create the column

    enter image description here

    Step 2 - make it the primary key

    enter image description here

    Step 3 - bring up the column or table properties

    enter image description here

    Step 4 - define it to be an IDENTITY column

    enter image description here

    Only now - save the changes. And now, it should work just fine.