简体   繁体   中英

How to delete an existing column with Not Null constraint in SQL Server

I am using SQL Server Management Studio. I have a table Employee . I want to delete the Starting Date and Ending Date columns. Initially, Not Null constraints were assigned to them. Then I changed to Null .

But whenever I am trying to delete these two columns, I am getting an error. Please see the screenshot and help me execute the task.

Thanks in advance

在此处输入图片说明

First, drop the constraints.

ALTER TABLE [dbo].[Employee] DROP CONSTRAINT NameYourConstraint;

If you don't now the names:

SELECT *
FROM [sys].[default_constraints]
WHERE [parent_object_id] = OBJECT_ID('[dbo].[Employee]');

You can try removing the contrainsts first -

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM