Define foreign key in SQL COMMANDS
Answers
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:
- The table is used to hold the data but there are some constraints that the data can be unique.
- So there is a need to make the two table but there is a relation between the two tables which is maintained by the primary key and the foreign key.
- So the data of a primary table is read by the help of the primary key, but the data of the foreign table is read by the primary key and foreign key.
Learn More :
- Database : https://brainly.in/question/10641019