简体   繁体   中英

Unable to drop foreign key in SQL server

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'.

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.

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