FOREIGN KEY
Foreign key is used to setup relation between two tables in a database. A foreign key is a values that appear in both the tables in database system.
The table which contain foreign key is called the child table while the table to which foreign key is referred is called parent table. Foreign key mainly reference a primary key in the parent table.
we can create a foreign key by using a CREATE TABLE statement or by ALTER TABLE statement.
Syntax
Below is the syntax for creating a foreign key by an CREATE TABLE statement :-
CREATE TABLE child-table
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
FOREIGN KEY (child-column-name(s))
REFERENCES parent table (parent-column-name(s))
);
Below is the syntax for creating a foreign key by an ALTER TABLE statement
ALTER TABLE child-table
(
FOREIGN KEY (child-column-name(s))
REFERENCES parent-table (parent-column-name(s)
);
0 Comment(s)