Create table in PostgreSQL with foreign key

Create a table in PostgreSQL with a foreign key. Here is how to do that easily. Remember both of the tables contains the primary key. And we need to make a common connection using a foreign key. Just follow the command.

CREATE TABLE Employees(
    EmployeeID int,
    FirstnName varchar(255),
    LastName varchar(255),
    JobTitle varchar(255),
    PRIMARY KEY(EmployeeID)
);

CREATE TABLE Stores(
    StoreID int,
    StoreName varchar(255),
    ZipCode int,
    ManagerID int,
    PRIMARY KEY(StoreID),
    FOREIGN KEY(ManagerID) REFERENCES Employees(EmployeeID)
);

Enjoy. Happy coding.

0 Comments