Key and Referential constraints in DBMS

Key and referential constraints are fundamental concepts in relational database management systems (RDBMS) that help ensure data integrity and maintain the relationships between tables. Let’s explore these concepts in detail.

Key Constraints:

Key constraints define a set of attributes (columns) in a relational table that uniquely identify each row. They ensure that there are no duplicate or null values in the specified columns, thereby maintaining data accuracy and integrity. There are several types of key constraints:

Primary Key Constraint: A primary key is a unique identifier for a record in a table. It ensures that each row has a distinct value in the specified column(s). In most cases, a primary key is used as a reference point for establishing relationships with other tables.


    student_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50)

Unique Key Constraint: A unique key ensures that the values in the specified column(s) are unique, but unlike the primary key, it allows null values.


CREATE TABLE Employees (
    employee_id INT UNIQUE,
    first_name VARCHAR(50),
    last_name VARCHAR(50)

Candidate Key: A candidate key is a set of columns that could potentially be used as a primary key. It satisfies the uniqueness requirement but might not have been designated as the primary key.

Referential Constraints (Foreign Key Constraints):

Referential constraints establish relationships between tables by ensuring that values in one table’s foreign key column(s) match values in another table’s primary key column(s). They maintain data integrity and enforce data dependencies across related tables.

Consider the following example:

    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)

CREATE TABLE Customers (
    customer_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50)

In this example, the Orders table has a foreign key constraint (customer_id) that references the primary key (customer_id) of the Customers table. This ensures that an order can only be associated with a valid customer.

Key and Referential Constraints in Combination:

The interaction between key and referential constraints is crucial for maintaining data consistency. For instance, in the example above, the customer_id column in the Orders table is a foreign key that references the customer_id primary key in the Customers table. This establishes a relationship between the two tables, allowing you to retrieve data from both tables while ensuring that the references remain accurate.

When a record is deleted or updated in the referenced table (e.g., a customer is deleted), the RDBMS can be configured to cascade these changes to related records, maintaining the referential integrity of the data.

In summary, key and referential constraints are essential tools in the arsenal of a database designer. They safeguard data accuracy, enable efficient querying of related data, and establish the foundation for building robust relational databases.





Leave a Reply

Your email address will not be published. Required fields are marked *