Foreign keys are a set of constraints in DBMS that establish relationships between tables and also ensure consistency and integrity of data.
A foreign key is applied to a column (or attribute) of one table which references the primary key of a column in another table.
It act as a cross-reference between two tables. It helps to maintain data integrity which is known as "Referential Integrity Constraints".
A foreign key ensures that the data in one table corresponds to a valid entry (or entries) in another table.
A foreign key can refer to a column in the same table, which is called self-referencing. This is often used to show relationships within the same table, like a manager and their employees, members of a family tree, or folders inside other folders.
Syntax For Creating and Deleting Foreign Key
Letβs see the Foreign Key syntax used for creating a table.
Data Integrity: We need foreign keys as they help us making sure that data is consistent, complete, between both the tables and overall accuracy is maintained.
Query Optimization: Foreign keys optimizes the query execution by utilizing query plans more efficiently and improving the relationships between tables. It also helps in fast data retrieval.
Establishing Relationships: The main requirement of foreign keys is the establishment of relationships between tables. It makes sure that data is linked across multiple tables and helps in storing and retrieving data.
Data Security: Foreign keys helps in improving the security of data by preventing unauthorized modifications or deletions of important data in the referenced table.
Database Maintenance: Foreign keys are required in database maintenance tasks and help to ensure integrity and consistency of data during these operations.
To Read more about SQL FOREIGN KEY Constraint Refer, Here.