-1

I am trying this query to drop a foreign key ALTER TABLE dbo.[User] DROP FOREIGN KEY FK_User_UserTypeID.

But I am getting this

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'FOREIGN'.

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

0

SQL FOREIGN KEY on CREATE TABLE : -

CREATE TABLE Orders (
    OrderID int NOT NULL PRIMARY KEY,
    OrderNumber int NOT NULL,
    PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);

SQL FOREIGN KEY on ALTER TABLE :-

ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

DROP a FOREIGN KEY Constraint :-

ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder;

NOTE Should not have any dependency on foreign key while dropping key constraints, otherwise you will not be able to drop the constraint.

HamTheAstroChimp
  • 1,275
  • 4
  • 15
  • 28
  • `ALTER TABLE dbo.[User] DROP CONSTRAINT FK_User_UserTypeID` gives me `Msg 3728, Level 16, State 1, Line 1 'FK_User_UserTypeID' is not a constraint. Msg 3727, Level 16, State 0, Line 1 Could not drop constraint. See previous errors.` – Moeez Dec 09 '20 at 14:53