Translate

Tuesday, October 29, 2024

How to define foreign key in POSTGRESQL

A foreign key is a column(s) in one table that references the primary key of another table, establishing a link between the two tables. 

Syntax


  constraint <title> Foreign key (<ColumnName>) References <SourceTableName>(<columnnameinsource>),
  

Example

CREATE TABLE Warriors (
  WarriorID uuid primary key,
  WarriorName VARCHAR(200),
  WarriorTitleID uuid not null,
  WarriorWingID uuid null,
  IsActive boolean,
  constraint Warrior_To_Title Foreign key (WarriorTitleID) References Titles(TitleID),
  constraint Warrior_To_Wing Foreign Key (WarriorWingID) References Wings(WingID)
);

No comments:

Post a Comment