Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 547 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I drop a foreign key in SQL Server?

#1
I have created a foreign key (in SQL Server) by:

alter table company add CountryID varchar(3);
alter table company add constraint Company_CountryID_FK foreign key(CountryID)
references Country;

I then run this query:

alter table company drop column CountryID;

and I get this error:

> *Msg 5074, Level 16, State 4, Line 2
The object 'Company\_CountryID\_FK' is dependent on column 'CountryID'.
Msg 4922, Level 16, State 9, Line 2
ALTER TABLE DROP COLUMN CountryID failed because one or more objects access this column*

I have tried this, yet it does not seem to work:

alter table company drop foreign key Company_CountryID_FK;
alter table company drop column CountryID;

What do I need to do to drop the `CountryID` column?

Thanks.
Reply

#2
This will work:

ALTER TABLE [dbo].[company] DROP CONSTRAINT [Company_CountryID_FK]
Reply

#3
You can also Right Click on the table, choose modify, then go to the attribute, right click on it, and choose drop primary key.
Reply

#4
alter table company drop constraint Company_CountryID_FK
Reply

#5
I don't know MSSQL but would it not be:

alter table company drop **constraint** Company_CountryID_FK;

Reply

#6
Are you trying to drop the FK constraint or the column itself?

To drop the constraint:

alter table company drop constraint Company_CountryID_FK

You won't be able to drop the column until you drop the constraint.
Reply

#7
I think this will helpful to you...

DECLARE @ConstraintName nvarchar(200)
SELECT
@ConstraintName = KCU.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
ON KCU.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
AND KCU.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
AND KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
WHERE
KCU.TABLE_NAME = 'TABLE_NAME' AND
KCU.COLUMN_NAME = 'TABLE_COLUMN_NAME'
IF @ConstraintName IS NOT NULL EXEC('alter table TABLE_NAME drop CONSTRAINT ' + @ConstraintName)

It will delete foreign Key Constraint based on specific table and column.
Reply

#8
First check of existence of the constraint then drop it.

if exists (select 1 from sys.objects where name = 'Company_CountryID_FK' and type='F')
begin
alter table company drop constraint Company_CountryID_FK
end
Reply

#9
Try

alter table company drop constraint Company_CountryID_FK


alter table company drop column CountryID

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through