【问题标题】:How to delete an existing column with Not Null constraint in SQL Server如何在 SQL Server 中删除具有 Not Null 约束的现有列
【发布时间】:2021-03-04 12:15:21
【问题描述】:
我正在使用 SQL Server Management Studio。我有一张桌子Employee。我想删除 Starting Date 和 Ending Date 列。最初,Not Null 约束被分配给它们。然后我换成了Null。
但每当我尝试删除这两列时,我都会收到错误消息。
请看截图并帮我执行任务。
提前致谢
【问题讨论】:
标签:
sql
sql-server
tsql
constraints
ssms
【解决方案1】:
首先,放弃约束。
ALTER TABLE [dbo].[Employee] DROP CONSTRAINT NameYourConstraint;
如果你现在不知道名字:
SELECT *
FROM [sys].[default_constraints]
WHERE [parent_object_id] = OBJECT_ID('[dbo].[Employee]');
【解决方案2】:
您可以先尝试移除约束 -
ALTER TABLE dbo.Employee DROP CONSTRAINT DF__Employee__Starti__47DBAE45;
ALTER TABLE dbo.Employee DROP CONSTRAINT DF__Employee__Ending__5AEE82B9;
ALTER TABLE dbo.Employee DROP COLUMN StartingDate;
ALTER TABLE dbo.Employee DROP COLUMN EndingDate;