A3.2.4 Construct tables for relational databases.

A3.2.4 Construct tables for relational databases. 
• The relationship between tables using primary keys, foreign keys, composite keys and concatenated keys 
• The importance of well-defined tables in ensuring data integrity

 

The Big Idea

In relational databases, data is stored in tables—structured collections of rows and columns where each column has a defined data type and each row represents a unique record. Designing these tables correctly is foundational to building efficient, scalable, and accurate data systems. A well-constructed table enforces integrity, eliminates redundancy, and enables powerful relational operations such as joins and constraints.

At the heart of this structure are keys, which uniquely identify records and define relationships between tables.


Understanding Keys

A key in a relational database is a column (or a group of columns) used to identify, link, or constrain records in a table. Keys are essential to ensuring that records remain unique and relationships between tables are valid and consistent.

1. Primary Key

A primary key is a field (or combination of fields) that uniquely identifies each row in a table. Every table should have exactly one primary key.

  • It must be unique: no two rows can have the same value for the primary key.
  • It must be not null: every row must have a value in the primary key field.

Example:

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    grade_level INT
);

Here, student_id is the primary key. It guarantees that each student is stored once and only once.


2. Foreign Key

A foreign key is a field in one table that refers to the primary key in another table. It creates a relationship between two tables, enforcing referential integrity.

  • It ensures that values in the foreign key column correspond to valid entries in the referenced table.
  • It prevents orphaned records—rows that reference data that doesn’t exist.

Example:

CREATE TABLE enrollments (
    enrollment_id INT PRIMARY KEY,
    student_id INT,
    course_code VARCHAR(10),
    FOREIGN KEY (student_id) REFERENCES students(student_id)
);

Here, student_id is a foreign key that links each enrollment record to a student in the students table.


3. Composite Key

A composite key is a primary key made up of two or more columns. This is useful when no single column alone is sufficient to uniquely identify a row.

Example:

CREATE TABLE course_enrollments (
    student_id INT,
    course_code VARCHAR(10),
    PRIMARY KEY (student_id, course_code)
);

In this example, a student may enroll in multiple courses, and each course may have multiple students. The combination of student_id and course_code ensures uniqueness for each enrollment.


4. Concatenated Key

A concatenated key is another name for a composite key—it "concatenates" multiple fields to form a unique identifier. The terms are sometimes used interchangeably, but in some contexts "concatenated" may emphasize the use of string values or derived keys.

The key idea is the same: a multi-field key that defines uniqueness across a relationship.


The Importance of Well-Defined Tables

Designing tables with clear, consistent, and well-normalized structure supports multiple aspects of data integrity and maintainability:

1. Data Integrity

  • Keys (primary and foreign) enforce entity and referential integrity.
  • Prevents invalid or duplicate records.
  • Constraints ensure that only valid data can be inserted or updated.

2. Avoiding Redundancy

  • Proper relational design using foreign keys eliminates repeated information.
  • Instead of storing full student details in every enrollment, we store the student_id and link it to a single source of truth.

3. Scalability and Query Efficiency

  • Indexed keys allow fast searches and joins.
  • Well-structured schemas support clean queries and better optimizer behavior.

4. Maintainability

  • Easier to update, audit, or change structures when tables are logically organized.
  • Ensures that changes to one table don't lead to unpredictable effects elsewhere.

Conclusion

Constructing tables in a relational database is about more than just creating rows and columns—it’s about designing logical, interconnected structures where data relationships are clearly and consistently defined. By using primary keys, foreign keys, and composite/concatenated keys, we establish a framework that ensures accuracy, prevents errors, and supports complex queries across large systems. The integrity of any relational database rests on the quality of its table design.