>_
EngineeringNotes
Back to DBMS Topics

Relational Model

The fundamental architecture of modern SQL databases. Discover how data is structured in relations (tables), what defines a valid row and column, and how different keys ensure data integrity across your entire database.

1

Introduction

The Relational Model (RM) organises data in the form of relations (which we commonly refer to as tables).

Crucial Terminologies

  • Database: A relational DB consists of a collection of tables, each assigned a unique name.
  • Row (Record): Represents a relationship among a set of values. The whole table is a collection of such relationships.
  • Tuple: The formal name for a single row representing a single data point (a unique record).
  • Columns (Attributes): Represents the properties/features of the relation.
  • Domain: The set of permitted values for a specific attribute. For example, the domain of an attribute `age` might only be positive integers.
  • Relation Schema: Defines the design and structure of the relation. It contains the name of the relation (table) and all its columns/attributes.
  • Degree: The total number of attributes (columns) in a given table.
  • Cardinality: The total number of tuples (rows) in a given relation.
  • Relational Key: A set of attributes which can uniquely identify each tuple.

Common RDBMS Systems: Oracle, IBM DB2, MySQL, PostgreSQL, Microsoft SQL Server, MS Access.

2

Properties of a Table

In the relational model, tables must adhere to a strict set of rules to be considered valid relations:

  • Distinct Names: The name of a relation must be entirely unique among all other relations in the database.
  • Atomic Values: Every value within a cell must be atomic. It cannot be broken down further (No arrays or objects inside a single cell).
  • Unique Attributes: The name of each attribute (column) within a single table must be unique.
  • Unique Tuples: Each tuple (row) must be unique in a table. Completely identical duplicate rows are not allowed.
  • No Significance to Sequence: The order of rows and the order of columns have no significance. The data means the exact same thing regardless of its visual sorting.
  • Integrity Constraints: Tables must follow defined rules (constraints) to help maintain data consistency across the database.

Relational Model Keys

Keys are arguably the most important concept in relational databases. They are mechanisms used to uniquely identify rows and to establish relationships between tables.

The Identification Hierarchy: SK → CK → PK

1. Super Key (SK)

Any Permutation & Combination (P&C) of attributes present in a table which can uniquely identify each tuple. It can have redundant/unnecessary attributes.

Example: {Employee_ID}, or {Employee_ID, Name}, or {Employee_ID, Name, Dept}

2. Candidate Key (CK)

The minimum subset of super keys which can uniquely identify each tuple. It contains no redundant attributes. A Candidate Key value shouldn't be NULL.

Example: {Employee_ID} and {Email} can both be CKs.
However, {Employee_ID, Name} is NOT a CK because Name is redundant.

3. Primary Key (PK)

The one specific Candidate Key chosen by the database designer to act as the principal means of identifying rows. It is selected out of the CK set, usually preferring the one with the least number of attributes.

Example: Out of CKs {Employee_ID} and {Email}, we choose {Employee_ID} as the Primary Key.

4. Alternate Key (AK)

All the Candidate Keys that were NOT chosen to be the Primary Key. In mathematical terms: AK = All CKs except PK.

Example: If {Employee_ID} is the PK, then {Email} becomes the Alternate Key.

5. Foreign Key (FK) : The Relationship Builder

A Foreign Key is an attribute in one relation (table) that refers to the Primary Key of another relation. It is what creates the relationships between two tables, enabling the cross-referencing of data.

  • A relation r1 may include among its attributes the PK of an another relation r2. This attribute is called the FK in r1 referencing r2.
  • The relation r1 (which holds the FK) is the Referencing (Child) relation.
  • The relation r2 (which holds the original PK) is the Referenced (Parent) relation.
Foreign Key Example
Department (Parent)
Dept_ID (PK)Dept_Name
Employee (Child)
Emp_ID (PK)NameDept_ID (FK)

The Dept_ID inside the Employee table acts as a pointer to the Dept_ID primary key in the Department table.

Specialized Keys

6. Composite Key

A Primary Key that is formed using at least 2 attributes. Used when a single column is not enough to guarantee uniqueness.

Ex: {Class_Grade, Roll_Number} together make a composite PK for a student table.

7. Compound Key

A type of Composite Key, but specifically, a Primary Key which is formed using two or more Foreign Keys.

Ex: A Many-To-Many relationship table `Enrollments` where PK is {Student_ID (FK), Course_ID (FK)}.

8. Surrogate Key

A completely synthetic / artificial Primary Key. It has no meaning or relation to the actual row data. It is usually an integer value generated automatically by the Database (like an auto-incrementing ID).

Ex: An auto-generated sequential `id` column that increments 1, 2, 3... just to provide an easy unique identifier.

Integrity & Key Constraints

CRUD (Create, Read, Update, Delete) operations must be done with an integrity policy so that the database is always consistent. Constraints are introduced so we do not accidentally corrupt the DB with bad data.

1. Domain Constraints

Restricts the value in the attribute, specifying its valid Domain (Data Type and rules).

  • Restricts the Data types of every attribute (e.g., must be integer).
  • Example: We want to specify that enrollment in a program should only happen if the candidate's birth year < 2002.

2. Entity Constraints

Every relation should have a Primary Key, and the Primary Key CANNOT be NULL.

If a PK was null, we couldn't reliably identify that row. Therefore, PK != NULL is the cardinal rule of entity constraints.

3. Referential Constraints

Specified between two relations. It helps maintain consistency among tuples of two relations (Parent-Child relationship).

  • It requires that the value appearing in the specified attribute of the referencing (child) relation must also exist in the referenced (parent) relation.
  • The Golden Rule of FKs: The Foreign Key must have a matching Primary Key for its each value in the parent table... OR it must be NULL.
    (If it is NULL, it means the relationship simply hasn't been assigned yet, which is valid. But it cannot hold an ID that doesn't exist in the parent).

The 6 Specific Key Constraints in SQL

NOT NULL

Restricts the user from inserting a NULL value. Ensures that every element in the database for that column has a value.

UNIQUE

Ensures that all values present in a specific column are completely different from each other. No duplicates allowed.

DEFAULT

Used to provide a default value for a column. This value is added automatically if no value is explicitly provided during row insertion.

CHECK

Keeps the check that data integrity is maintained before and after CRUD completion. (Testing a condition, e.g., Price > 0).

PRIMARY KEY

An attribute (or set of attributes) that uniquely idenfity each entity. By definition, a PK must contain UNIQUE values and must NOT NULL.

FOREIGN KEY

Creates a relationship between two entities. Represents a common attribute. This key prevents actions which could result in a loss of connection (orphaned records) between tables.

Transform: ER Model to Relational Model

Because both the ER-Model and the Relational Model are abstract logical representations of real-world enterprises built on similar design principles, we can convert ER designs directly into Relational (table) designs. This is how we arrive at a valid database schema from an ER diagram.

1. Strong Entity

  • Becomes an individual table with the entity name. Attributes become columns.
  • Entity's Primary Key (PK) becomes the Relation's PK.
  • Foreign Keys (FK) are added to establish relationships with other relations.

// ER Notation: Student(RollNo, Name)
Table: Student(RollNo, Name)

2. Weak Entity

  • A table is formed with all the attributes of the entity.
  • The PK of its corresponding Strong Entity is added as an FK.
  • The PK of this new relation will be a composite PK: {FK + Partial Discriminator Key}.

// Strong: Loan, Weak: Payment
Table: Payment(Loan_No (FK), Instalment_No, Date)

Attribute Transformations

3. Single Values

Represented as columns directly in the tables/relations.

4. Composite Attributes

Handled by creating a separate column for each composite sub-attribute. The original parent attribute is completely ignored.

// Address has {street-name, house-no}
Customer(Cust_ID, address-street-name, address-house-no)

5. Multivalued Attributes

New tables are created for each multivalued attribute (named as the original attribute). The PK of the entity is used as a column (FK) in the new table. The PK of this new table is composite: {FK + multivalued name}.

// Employee has multivalued dependent-name
New Table: Dependent-Name(emp-id (FK), dname)

6. Derived Attributes

Not considered in the relational tables at all (computed dynamically at query time).

7. Generalisation (Is-A)

Method 1 (Recommended)

Create table for higher-level entity, and tables for lower-level entities containing the PK of higher-level + their specific attributes.

Account(acc_no, balance)
Savings(acc_no, interest_rate)
Current(acc_no, overdraft)

Method 2

Create tables ONLY for lower-level entities containing all attributes of higher-level entities.

Savings(acc_no, balance, int_rate)
Current(acc_no, balance, overdraft)

Drawback: Data duplication for overlapping entities (balance is stored twice), and cannot store generic accounts that are neither Savings nor Current.

8. Aggregation

When a relationship set is treated as an abstract entity to participate in another relationship.

  • A table of the relationship set is made.
  • Attributes of this table include the primary keys of the entity set AND the aggregation set's entities.
  • Also, add any descriptive attributes directly defined on the relationship.

// Example where Evaluation relates to the (Student-Project) aggregation
Table: Evaluation(Student_ID (FK), Project_ID (FK), Evaluator_ID (FK), Grade)