The relational model
DBMS — A database management system is the software sitting between users and the stored data. Every read and write goes through it, which is how it can enforce data types and rules, control who may see what, keep the data consistent when two people change it at once, and recover after a failure.
A relational database stores data in tables, each row a record and each column a field. Tables are connected not by pointers or physical links but by shared values — the student's roll number appearing in both the STUDENT table and the ENROLMENT table is what joins them.
That indirection is the whole design. Because the connection is a value rather than a location, tables can be reorganised, rows can move, and the relationship still holds.
| The DBMS provides | Which means |
|---|---|
| Data independence | programs need not know how data is physically stored |
| Integrity enforcement | invalid data is rejected at the door |
| Security | permissions per user, per table, even per field |
| Concurrency control | two simultaneous updates cannot corrupt each other |
| Backup and recovery | the database can be restored after a crash |
| A query language | new questions answered without new programs |
Keys
Keys are how rows are identified and how tables are joined, and the exam distinguishes several kinds.
A primary key uniquely identifies each record in a table. It must be unique, must never be empty, and should never change. A foreign key is a field in one table holding the primary key of another — that is the link. A candidate key is any field that could serve as the primary key; one is chosen and the rest become alternate keys. A composite key is a primary key made of two or more fields together, needed when no single field is unique.
On One to many, the foreign key goes on the "many" side — the class's key is stored in the student table, not the reverse. On Many to many, notice it cannot be done with two tables at all.
Why a name is never a primary key
Names are not unique — two students called Ali Khan is not a hypothetical. They also change, on marriage or by correction, and a changing primary key breaks every foreign key referring to it. A good primary key is meaningless: a roll number or an auto-generated ID has no reason ever to change, which is exactly what makes it safe.
Relationships
Two entities are related in one of three ways, and identifying which decides the table design.
One to one: each record on one side matches at most one on the other. Genuinely rare — usually the two entities should be one table, unless there is a reason to separate them such as differing access permissions.
One to many: the common case. One class contains many students; each student is in one class. Implemented by putting the "one" side's primary key into the "many" side's table as a foreign key.
Many to many: a student studies many subjects and each subject has many students. This cannot be represented with two tables and must be broken into two one-to-many relationships using a third linking table.
A library has BOOK and MEMBER entities. A member may borrow many books over time, and a book may be borrowed by many members over time. Design the tables.
- Identify the relationship: many to many, because both sides are "many" over time.The phrase "over time" is what makes it many-to-many rather than one-to-many.
- A many-to-many relationship needs a linking table, so create LOAN.Neither BOOK nor MEMBER can hold a list of the other.
- BOOK(BookID, Title, Author) and MEMBER(MemberID, Name, Address).Each has a meaningless, stable primary key.
- LOAN(LoanID, BookID, MemberID, DateOut, DateDue).BookID and MemberID are foreign keys; the extra fields belong to the loan itself, not to the book or the member.
- BOOK to LOAN is one to many, and MEMBER to LOAN is one to many.The many-to-many has become two one-to-many relationships, which tables can express.
Three tables: BOOK, MEMBER, and a LOAN linking table holding both foreign keys plus the loan dates.
Where the extra fields go
A linking table is not merely a pair of keys. Any attribute belonging to the relationship itself rather than to either entity lives there.
The date a book was borrowed is not a property of the book, which exists before and after the loan, and not a property of the member. It is a property of that particular loan. Likewise a grade belongs to a student's enrolment in a subject, not to the student and not to the subject. Recognising which attributes belong to the relationship is a standard exam question, and putting them in the wrong table is the standard error.
Before you leave this chapter
- Tables are joined by shared values, not by physical links.
- A DBMS gives independence, integrity, security, concurrency control, recovery and a query language.
- Primary key: unique, never empty, never changing. A name is never suitable.
- A foreign key holds another table's primary key, and lives on the "many" side.
- Many to many needs a linking table, which also holds any attribute of the relationship itself.