| Data type | Typical use | Field‑size limits |
|---|---|---|
| INTEGER | Whole numbers | ‑2 147 483 648 to 2 147 483 647 (4 bytes) |
| SMALLINT | Small whole numbers | ‑32 768 to 32 767 (2 bytes) |
| DECIMAL(p,s) | Fixed‑point numbers (e.g., prices) | p = total digits (max 38), s = digits after decimal (0 ≤ s ≤ p) |
| CHAR(n) | Fixed‑length text | n = 1‑255 characters (padded with spaces) |
| VARCHAR(n) | Variable‑length text | n = 1‑65 535 characters (actual length stored) |
| DATE | Calendar dates | ‘YYYY‑MM‑DD’ (range 1000‑01‑01 to 9999‑12‑31) |
| TIME | Time of day | ‘HH:MM:SS’ (00:00:00‑23:59:59) |
| BOOLEAN | True / False values | 0 = FALSE, 1 = TRUE |
| Aspect | Validation (rules that *prevent* bad data) | Verification (rules that *supply* default/derived data) |
|---|---|---|
| NOT NULL | Disallows missing values | — |
| CHECK (condition) | Ensures values meet a condition (e.g., CHECK (Credits BETWEEN 1 AND 10)) | — |
| UNIQUE | Prevents duplicate values in a column | — |
| DEFAULT value | — | Provides a value when none is entered (e.g., StockQty INT DEFAULT 0) |
CREATE TABLE Course (
CourseID CHAR(6) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Credits SMALLINT CHECK (Credits BETWEEN 1 AND 10)
);
CREATE TABLE StudentCourse (
StudentID CHAR(8),
CourseID CHAR(6),
Semester CHAR(6),
PRIMARY KEY (StudentID, CourseID), -- compound PK
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
| Aspect | Flat‑file | Relational DB |
|---|---|---|
| Structure | Single table (CSV, TXT, spreadsheet) | Multiple related tables (normalised) |
| Data redundancy | High – same data repeated in many rows | Low – PK/FK links minimise duplication |
| Integrity control | Manual checks only | Built‑in constraints (PK, FK, UNIQUE, CHECK) |
| Query capability | Simple filters (e.g., Excel auto‑filter) | Complex queries, joins, sub‑queries, aggregates |
| Concurrency | Single‑user or read‑only sharing | Multi‑user, transaction support |
| Typical use | Very small data sets, log files, data exchange | Any application needing reliable storage, reporting, security |
Transition tip: Start with a conceptual diagram, add attributes to obtain the logical diagram, then translate each entity into a table (adding PK/FK, data types, constraints) to produce the physical diagram.
UNIQUE foreign key in either table; the table holding the optional data usually contains the FK.Example – Student & StudentPassport
CREATE TABLE Student (
StudentID CHAR(8) PRIMARY KEY,
Name VARCHAR(100) NOT NULL
);
CREATE TABLE StudentPassport (
PassportID CHAR(9) PRIMARY KEY,
StudentID CHAR(8) UNIQUE, -- enforces 1:1
PassportNo VARCHAR(20) NOT NULL,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
Example – Department & Employee
CREATE TABLE Department (
DeptID SMALLINT PRIMARY KEY,
DeptName VARCHAR(100) NOT NULL
);
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
DeptID SMALLINT,
EmpName VARCHAR(100) NOT NULL,
FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);
Example – Students enrol in Courses
CREATE TABLE Student (
StudentID CHAR(8) PRIMARY KEY,
Name VARCHAR(100) NOT NULL
);
CREATE TABLE Course (
CourseID CHAR(6) PRIMARY KEY,
Title VARCHAR(100) NOT NULL
);
CREATE TABLE Enrolment ( -- junction table
StudentID CHAR(8),
CourseID CHAR(6),
EnrolDate DATE,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
Below are simplified ASCII sketches – replace with proper drawing tools (e.g., Lucidchart, draw.io) for exam work.
Conceptual (entities only)
--------------------------
[Student] [Course]
| |
+-------+-------+
|
[Enrolment]
Logical (attributes & PK/FK)
----------------------------
[Student] [Course]
PK StudentID PK CourseID
Name Title
[Enrolment]
PK (StudentID,CourseID)
FK StudentID → Student
FK CourseID → Course
EnrolDate
Physical diagrams would add data types, field sizes, and constraints exactly as shown in the CREATE TABLE statements above.
ON DELETE or ON UPDATE:
CASCADE – propagate the change to child rows.SET NULL – replace the foreign key with NULL.RESTRICT (or NO ACTION) – block the change if dependent rows exist.SQL example – cascade delete from Department to Employee
ALTER TABLE Employee
ADD CONSTRAINT FK_Employee_Department
FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
ON DELETE CASCADE
ON UPDATE RESTRICT;
CREATE TABLE statements – include:
INSERT statements or import from CSV/flat files.CREATE TABLE Product (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(80) NOT NULL,
Price DECIMAL(8,2) CHECK (Price >= 0),
StockQty INT DEFAULT 0 CHECK (StockQty >= 0)
);
| Query type | Purpose | Typical SQL construct |
|---|---|---|
| Static query | Returns a fixed result set (no parameters) | SELECT … FROM … WHERE …; |
| Dynamic query | Prompts the user for criteria at run‑time | SELECT … FROM … WHERE Field = [Enter value]; |
| Simple query | One table, no aggregation or joins | SELECT Name, Salary FROM Employee; |
| Complex query | Multiple tables, joins, calculations, sub‑queries | SELECT d.DeptName, COUNT(e.EmpID) AS NoOfStaff
FROM Department d
LEFT JOIN Employee e ON d.DeptID = e.DeptID
GROUP BY d.DeptName; |
| Nested query | Query inside another query (sub‑select) | SELECT * FROM Employee
WHERE DeptID IN (SELECT DeptID FROM Department WHERE DeptName='HR'); |
| Summary (aggregate) query | Uses aggregate functions (SUM, AVG, MIN, MAX, COUNT) | SELECT AVG(Price) AS AvgPrice FROM Product; |
| Cross‑tab (pivot) query | Displays aggregated data in a matrix format | In Access: Query Design → Crosstab or SQL Server PIVOT syntax |
WHERE to filter rows; it is evaluated before GROUP BY or HAVING.WHERE, JOIN, or ORDER BY dramatically improve speed.WHERE over HAVING when the condition does not involve aggregates.SELECT * FROM Employee
ORDER BY DeptID ASC, Salary DESC;
-- Find duplicates based on Name and DOB
SELECT Name, DOB, COUNT(*) AS DupCount
FROM Student
GROUP BY Name, DOB
HAVING COUNT(*) > 1;
-- Delete duplicates, keeping the earliest StudentID
DELETE s1
FROM Student s1
JOIN Student s2
ON s1.Name = s2.Name
AND s1.DOB = s2.DOB
AND s1.StudentID > s2.StudentID;
Price * Quantity AS LineTotalCASE WHEN Qty > 0 THEN 'In stock' ELSE 'Backorder' END AS StockStatusFirstName + ' ' + LastName AS FullNameCREATE TABLE statements with PK, FK, UNIQUE, NOT NULL, CHECK and DEFAULT?ON DELETE/UPDATE actions?Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.