4 min read

Data Modeling: Primary and Foreign Keys, Constraints

Primary keys and foreign keys are fundamental concepts in relational databases, playing a crucial role in establishing relationships between tables and ensuring data integrity. Let's explore each of these key concepts:

Primary Key:

Definition:

A primary key is a unique identifier for a record (row) in a table. It serves as a means to uniquely identify each record within the table, ensuring that there are no duplicate values in the primary key column.

Characteristics:

  1. Uniqueness: Each value in the primary key column must be unique within the table.
  2. Non-null: The primary key column cannot contain null (empty) values. This is a necessary constraint on the column
  3. Permanence: Ideally, the values in the primary key column should be stable and not change over time.
  4. Single-Column or Composite: A primary key can consist of a single column or a combination of multiple columns (composite key) to achieve uniqueness.

Example:

Consider a table "Students" with the following structure:

StudentID FirstName LastName Age
1 Alice Johnson 20
2 Bob Smith 22
3 Charlie Brown 21

In this example, "StudentID" is a primary key because it uniquely identifies each student.

Foreign Key:

Definition:

A foreign key is a column or a set of columns in a table that refers to the primary key of another table. It establishes a link between the data in two tables, creating a relationship.

Characteristics:

  1. References Primary Key: The foreign key in one table refers to the primary key in another table.
  2. Ensures Referential Integrity: The foreign key ensures that values in the referencing column (foreign key) exist in the referenced column (primary key) of the related table.
  3. Can Allow Null: Unlike the primary key, a foreign key column can contain null values. A null in the foreign key indicates that the relationship is not mandatory.

Example:

Consider two tables, "Students" and "Courses," where "Courses" has a foreign key referencing the "StudentID" in the "Students" table:


Students Table:

StudentID FirstName LastName Age
1 Alice Johnson 20
2 Bob Smith 22
3 Charlie Brown 21

Courses Table:

CourseID CourseName StudentID
101 Math 1
102 History 2
103 Chemistry 1

In this example, "StudentID" in the "Courses" table is a foreign key that references the primary key "StudentID" in the "Students" table. This establishes a relationship between the two tables based on the students' IDs.

Relationship between Primary Key and Foreign Key:

  • One-to-Many Relationship: The most common relationship is a one-to-many relationship. In the example above, one student (primary key) in the "Students" table can be associated with multiple courses (foreign key) in the "Courses" table.
  • Referential Integrity: The foreign key ensures referential integrity by preventing the creation of "orphan" records. If a foreign key references a non-existent primary key, the database will typically prevent such an operation.
  • Cascade Actions: In some database systems, you can define cascade actions for foreign keys. For example, if a referenced record in the primary key is deleted, the corresponding records in the foreign key can be automatically deleted (cascade delete) or set to null (cascade update).
    • For example, if a Student drops out of the university, then the student should be removed from all of the Courses they were enrolled in. All the courses in the courses table should remove all instances of that enrollment of that particular StudentID

Understanding and properly implementing primary and foreign keys are essential for designing well-structured and relationships-driven relational databases. They contribute to data consistency, integrity, and the ability to represent complex relationships between different entities in a database.


Constraints

Constraints are rules or conditions that are defined to control the type of data that can be stored in a table. They play a vital role in maintaining and preventing the entry of invalid or inconsistent information. Here are some common types of constraints in a relational database:

1. Primary Key Constraint:

  • Definition: A primary key constraint uniquely identifies each record in a table.
  • Characteristics:
    • The primary key must contain unique values for each record.
    • It cannot contain null (empty) values.
    • Each table can have only one primary key

2. Foreign Key Constraint:

  • Definition: A foreign key constraint establishes a link between two tables by enforcing referential integrity.
  • Characteristics:
    • The foreign key in one table refers to the primary key in another table.
    • It ensures that values in the foreign key column exist in the referenced primary key column.

3. Unique Constraint:

  • Definition: A unique constraint ensures that values in a column or a combination of columns are unique across all records in the table.
  • Characteristics:
    • It allows null values, but if a value is present, it must be unique.

Example:

4. Not Null Constraint:

  • Definition: A not null constraint ensures that a column cannot contain null values.
  • Characteristics:
    • It enforces the presence of a value in a specified column.
    • Any data that is inserted must have a value in order for the SQL transaction to complete. The value for that column cannot be of NULL value.
    • This constraint is set depending on the business logic of an application where a value is needed to be stored

Example:

Constraints ensure that data adheres to specified rules, preventing inconsistencies and errors. When designing a database schema, careful consideration of constraints is crucial to creating a robust and accurate data model.