SQL Server 2008: cannot save because the table must be deleted and re-created to save?

I am stuck on a slightly obscure issue.

I am using MS SQL Managment Studio 2008. I have a new database with table X.

X has 3 columns:

  • ID, uniqueidentifier but not null
  • username, nvarchar 50, not null
  • password, nvarchar50 but not null

Now I save. Now I set the identifier to the primary key - no saving problems.

Now I am adding a column: Email, nvarchar 50, not null - when I try to save now, I cannot do this because it tells me that in order to save the table, it is necessary to delete and create a new one.

I don’t understand this, in SQL Server 2005 I’m sure it’s easy to add such a line ?!

+3
3
+2

, Damien_The_Unbeliever (EDIT: , ), , , NOT NULL, EMAIL, NOT NULL . -, EMAIL, null. , . NOT NULL EMAIL. () EMAIL, , . "temp@somedomain.com", ( , ). .

: , , .

"" - , .

+2

You cannot do this without also specifying what is the default value for a column — otherwise, how will SQL Server provide a non-null constraint for existing rows in a table?

If you try to do this with a query, you will get a lot more explanation:

ALTER TABLE Table_2 ADD Email varchar(100) not null

Msg 4901, Level 16, State 1, Line 1
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, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'Email' cannot be added to non-empty table 'Table_2' because it does not satisfy these conditions.
+1
source

Source: https://habr.com/ru/post/1774930/


All Articles