The Relational Model — Mathematical Foundations
Relational Model — গাণিতিক ভিত্তি
1. Why Math? — The Calm Power of the Relational Model
Codd was a mathematician before he was a database researcher. His genius was to define a database in pure set theory: a database is a collection of relations, a relation is a set of tuples, and SQL operations correspond to operations on sets. This is not academic decoration — it is the reason the optimizer can rewrite your query, why duplicates can be removed safely, and why a query gives the same answer regardless of the order rows happen to be stored on disk.
In this module we tie every SQL feature back to its math. We will define domain,
attribute, tuple, relation; meet four kinds of
keys; and write our first CREATE TABLE statements that put these constraints
to work in real, runnable SQLite.
2. Domain, Attribute, Tuple, Relation
Four definitions, in increasing order of complexity. Memorise them — every database book in the world uses this exact vocabulary.
| Math term | SQL term | Meaning | বাংলায় |
|---|---|---|---|
| Domain | Data type / CHECK | The set of legal values for an attribute (e.g. INTEGER, or CGPA between 0.0 and 4.0). | একটি attribute-এর জন্য বৈধ মানের সেট। |
| Attribute | Column | A named slot in each tuple; has a domain. | Column — প্রতিটি tuple-এর একটি নামকৃত স্লট। |
| Tuple | Row | An ordered set of attribute values; one record. | Row — attribute মানের একটি কপি। |
| Relation | Table | A set of tuples — therefore no duplicates and no inherent order. | Tuple-এর set — duplicate নয়, কোনো নির্দিষ্ট order নয়। |
| Relation schema | CREATE TABLE … | The header: name and attributes with domains. | Table-এর design। |
| Relation instance | The current rows | The actual set of tuples right now. | এই মুহূর্তের actual rows। |
Notice the most subtle point: a relation is a set. Sets do not have duplicates, and sets do not
have order. This is why SELECT without ORDER BY may return rows in any order, and
why pure relational theory disallows duplicate rows. SQL relaxes both rules in practice — but the math is
the foundation.
3. Keys — Super, Candidate, Primary, Foreign
A key is the single most important idea in relational design. It is what lets a row be
uniquely identified — without keys, you cannot meaningfully UPDATE, DELETE, or
JOIN. There are four flavours, each a refinement of the previous.
| Key type | Definition | Example (student) | বাংলায় |
|---|---|---|---|
| Super key | Any set of attributes that uniquely identifies a tuple — possibly with extras. | {roll}, {roll, name}, {roll, name, cgpa} | যেকোনো attribute-set যা row uniquely identify করে। |
| Candidate key | A minimal super key — remove any attribute and uniqueness breaks. | {roll}, {nid} | সবচেয়ে ছোট super key — যেকোনো একটিও বাদ দিলে uniqueness ভাঙে। |
| Primary key | The candidate key the designer chose; cannot be NULL. | roll | Designer যে candidate key বেছে নিলেন; NULL হতে পারে না। |
| Alternate key | A candidate key that was not chosen as primary. | nid | যে candidate key primary হয়নি। |
| Foreign key | An attribute in one table whose values must match a primary key in another (or be NULL). | student.did → department.did | একটি table-এর attribute যা অন্য table-এর primary key-কে রেফার করে। |
Below we declare every key type in one schema and run a query that walks the foreign-key chain. Notice the
UNIQUE on nid — it is an alternate candidate key, even though we chose roll
as primary.
-- Walk foreign keys: each student → department
SELECT s.roll, s.name, s.nid,
d.dname AS dept, s.cgpa
FROM student s
JOIN department d ON d.did = s.did
ORDER BY s.cgpa DESC;
4. Why a Relation Cannot Have Duplicate Tuples
A pure relation is a set. Sets, by definition, do not contain duplicates: {a, a, b}
and {a, b} are the same set. Therefore in the relational model, two identical rows cannot
both exist.
By declaring a primary key, you make duplicates of the key impossible. The DBMS rejects them at insert time. This is how the set property of a relation gets enforced concretely. Without a primary key, SQL does permit duplicates (a "bag," not a set) — which is one of the small ways SQL deviates from pure theory.
Primary key declare করলেই duplicate insert আটকে যায় — এভাবেই বিমূর্ত set-এর গুণ বাস্তবে চাপানো হয়।
-- The DBMS rejects this — set semantics in action
INSERT INTO student VALUES (101, 'Mahmuda');
SELECT * FROM student;
5. Integrity Constraints — Three Shields That Protect the Data
An integrity constraint is a rule that the DBMS enforces on every INSERT,
UPDATE, or DELETE. There are three classical kinds, and every modern SQL dialect
implements all three.
| Type | What it protects | SQL keyword | বাংলায় |
|---|---|---|---|
| Entity integrity | Every tuple must be uniquely identifiable; primary key cannot be NULL. | PRIMARY KEY | প্রতিটি row uniquely চেনা যাবে; PK NULL হবে না। |
| Referential integrity | A foreign-key value must match an existing primary key (or be NULL). | FOREIGN KEY … REFERENCES | FK-এর মান অন্য table-এর PK-তে থাকতে হবে। |
| Domain integrity | Every attribute value must be inside its declared domain. | CHECK, types, NOT NULL | প্রতিটি মান তার declared domain-এর মধ্যে থাকবে। |
Below is a single schema demonstrating all three. Try to break any rule — the DBMS will reject your attempt:
-- 1. Entity integrity — primary key must exist and be unique
INSERT INTO account VALUES ('A1001', 'Rahim Uddin', 15000, 1);
-- 2. Domain integrity — CHECK rejects negative balance
-- The next line will fail.
INSERT INTO account VALUES ('A1002', 'Karim', -100, 2);
SELECT * FROM account;
-- 3. Referential integrity — branch 99 does not exist, so FK fails
INSERT INTO account
VALUES ('A2001', 'Salma', 50000, 99);
SELECT * FROM account;
6. Relation Schema vs Relation Instance — Same Idea, Stricter Words
We met schema vs instance in Module 03. In the relational model these terms get a little more precise.
📐 Relation schema R(A₁, A₂, …, Aₙ)
The name of the relation plus its ordered list of attributes with domains. Mathematically: a header.
student(roll: INT, name: TEXT, dept: TEXT, cgpa: REAL ∈ [0,4])
🧾 Relation instance r(R)
The set of tuples currently in the table — each tuple has values from the matching domains.
{(101, 'Mahmuda', 'CSE', 3.78), (102, 'Tanvir', 'EEE', 3.20)}
7. SQL — How the Math Becomes a Language
Every line of SQL is a relational-model operation in disguise. Let us read three SQL constructs through the relational lens, so the rest of this course feels less like memorising syntax and more like applying theory.
CREATE TABLE student (roll INT PRIMARY KEY, …)— "Define a relation schema with these attributes and this entity-integrity constraint."SELECT name FROM student WHERE cgpa > 3.5— "From the relationstudent, project thenameattribute, restricted to tuples whosecgpa > 3.5." (Pure relational algebra.)FROM student JOIN department ON student.did = department.did— "The natural join of two relations on equaldidvalues."
Run a one-shot example that ties it all together — model a simple e-commerce order line in pure relational terms. Notice every constraint and key:
-- π_(district, total) σ_(year=2025) (orders ⋈ customer ⋈ product)
SELECT c.district,
SUM(o.qty * p.price) AS total_revenue
FROM orders o
JOIN customer c ON c.cid = o.cid
JOIN product p ON p.pid = o.pid
GROUP BY c.district
ORDER BY total_revenue DESC;
Every
SELECT … FROM … WHERE … can be re-written as a chain of relational-algebra operators:
σ (select rows), π (project columns), ⋈ (join), ∪ (union), − (difference). The optimizer literally does
this rewriting under the hood to choose a fast plan.প্রতিটি SELECT-WHERE-JOIN আসলে σ-π-⋈-এর শৃঙ্খল। Optimizer এই শৃঙ্খলটিই rewrite করে দ্রুততম plan বেছে নেয়।
8. Glossary (শব্দকোষ)
| Term | Meaning | বাংলায় |
|---|---|---|
| Domain | Set of legal values for an attribute. | একটি attribute-এর বৈধ মানের সেট। |
| Attribute | A named column. | নামকৃত column। |
| Tuple | One row. | এক row। |
| Relation | A set of tuples — a table without duplicates. | Tuple-এর set। |
| Super key | Any uniquely-identifying attribute set. | যেকোনো unique-করা attribute-set। |
| Candidate key | Minimal super key. | সবচেয়ে ছোট super key। |
| Primary key | The chosen candidate key — never NULL. | বেছে নেওয়া candidate key — NULL নয়। |
| Foreign key | An attribute referencing another relation's primary key. | অন্য relation-এর PK-কে রেফার করা attribute। |
| Entity integrity | PK exists & is unique. | PK থাকতে হবে ও unique হবে। |
| Referential integrity | FK matches an existing PK. | FK অন্য PK-তে থাকতে হবে। |
| Domain integrity | Values stay inside declared domain. | মান domain-এর বাইরে যাবে না। |
9. Practice Problems
Twelve problems — from definitions to runnable SQL. Try each before peeking.
-
Define domain, attribute, tuple, and relation in one sentence each.এক বাক্যে বলুন: domain, attribute, tuple, relation।
✨ Show Answer
Answer: A domain is the set of legal values for one attribute. An attribute is a named column whose values come from a domain. A tuple is one ordered set of attribute values — a row. A relation is a set of tuples sharing one schema — a table.
Domain — attribute-এর বৈধ মানের সেট; attribute — column; tuple — row; relation — tuple-এর set।
-
For an e-commerce
order(oid, cid, pid, qty, order_date)table, list one super key, one candidate key, one primary key, and any foreign keys.order(oid, cid, pid, qty, order_date)— একটি super key, একটি candidate key, একটি primary key এবং foreign key চিহ্নিত করুন।✨ Show Answer
Answer: Super key example:
{oid, cid}. Candidate key:{oid}. Primary key:oid. Foreign keys:cid→customer.cid,pid→product.pid.Super key:
{oid, cid}; Candidate key:{oid}; Primary key:oid; FK:cid → customer.cid,pid → product.pid। -
Write
CREATE TABLEfor an NID-style table with a 13-character national ID as primary key, a non-null name, and a year-of-birth domain check (1900–2025).NID-এর জন্য table বানান: ১৩-অক্ষরের NID primary key, name NOT NULL, year-of-birth domain check (1900–2025)।✨ Show Answer
ans3.sqlCREATE TABLE citizen ( nid TEXT PRIMARY KEY CHECK (length(nid) = 13), name TEXT NOT NULL, yob INTEGER NOT NULL CHECK (yob BETWEEN 1900 AND 2025) ); INSERT INTO citizen VALUES ('1990123456789', 'Mahmuda Khatun', 1990); SELECT * FROM citizen; -
Show that domain integrity stops a 14-character NID from being inserted into the table above.দেখান, ১৪-অক্ষরের NID উপরের table-এ ঢুকতে পারবে না।
✨ Show Answer
ans4.sql-- 14-character NID — domain check fails INSERT INTO citizen VALUES ('19901234567890', 'Bad Row', 1990); SELECT * FROM citizen; -
Define entity integrity, referential integrity, and domain integrity, with one bKash example each.প্রতিটি integrity-র সংজ্ঞা ও bKash থেকে একটি উদাহরণ দিন।
✨ Show Answer
Answer:
- Entity: Every wallet has a unique non-null msisdn.
- Referential: A transaction's
sender_msisdnmust exist in the wallet table. - Domain:
amount > 0— no negative-amount transfers.
Entity — প্রতিটি wallet-এর unique msisdn। Referential — txn-এর sender_msisdn আগে wallet-এ থাকতে হবে। Domain — amount > 0।
-
Try to insert a duplicate primary key and observe the error.Duplicate primary key insert করার চেষ্টা করুন।
✨ Show Answer
ans6.sqlINSERT INTO wallet VALUES ('01711000001', 'Different Owner'); SELECT * FROM wallet; -
Why does a "relation" not have a built-in row order? Give one practical consequence.Relation-এর কোনো built-in order কেন নেই? এর একটি practical ফলাফল দিন।
✨ Show Answer
Answer: Because a relation is a set, and sets are unordered. Practical consequence: a
SELECTwithoutORDER BYmay return rows in any order, and the optimizer is free to read them in whatever sequence is fastest.Relation একটি set; set-এ order নেই। ফলাফল:
ORDER BYছাড়াSELECTযেকোনো order-এ row দিতে পারে — optimizer যা দ্রুত মনে করে সেটাই বেছে নেয়। -
Explain the difference between a candidate key and a primary key. Can a relation have more than one candidate key?Candidate key ও primary key-এর পার্থক্য কী? একটি relation-এ একাধিক candidate key থাকতে পারে?
✨ Show Answer
Answer: A candidate key is any minimal super key. A relation may have many — for example a
studentrelation could have both{roll}and{nid}. The designer picks exactly one to be the primary key; the others become alternate keys, often enforced withUNIQUE.Candidate key — যেকোনো minimal super key। একটি relation-এ একাধিক থাকতে পারে। Designer একটিকে primary বাছেন, বাকিগুলো alternate, সাধারণত
UNIQUE-এ enforce করা হয়। -
Build
courseandenrollmenttables (a student can enrol in many courses) and list the courses each student is enrolled in.courseওenrollmenttable বানান (এক student অনেক course-এ ভর্তি হতে পারে) এবং প্রতিটি student-এর course list করুন।✨ Show Answer
ans9.sqlSELECT s.name, c.title, e.grade FROM enrollment e JOIN student s ON s.roll = e.roll JOIN course c ON c.cid = e.cid ORDER BY s.name; -
In the schema above, why is the primary key of
enrollmenta composite of(roll, cid)?উপরের schema-এenrollment-এর primary key(roll, cid)composite কেন?✨ Show Answer
Answer: Neither
rollalone norcidalone identifies an enrolment uniquely — a student takes many courses and a course has many students. Their combination is the smallest set that uniquely identifies one enrolment row, so the candidate key is{roll, cid}.শুধু
rollবা শুধুcidদিয়ে এক row চেনা যায় না; দুটি মিলিয়েই unique। তাই composite primary key। -
Demonstrate referential integrity: try to enrol a non-existent student. The DBMS should reject it.Referential integrity দেখান — অনুপস্থিত roll দিয়ে enroll করার চেষ্টা করুন।
✨ Show Answer
ans11.sql-- Roll 999 does not exist — FK should fail INSERT INTO enrollment VALUES (999, 'CSE101'); SELECT * FROM enrollment; -
In one paragraph, explain why "the relational model is built on set theory" actually matters to a working developer.এক অনুচ্ছেদে ব্যাখ্যা করুন — "relational model set theory-র উপর বানানো" — এটি কেন সত্যিকার developer-এর জন্য গুরুত্বপূর্ণ।
✨ Show Answer
Answer: Because set theory makes queries composable and rewritable. The optimizer can re-arrange joins, push filters down, eliminate duplicates, and parallelise — all because every operation has clean mathematical meaning. It is also why SQL is portable: any DBMS that respects the math will compute the same answer for the same query, regardless of which indexes or storage tricks it uses internally.
Set theory-র কারণেই query compose ও rewrite করা যায়। Optimizer JOIN order বদলায়, filter আগে চালায়, duplicate সরায়, parallelise করে — কারণ প্রতিটি operation-এর গণিতগত অর্থ পরিষ্কার। এই কারণেই একই SQL ভিন্ন DBMS-এ একই উত্তর দেয়।
Summary — Module 05
The relational model is just four tidy ideas: a domain is a set of legal values, an attribute is a named column drawing from a domain, a tuple is one row, and a relation is a set of tuples. From these come super, candidate, primary, and foreign keys, and the three classical integrity constraints — entity, referential, and domain — that the DBMS enforces on every write. SQL is the English-like language we use to describe operations on these sets; behind the scenes the optimizer rewrites every query as relational algebra. The rest of this course is, in a sense, an extended workshop on this single mathematical foundation.