Define foreign key in SQL COMMANDS
Share
Define foreign key in SQL COMMANDS
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Hope it helped
FOREIGN KEY
The SQL FOREIGN KEY CONSTRAINT is used to ensure the referential integrity of the data in one table to match values in another table.
The FOREIGN KEY CONSTRAINT is a column or list of columns which points to the PRIMARY KEY of another table.
The main purpose of FOREIGN KEY is, only those values will appear which are present in the primary key table.
SQL FOREIGN KEY
For each row in the referencing table( the table contains the FOREIGN KEY), the foreign key must match an existing primary key in the referenced table(the table contains the PRIMARY KEY). This enforcement of FOREIGN KEY called the Referential Integrity.
The structure and data type of PRIMARY KEY and FOREIGN KEY must be same.
The values of the FOREIGN KEY columns in each row of the referencing table have to match with the values of the corresponding primary key columns of a row in the referenced table.
Syntax:
CREATE TABLE <table_name>(
column1 data_type[(size)] ,
column2 data_type[(size)] ,
constraint(constraint_name)
FOREIGN KEY [column1,column2...]
REFERENCES [primary_key_table]
Foreign key is used to help to read the data of a primary table:
Explanation:
Learn More :