patternsqlMinor
Altering nullability of column in SQL Server
Viewed 0 times
columnserversqlalteringnullability
Problem
Say I have the following table:
Now
Nevertheless, I cannot change
ALTER TABLE ALTER COLUMN field2 failed because one or more objects
access this column.
Also, I cannot change
However, I can add and then drop check constraint as many times as I want :
Is it well defined behaviour? Could someone explain why that happens or point me to documentation?
I'm using SQL Server 2008 R2 if that matters.
Thank you.
CREATE TABLE test(test_id int not null identity primary key,
field1 int not null,
field2 int
);
CREATE INDEX IDX_test_field1 ON test(field1);
CREATE INDEX IDX_test_field2 ON test(field2);Now
ALTER TABLE test ALTER COLUMN field1 int works, and field1 allows null. Nevertheless, I cannot change
ALTER TABLE test ALTER COLUMN field2 int not null because ofALTER TABLE ALTER COLUMN field2 failed because one or more objects
access this column.
Also, I cannot change
field1 back to not null. However, I can add and then drop check constraint as many times as I want :
ALTER TABLE test ADD CONSTRAINT CHK_NN_field2 CHECK (field2 IS NOT NULL);
DROP CONSTRAINT CHK_NN_field2` without any problems.Is it well defined behaviour? Could someone explain why that happens or point me to documentation?
I'm using SQL Server 2008 R2 if that matters.
Thank you.
Solution
The relevant documentation is here
altered.
The modified column cannot be any one of the following:
-
Used in an index, unless the column is a
equal to or larger than the old size, and the index is not the result
of a
-
Used in statistics generated by the
the column is a
type is not changed, and the new size is equal to or greater than the
old size, or if the column is changed from not null to null. First,
remove the statistics using the
that are automatically generated by the query optimizer are
automatically dropped by
In practice it seems that SQL Server does allow some additional cases beyond that mentioned in the documentation however.
As you show in your question
Moreover the caveat mentioned about Primary Keys seems to be untrue also. The following works fine.
ALTER COLUMN Specifies that the named column is to be changed oraltered.
The modified column cannot be any one of the following:
-
Used in an index, unless the column is a
varchar, nvarchar, orvarbinary data type, the data type is not changed, the new size isequal to or larger than the old size, and the index is not the result
of a
PRIMARY KEY constraint.-
Used in statistics generated by the
CREATE STATISTICS statement unlessthe column is a
varchar, nvarchar, or varbinary data type, the datatype is not changed, and the new size is equal to or greater than the
old size, or if the column is changed from not null to null. First,
remove the statistics using the
DROP STATISTICS statement. Statisticsthat are automatically generated by the query optimizer are
automatically dropped by
ALTER COLUMN.In practice it seems that SQL Server does allow some additional cases beyond that mentioned in the documentation however.
As you show in your question
ALTER TABLE test ALTER COLUMN field1 int null does in fact work so the restrictions on changes to columns used in indexes appear to be the same as those for user created statistics.Moreover the caveat mentioned about Primary Keys seems to be untrue also. The following works fine.
CREATE TABLE test2(pk varchar(10) primary key);
ALTER TABLE test2 ALTER COLUMN pk varchar(100) NOT NULLCode Snippets
CREATE TABLE test2(pk varchar(10) primary key);
ALTER TABLE test2 ALTER COLUMN pk varchar(100) NOT NULLContext
StackExchange Database Administrators Q#27262, answer score: 4
Revisions (0)
No revisions yet.