The following syntax will add a foreign key to the database table at the time of table creation,
CREATE TABLE<table_name>(col datatype FOREIGN KEY REFERENCES<pk_table>(PK_colname),col datatype,....) pk_table
is the name of the table which will be accessed by the foreign key and that contains the primary key. PK_colname
is the name of the column on which primary key is defined in the primary table.
The datatype of both the columns should match
, i.e, primary key column and foreign key column.
A table can contain more than one foreign key.
The value of the foreign key be either null or any value from the refereed primary key domain.