SQL – Foreign Key

SQL Foreign Key

In relational database management systems, maintaining data integrity and creating relationships among different entities is important for effective organization and retrieval of data

. The Foreign Key is the tool that manages this by forming links between tables. In this article, we will discuss the SQL Foreign Key Constraint, Referential integrity in SQL, its implication, its usage, along with comparison with primary keys, as well as a practical example.

Understanding Foreign Keys

What is a Foreign Key?

A Foreign Key in the database can be explained as that one or more columns appearing in a table by which a unique row in another table can be identified. This establishes a relationship between two tables, making sure that the database maintains referential integrity. The table whose column is the foreign key is called the child table while the table where the original primary key resides is called the parent table.

Key Characteristics of Foreign Keys

  1. Reference to Primary Key: A foreign key in one table refers to the primary key in another table. This establishes a link between the two tables.
  2. Nullability: Foreign keys can accept NULL values unless specified otherwise, meaning that not all records in the child table must reference records in the parent table.
  3. Referential Integrity: Foreign keys ensure that relationships between tables remain consistent. If a record in the parent table is deleted or updated, the corresponding foreign keys in the child table can either be set to NULL or also deleted/updated, based on the defined actions.

Why are Foreign Keys Important?

Foreign keys are essential for several reasons:

  • Data Integrity: They enforce referential integrity, ensuring that relationships between tables remain valid.
  • Cascading Actions: Foreign keys can be set to perform cascading actions (e.g., DELETE, UPDATE) when a record in the parent table is modified.
  • Complex Queries: They allow for complex queries that can retrieve related data across multiple tables.

Foreign Key Constraint in SQL

What is a Foreign Key Constraint?

The Foreign Key Constraint is a rule applied to a column or a set of columns in a table that ensures the values in that column(s) match values in a column of another table, usually the primary key.

Creating a Foreign Key in SQL

To create a foreign key, you can define it at the time of table creation or alter an existing table to add a foreign key. Below, we’ll discuss the syntax and provide examples.

SQL FOREIGN KEY Syntax

The basic syntax for creating a foreign key when defining a table is as follows:

CREATE TABLE child_table (
    column1 datatype,
    column2 datatype,
    ...
    FOREIGN KEY (foreign_key_column) REFERENCES parent_table (primary_key_column)
);

Example: Creating a Foreign Key

Let’s consider an example with two tables: Departments and Employees. The Departments table will serve as the parent table, and the Employees table will be the child table.

CREATE TABLE Departments (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(100)
);

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    EmployeeName VARCHAR(100),
    DepartmentID INT,
    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

In this example:

  • The Departments table has a primary key DepartmentID.
  • The Employees table has a foreign key DepartmentID that references the primary key in the Departments table.

How to Add Foreign Key in SQL

If you already have an existing table and want to add a foreign key, you can use the ALTER TABLE statement.

Example: Adding a Foreign Key

Suppose the Employees table is created without a foreign key, and you want to add it later:

ALTER TABLE Employees
ADD FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID);

Foreign Key Example in SQL

Now that we have established our tables, let’s insert some data into the Departments and Employees tables.

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES
(1, 'Human Resources'),
(2, 'Finance'),
(3, 'IT');

INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID) VALUES
(101, 'Alice Smith', 1),
(102, 'Bob Johnson', 2),
(103, 'Charlie Brown', 1);

Referential Integrity in SQL

Referential integrity in SQL is a crucial concept that ensures the accuracy and consistency of relationships between tables within a relational database. By enforcing referential integrity in SQL, databases maintain valid links between foreign keys in one table and primary keys in another, preventing orphaned records and ensuring that every foreign key value corresponds to an existing primary key. This means that if a record in a parent table is deleted or updated, any corresponding records in the child table must also be handled appropriately to maintain referential integrity in SQL. Without this enforcement, data inconsistencies can arise, leading to incomplete or erroneous information being returned in queries. Therefore, understanding and implementing referential integrity in SQL is essential for maintaining the reliability and integrity of data across complex relational structures.

Referential integrity enforces that foreign key values in a child table must match valid primary key values in a parent table. For example, if you try to insert an employee with a DepartmentID that doesn’t exist, you get the following error:

INSERT INTO Employees (EmployeeID, EmployeeName, DepartmentID) VALUES
(104, 'David Wilson', 5);

This will fail because 5 does not exist in the Departments table.

Foreign Key vs Primary Key

Understanding the differences between foreign keys and primary keys is essential for database design. Here’s a comparison table:

FeaturePrimary KeyForeign Key
UniquenessMust be unique across the tableCan allow duplicate values
NULL ValuesCannot contain NULL valuesCan contain NULL values
RelationshipIdentifies a record in its own tableReferences a record in another table
Number of KeysOnly one primary key per tableMultiple foreign keys are allowed
PurposeEnsures record uniquenessEstablishes relationships between tables

Composite Foreign Key in SQL

A Composite Foreign Key is a foreign key that is composed of two or more columns. This is used when one single column cannot uniquely identify a record in the child table.

Example of Composite Foreign Key

Let’s say we need to keep track of who is assigned to what project, and we define a Projects table in which we have a composite foreign key within the EmployeeProjects table referencing EmployeeID and ProjectID.

CREATE TABLE Projects (
    ProjectID INT PRIMARY KEY,
    ProjectName VARCHAR(100)
);

CREATE TABLE EmployeeProjects (
    EmployeeID INT,
    ProjectID INT,
    PRIMARY KEY (EmployeeID, ProjectID),
    FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID),
    FOREIGN KEY (ProjectID) REFERENCES Projects(ProjectID)
);

In this example:

  • The EmployeeProjects table has a composite primary key formed by EmployeeID and ProjectID.
  • Both EmployeeID and ProjectID are foreign keys referencing their respective parent tables.

Dropping a Foreign Key in SQL

If there’s a need to remove a foreign key from a table, you can do so using the ALTER TABLE command.

Example: Dropping a Foreign Key

To drop a foreign key on the Employees table, you need to have the name of the foreign key constraint. If you did not assign a name when defining the foreign key, the database probably assigned one by default.

Here’s the syntax to drop a foreign key:

ALTER TABLE Employees
DROP FOREIGN KEY foreign_key_name;

If you don’t know the name, you can probably locate it by searching through the metadata of your database or with the use of a database management tool.

Important Considerations When Working with Foreign Keys

  • Cascading Actions: You can specify a foreign key and at what point it should cascade. It would be by deleting child records when you delete a parent record.
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE CASCADE
  • Performance Implications: Although foreign keys maintain data integrity, they can also bring performance implications. Balance your database structure with respect to maintaining data integrity and performance.
  • Reference Integrity Checks: You need to have a review of your foreign key relationships to ensure that they are valid and maintained during the lifetime of your database.
  • Documentation: Document your foreign key relationships as clear as you can, including which tables are affected, why they are affected, and what cascading actions take place.

Advantages of SQL Foreign Key

A foreign key is one of the most important relational database concepts; it links two tables to establish referential integrity and enforce the relationships between data. It allows one table to reference another table’s primary key, creating meaning through the relationships between related data sets. Here are the key advantages of using foreign keys in SQL:

1. Enforces Referential Integrity

The principal advantage of using a foreign key is that it keeps referential integrity in the database. Since a foreign key should match one of valid values in the referenced table’s primary key, it does not allow orphaned records which often result in data corruption, thereby keeping the consistency of the data and its relation with the tables.

2. Establishes relations between tables

Foreign keys truly allow to link tables meaningfully. This makes it much easier to model complex relationships within a relational database, since such relationships can be used for data normalization – related information is placed in different tables and referred to via foreign keys. Such an approach eliminates redundant data, thus increasing the overall structure of the database.

3. Data retrieval is less complicated with joins

Foreign keys enable retrieval of related data from multiple tables by using SQL joins much easier. Using foreign keys, a developer will be in a position to write more efficient queries that merge data for different sources, meaning the creation of complex reports will be made easy and comprehensive analysis of data regarding relations between all sets of data will be possible.

4. Improves Data Consistency

Another way in which foreign keys improve data consistency is by permitting to be referenced only valid and existing data. For example, if a table contains an order having a foreign key to another table named customer, then the foreign key constraint will check each order against that valid customer. So there will not be any inconsistencies such as an invalid reference or even missing related data.

5. Cascading Actions on Delete and Update

Many database systems support cascading actions like ON DELETE CASCADE and ON UPDATE CASCADE for the foreign keys. These actions automate the deletion and updating of related records. For example, when a record is deleted in the parent table, all the related child records in the other table are automatically deleted, thus less human intervention with intact data integrity.

6. Supports Normalization and Reduces Redundancy

The use of foreign keys enables the application of data normalisation techniques that minimize the likelihood of data redundancy. Data that has relationship can be actually held within a different table and then referenced through foreign keys, therefore attaining an even better organised and efficient database structure. This ensures avoidance of duplicate data, thereby reducing the chances of inconsistency and error making.

7. Advancements Database Design and Scalability

In reality, foreign keys are one of the things that aid in database design improvement. For instance, these ensure that data is gathered into tables that are somehow interrelated with one another. In addition to that, this organization improves readability of a database schema and also scales better. As the database grows, foreign keys will enable easier management of relationships between tables. That is why adding, modifying, or removing data would be much easier without interrupting the entire system.

8. Data Integrity Across Multiple Tables

The database system can enforce rules ensuring data integrity across the entire database by linking the tables through foreign keys. A foreign key constraint prevents the insertion of a record in the child table in the absence of a related record in the parent table. Such integrity at that level helps in avoiding any given potential problems such as inconsistent or incomplete data.

9. Blocks Unintended Deletions

Foreign keys will prevent accidental deletion of the record from the parent table if such records are still referenced by other tables. For instance, if a user wants to delete one row in a table that has a referencing foreign key in another table, then the database will never let this row deleted without proper cascading actions defined, so that data cannot get lost accidentally.

10. Supports Transaction Management

Foreign keys simplify the process of dealing with multiple tables in transactions. They ensure consistency in operations made on related tables given any certain logic and will most likely have fewer errors when several operations are involved in database transactions. This is especially applicable in scenarios where changes introduced in one table are reflected in related tables.

Disadvantages of SQL Foreign Key

Although Foreign Keys is an essential feature, which enforces relationships in relational databases and maintains referential integrity; it carries several disadvantages. These need to be known as they would impact performance, flexibility, and scalability while you intend to design and manage the databases. Below are the key disadvantages of foreign keys in SQL:

1. Performance Overhead

Foreign keys add performance overhead, especially on a large database size or high-traffic system. Every record that is being inserted, updated, or even deleted makes a pass through the database checking for foreign key constraints to ensure referential integrity is maintained. Operations may slow down when dealing with complex relationships within large tables or in performing modifications of data in bulk.

2. Complexity in Data Maintenance

Foreign keys now add complexity to a database design and maintenance. Managing relationships among many tables require quite elaborate planning and may render database update complex. In the above sense, for instance, deleting or updating records in one table may need to refer to the tables associated with the first table since you can’t delete or update without violating the foreign key constraints.

3. Limited Deletions and Updates

Another limitation is the foreign key in itself, because it can prevent you from deleting or updating records in the parent table when those records are referenced by child tables. Some operations might not be allowed without a cascading action such as ON DELETE CASCADE or ON UPDATE CASCADE, as attempting to delete or change parent records could cause constraint violations that developers would then have to track and correct.

4. Increased Complexity for Cascading Actions

Although cascading actions such as ON DELETE CASCADE or ON UPDATE CASCADE can automate deleting or updating related rows, these can also become a danger. Cascading deletes, for example may unintentionally delete several related data elements spread out across multiple tables. Not updated and tested correctly, this would result in the loss of data, so such features are used with caution.

5. Limitations of Distributed Systems

Foreign keys are challenging in distributed or sharded databases due to their spread across different servers or locations. Imposing foreign keys across distributed systems is either very complex or utterly impossible. This happens for the reason that real-time synchronization of various nodes for the same data is extremely hard. Consequently, foreign keys are often avoided due to the increased complexity and performance reduction in such architectures.

6. Difficulties in data migration

Foreign key constraints will also raise additional complications in migration data, such as migrating data between databases and from one system to another. All relations that are due to be maintained while migrating must be planned well, and any error will result in inconsistent data. Conversely, migrating data from those systems that do not have foreign key constraints into a system which imposes them can be challenging, especially if it is already not normalized.

7. Structured Data

This structure makes the database less flexible as changes are needed. Modifying or adding new relationships between tables could be significant schema changes that might break existing applications or workflows. This inflexibility in databases becomes a bottleneck in agile environments, where schemas frequently change.

8. Deadlock Probabilities

In systems of heavy concurrent access, foreign key increases the probability of deadlocks-a state wherein two or more transactions are blocked each other due to the waiting involved on resources that lock up by the other transaction. This will occur when several users update parent and child tables in a synchronized manner, and foreign key constraints must be verified before applying the transactions.

9. Index Maintenance

Foreign keys are typically indexed to expedite constraint checking. The overhead of maintaining these indexes, however, is additional storage. This also slows down data modification operations like inserts, updates, and deletes. Thus, as the size of the tables grows more is the overhead of managing the indexes which results in reduced database performance.

10. Reduced In Denormalized Databases

Foreign keys are less applicable in some designs of databases. Such cases include those using denormalization as optimization for performance, like NoSQL databases. In such a scenario, faster and scalable results may be preferred over strict referential integrity. In such scenarios, foreign keys can be regarded as redundant or even a hindrance. This makes them less applicable in non-relational or hybrid systems.

11. Dependency on Parent Table Availability

For instance, since foreign keys rely on the existence of records in a parent table, every issue with the parent table – corruption, lockout, or downtime – can directly impact the availability and usability of associated data in the child table. This could make the whole system more brittle as problems within the parent table propagate throughout other parts of the database.

12. Problems with Conceived Soft Deletes

The main problem with implementing foreign keys is that implementing soft deletes would mean that a record might be marked as inactive but not actually deleted from the database. Even though we technically do delete the record, we cannot refer to a deleted record in terms of foreign key constraints. Implementing foreign keys with functionality around soft deletes necessitates more complex logic: the addition of filter conditions to exclude “deleted” records when constructing the queries-problematic.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading