It is very easy to use the database management tool to directly visualize a column in the SQL Server database, but what if you want to use an SQL script? You may say it's very simple to use it directly.
Alter table [name of the table where the field to be deleted is located]
Drop column [fields to be deleted];
Go
Isn't it OK?
However, if there is a constraint on this field, a prompt similar to the following will pop up:
Message 5074, level 16, state 1, 1st rows
The object 'df _ *** _ 682a18f4 'depends on the column '***'.
Message 4922, level 16, status 9, 1st rows
Alter table drop column *** failed because one or more objects access this column.
How can this problem be solved?
First, you can find the constraint for this field and then delete it.
-- Query all the field constraints of a table
Sp_helpconstraint [name of the table where the field to be deleted is located]
-- Query the default constraints of a field in a table
Select name as constraint name
From sysobjects
Where ID in (select cdefault
From syscolumns
Where name in ([field to be deleted])
And Id = (select ID
From sysobjects
Where name = [name of the table where the field to be deleted is located])
After the constraints are found, the constraints will be deleted.
-- Delete constraints on fields
Alter table [name of the table where the field to be deleted is located]
Drop constraint name;
Go
Now let's Delete the field you want to delete.
Alter table [name of the table where the field to be deleted is located]
Drop column [fields to be deleted];
Go