开发者

why can't I convert a field in sql server from not null to null

开发者 https://www.devze.com 2023-01-27 07:46 出处:网络
I have a db table that previously was not null (so every record had a value). I now want to make it nullable so its optional but it seems like SQL won\'t let you do this without deleting and recreatin

I have a db table that previously was not null (so every record had a value). I now want to make it nullable so its optional but it seems like SQL won't let you do this without deleting and recreating the table.

Why would it have this restriction if no data will be affected as I am going to a more lenient model (not a stric开发者_开发技巧ter one)?


You can stop the table designer from blocking you: Tools | Options | Designers | Table and Database Designers > uncheck "Prevent saving changes that require table re-creation".


ALTER TABLE table_name ALTER COLUMN column_name data_type NULL


You can:

create table test (id int not null)
insert test values (1), (2), (3), (4), (5)
insert test values (null) -- fails
alter table test alter column id int null
insert test values (null) -- succeeds
select * from test
0

精彩评论

暂无评论...
验证码 换一张
取 消