Relational Model Constraints

Relational databases are widely used; they provide structured and reliable ways to store and access the data. To ensure data integrity and consistency, relational databases mostly rely on certain rules known as constraints. These constraints are used to maintain the quality and reliability of data across different tables and records.

In this chapter, we will take a look at the types of relational model constraints, supplemented with practical examples and explanations for a better understanding.

Relational Model Constraints

Relational model constraints are set of rules applied to the structure and data of a database to maintain logical consistency. They ensure that data is valid according to the rules defined in the database schema. These constraints can be broken down into three main types as given below −

  • Inherent model-based constraints − Implied by the nature of the relational model itself.
  • Schema-based constraints − Explicitly defined within the database schema and enforced by the database system.
  • Application-based constraints − Managed through external application logic as they cannot be directly expressed in the schema.

Types of Relational Model Constraints

Relational model constraints can be of the following types −

  • Domain Constraints
  • Key Constraints
  • Entity Integrity Constraint
  • Referential Integrity Constraint

Let’s understand each of these in detail.

Domain Constraints

Domain constraints are used to specify that, every attribute in a database must contain only values from a predefined set known as domain. This ensures that each column in a table holds data of a particular type and format.

For example, consider an EMPLOYEE table with an Age column that only allows integer values between 18 and 65. This domain constraint ensures that inserting a value such as “17” or “70” would violate the rule and be rejected by the system.

If we define Age as INTEGER CHECK (Age BETWEEN 18 AND 65), any attempt to insert Age = 17 will trigger an error. The domain constraints is used to maintain data accuracy by preventing improper data types or out-of-bounds values.

Key Constraints

Key constraints are used for each record within a relation. It can be uniquely identified. This uniqueness is achieved through keys.

Keys can be of the following types −

  • Superkey − Any set of attributes that uniquely identifies a tuple in a relation.
  • Candidate key − A minimal superkey. Removing any attribute would break the uniqueness property.
  • Primary key − A chosen candidate key that uniquely identifies each tuple in the table.

For example, consider a table called STUDENT as given below −

NameSSNHome PhoneAddressOffice PhoneAgeGPA
Dick Davidson422-11-2320NULL3452 Elgin Road(817)749-1253253.53
Barbara Benson533-69-1238(817)839-84617384 Fontana LaneNULL193.25
Rohan Panchal489-22-1100(817)376-9821265 Lark Lane(817)749-6492283.93
Chung-cha Kim381-62-1245(817)375-4409125 Kirby RoadNULL182.89
Benjamin Bayer305-61-2435(817)373-16162918 Bluebonnet LaneNULL193.21

The SSN (Social Security Number) serves as a primary key because each student must have a unique SSN. The primary key constraint guarantees that no two students can have the same SSN.

This is important because the primary keys help to maintain the identity of each record. If a STUDENT table did not have a primary key, then distinguishing between duplicate records would be difficult, which will lead to a potential data ambiguity.

Entity Integrity Constraint

The entity integrity constraint states that, the primary key of a relation must always have a non-NULL value. It is needed because primary keys are used to identify tuples, and a NULL value would make identification impossible.

Consider a table called CUSTOMER. If Customer_ID is defined as the primary key, then any attempt to insert a record with “Customer_ID = NULL” will violet the entity integrity constraint. It will be rejected by the system.

Practically, we can consider a situation where a banking system has a Customer relation with primary keys representing account numbers. If one record had Account_ID = NULL, then it would be impossible to reference or link this record.

Referential Integrity Constraint

Referential integrity constraint states that, the relationships between tables remain consistent. A foreign key in one table must either match a primary key value in another table or be NULL. This constraint can preserve the logical connections between records in different tables.

Suppose we have an EMPLOYEE table with a Dno column. Here, it references the Dnumber from the DEPARTMENT table. If Dno is set to a value that does not exist in the DEPARTMENT table, then it violates the referential integrity constraint.

Detailed Scenario − See the Employee table −

FnameMinitLnameSSNBdateAddressSexSalarySuper_ssnDno
JohnBSmith1234567891965-01-09731 Fondren, Houston, TXM300003334455555
FranklinTWong3334455551955-12-08638 Voss, Houston, TXM400008886655555
AliciaJZelaya9998877771968-01-193321 Castle, Spring, TXF250009876543214
JenniferSWallace9876543211941-06-20291 Berry, Bellaire, TXF430008886655554
RameshKNarayan6668844441962-09-15975 Fire Oak, Humble, TXM380003334455555
JoyceAEnglish4534534531972-07-315631 Rice, Houston, TXF250003334455555
AhmadVJabbar9879879871969-03-29980 Dallas, Houston, TXM250009876543214
JamesEBorg8886655551937-11-10450 Stone, Houston, TXM55000NULL1

See the Department table −

DnameDnumberMgr_ssnMgr_start_date
Research53334455551988-05-22
Administration49876543211995-01-01
Headquarters18886655551981-06-19

Valid Insertion − Adding an EMPLOYEE with Dno = 4 is allowed if Dnumber = 4 exists in DEPARTMENT.

Violation Example − Inserting an EMPLOYEE with Dno = 99, where Dnumber = 99 does not exist in DEPARTMENT, would be rejected.

This constraint is particularly useful in maintaining relationships between parent and child tables. So, deleting a department referenced by employees must either be restricted or cascaded to maintain integrity.

Handling Constraint Violations

In this section, let’s understand the kind of operations that can cause constraint violations and what precautionary measures can be taken in order to prevent such operations.

Insertion Violations

An insertion operation can violate various constraints because of −

  • Domain Constraint Violation − Inserting a non-integer value in an Age column defined as an integer will be rejected.
  • Key Constraint Violation − Adding a tuple with a duplicate Ssn in a STUDENT table where Ssn is the primary key will result in an error.
  • Entity Integrity Violation − Attempting to insert a row with NULL as the primary key value violates the entity integrity constraint.
  • Referential Integrity Violation − Inserting an EMPLOYEE with a Dno value that does not match any Dnumber in DEPARTMENT.

Deletion and Referential Integrity

Deleting a tuple can trigger constraint violations, particularly for referential integrity. For example, deleting the DEPARTMENT record that is referenced by the Dno of an EMPLOYEE tuple results in referential integrity issues.

We can solve this issue by Restrict operation, like prevent deletion if it causes a violation. Or cascade, that is, automatically delete all dependent records. There is another solution: Set NULL / Default, i.e., modify the foreign key in dependent records to NULL or a default value.

Update Operations

Updating a record can affect primary keys and foreign keys. Modifying a primary key is similar to deleting the original record and inserting a new one, which may violate existing constraints. Updates can be two types:

  • Safe Update − Changing an employee’s salary does not affect primary or foreign keys and is permissible.
  • Risky Update − Modifying Ssn in EMPLOYEE to an existing Ssn violates the primary key constraint.

Other Constraints and Business Rules

We have studied the rules, but there are some other application-based constraints, or business rules. These rules include those that cannot be easily defined within the schema, such as −

  • “An employee’s salary must not exceed that of their supervisor.”
  • “Total weekly working hours for an employee should not exceed 56 hours.”

These are typically enforced using application logic or triggers.

Conclusion

In this chapter, we presented an elaborate explanation of how relational model constraints play a crucial role in maintaining data integrity and consistency in a database. We understood the different types of constraints, such as domain constraint, key constraint, entity integrity constraints, and referential integrity constraints. We discussed practical examples for these constraints in action. We also reviewed how constraint violations can be handled and the importance of application-based rules.

Comments

Leave a Reply

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