william-r
10/31/2017 - 9:33 PM

Add and Drop Foreign Key

Replace anything with _ (underscore)

--Creating the new table. 
CREATE TABLE Table_Name (
    Id int NOT NULL,
    Person_ID int,
    PRIMARY KEY (Id),
    CONSTRAINT Foreign_Key_Name FOREIGN KEY (Person_ID)
    REFERENCES Other_Table_Name (Person_ID)
);

--Alter the current table.
ALTER TABLE Table_Name
ADD CONSTRAINT Foreign_Key_Name
FOREIGN KEY (Person_ID) REFERENCES Other_Table_Name (Person_ID);

--Drop the foreign key
ALTER TABLE Table_Name
DROP CONSTRAINT Foreign_Key_Name;