Create Table with SQL FOREIGN KEY Constraint

Create Table with SQL FOREIGN KEY Constraint

In this article, when you create two tables that are related to each other, they are often related by a column in one table referencing the primary key of the each other table, that column is called the foreign key.

If you would like to make sure that the SQL engine would not insert a row with a foreign key that references a non-existent primary key, then you can add a FOREIGN KEY constraint in your CREATE TABLE statement.

The table having the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

CREATE TABLE Order (
    ID int NOT NULL PRIMARY KEY,
    OrderNumber int NOT NULL,
    PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
)

DROP a FOREIGN KEY Constraint

ALTER TABLE Order
DROP CONSTRAINT FK_PersonOrder

 

 

 

 

2020-04-03