Introduction
The relational model is a foundational concept in database management, introduced by E.F. Codd in 1970. It organizes data into tables (also known as relations) which can be easily manipulated using a structured query language (SQL). This module will cover the basic principles of the relational model, its components, and how it is used to manage data efficiently.
Key Concepts
- Tables (Relations)
- Definition: A table is a collection of related data entries and consists of rows and columns.
- Structure:
- Rows (Tuples): Each row in a table represents a single record.
- Columns (Attributes): Each column represents a specific attribute of the data.
- Schema
- Definition: The schema of a table defines its structure, including the table name, column names, and data types.
- Example:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), BirthDate DATE, Position VARCHAR(50) );
- Keys
- Primary Key: A unique identifier for each record in a table.
- Example:
EmployeeID
in theEmployees
table.
- Example:
- Foreign Key: A field in one table that uniquely identifies a row of another table.
- Example:
DepartmentID
in theEmployees
table that referencesDepartmentID
in theDepartments
table.
- Example:
- Relationships
- One-to-One: Each row in Table A is linked to one and only one row in Table B.
- One-to-Many: Each row in Table A can be linked to multiple rows in Table B.
- Many-to-Many: Rows in Table A can be linked to multiple rows in Table B and vice versa.
- Integrity Constraints
- Entity Integrity: Ensures that each table has a primary key and that the key is unique and not null.
- Referential Integrity: Ensures that foreign keys correctly and consistently reference primary keys in other tables.
Practical Example
Creating Tables and Defining Relationships
-
Create
Departments
Table:CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(50) );
-
Create
Employees
Table with Foreign Key:CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), BirthDate DATE, Position VARCHAR(50), DepartmentID INT, FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) );
Inserting Data
-
Insert Data into
Departments
Table:INSERT INTO Departments (DepartmentID, DepartmentName) VALUES (1, 'Human Resources'), (2, 'Engineering'), (3, 'Marketing');
-
Insert Data into
Employees
Table:INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate, Position, DepartmentID) VALUES (101, 'John', 'Doe', '1980-01-15', 'Manager', 1), (102, 'Jane', 'Smith', '1985-03-22', 'Engineer', 2), (103, 'Emily', 'Jones', '1990-07-30', 'Marketer', 3);
Querying Data
-
Retrieve All Employees:
SELECT * FROM Employees;
-
Join
Employees
andDepartments
Tables:SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Exercises
Exercise 1: Create and Populate Tables
-
Create a
Projects
Table:CREATE TABLE Projects ( ProjectID INT PRIMARY KEY, ProjectName VARCHAR(100), StartDate DATE, EndDate DATE );
-
Insert Data into
Projects
Table:INSERT INTO Projects (ProjectID, ProjectName, StartDate, EndDate) VALUES (1, 'Project Alpha', '2023-01-01', '2023-06-30'), (2, 'Project Beta', '2023-02-01', '2023-12-31');
Exercise 2: Define Relationships and Query Data
-
Add a Foreign Key to
Employees
Table:ALTER TABLE Employees ADD ProjectID INT, ADD FOREIGN KEY (ProjectID) REFERENCES Projects(ProjectID);
-
Insert Data with Foreign Key:
UPDATE Employees SET ProjectID = 1 WHERE EmployeeID = 101; UPDATE Employees SET ProjectID = 2 WHERE EmployeeID = 102;
-
Query Employees and Their Projects:
SELECT Employees.FirstName, Employees.LastName, Projects.ProjectName FROM Employees JOIN Projects ON Employees.ProjectID = Projects.ProjectID;
Solutions
-
Solution for Exercise 1:
CREATE TABLE Projects ( ProjectID INT PRIMARY KEY, ProjectName VARCHAR(100), StartDate DATE, EndDate DATE ); INSERT INTO Projects (ProjectID, ProjectName, StartDate, EndDate) VALUES (1, 'Project Alpha', '2023-01-01', '2023-06-30'), (2, 'Project Beta', '2023-02-01', '2023-12-31');
-
Solution for Exercise 2:
ALTER TABLE Employees ADD ProjectID INT, ADD FOREIGN KEY (ProjectID) REFERENCES Projects(ProjectID); UPDATE Employees SET ProjectID = 1 WHERE EmployeeID = 101; UPDATE Employees SET ProjectID = 2 WHERE EmployeeID = 102; SELECT Employees.FirstName, Employees.LastName, Projects.ProjectName FROM Employees JOIN Projects ON Employees.ProjectID = Projects.ProjectID;
Conclusion
In this module, we covered the fundamental concepts of the relational model, including tables, schemas, keys, relationships, and integrity constraints. We also provided practical examples and exercises to help solidify your understanding. Mastering these concepts is crucial as they form the backbone of relational database management systems (RDBMS). In the next module, we will delve into the SQL language, which is used to interact with relational databases.
Fundamentals of Databases
Module 1: Introduction to Databases
Module 2: Relational Databases
Module 3: Non-Relational Databases
- Introduction to NoSQL
- Types of NoSQL Databases
- Comparison between Relational and Non-Relational Databases
Module 4: Schema Design
- Principles of Schema Design
- Entity-Relationship (ER) Diagrams
- Transformation of ER Diagrams to Relational Schemas