T-SQL Tutorial

Create Table with Foreign Key T-SQL


Create Table with Foreign Key

To create a table with Foreign Key constraint you need to use create table command like in the below example.

Create Table with Foreign Key

USE model;
GO
create table Customers
(id int not null primary key,
name varchar(500) not null,
city varchar(500)
);
GO
create table Sales(
OrderID int identity not null primary key,
CustomerID int not null,
SalesPersonID int not null,
OrderDate datetime not null,
Status int not null,
constraint FK_CustomerID
foreign key (CustomerID) references Customers(ID)
);
GO