Chapterبەش 3 · Lectureوانە 3.4
From EER Diagrams to Tablesلە هێڵکاریی EERـەوە بۆ خشتەکان
Turning the Enhanced E-R model into SQL tables: recursive relationships as foreign keys to the same table, ternary relationships, three ways to store an ISA hierarchy, categories and aggregation, and counting tables — every example run in SQLite, then a hospital, a package-delivery company and an online market designed from start to finish.
گۆڕینی بۆ : وەک بۆ هەمان ، ، سێ ڕێگە بۆ هەڵگرتنی پلەبەندییەکی ISA، و و ژماردنی — هەموو نموونەیەک لە SQLite جێبەجێ کراوە، پاشان نەخۆشخانەیەک، کۆمپانیایەکی گەیاندنی پاکەت و بازاڕێکی ئۆنلاین لە سەرەتاوە تا کۆتایی دیزاین دەکرێن.
What you will learnچی فێر دەبیت
- Turn recursive and ternary relationships into tables with the right foreign keys and primary key.
- Compare the three ways of storing an ISA hierarchy and choose one for a given specialisation.
- Store categories and aggregations, and count the tables a diagram needs.
- Design a complete database from written rules: diagram, tables, keys and checks in SQL.
- و بکەیتە بە و دروست.
- سێ ڕێگەی هەڵگرتنی پلەبەندییەکی ISA بەراورد بکەیت و یەکێکیان بۆ هەڵبژێریت.
- و هەڵبگریت و بژمێریت هێڵکارییەک چەند پێویستە.
- تەواو لە یاسا نووسراوەکانەوە دیزاین بکەیت: هێڵکاری، ، و پشکنین بە .
From diagram to tablesلە هێڵکارییەوە بۆ خشتەکان
Beyond the Basic E-R Model drew recursive and ternary relationships, (min,max) limits, ISA hierarchies, categories and aggregation. A DBMS stores only tables, so every one of these must become tables, keys and constraints. This lesson does that in SQL, runs every example in SQLite, and ends with three complete design tasks.
وانەی زیاتر لە مۆدێلی قەوارە و پەیوەندیی بنەڕەتی و ، سنووری (min,max)، پلەبەندیی ISA، و کێشا. تەنها هەڵدەگرێت، بۆیە هەر یەک لەمانە دەبێت ببێتە ، و . ئەم وانەیە ئەمە بە دەکات، هەموو نموونەیەک لە SQLite جێبەجێ دەکات و بە سێ ئەرکی دیزاینی تەواو کۆتایی دێت.
The basic rules for entities, 1:N, M:N and multi-valued attributes are in From E-R diagram to tables; keys and foreign keys are in Database keys. Read those first.
یاسا بنەڕەتییەکان بۆ ، 1:N، M:N و لە شاشەی گۆڕینی هێڵکاریی قەوارە و پەیوەندی بۆ خشتەکان هەن؛ و لە کلیلەکانی بنکەدراوەدان. سەرەتا ئەوانە بخوێنەوە.
From EER to tables: the rulesلە EERـەوە بۆ خشتەکان: یاساکان
The basic rules are in From E-R diagram to tables: an entity becomes a table, a 1:N relationship becomes a foreign key on the N side, an M:N relationship becomes a junction table, and a multi-valued attribute gets a table of its own. The EER constructs add these:
یاسا بنەڕەتییەکان لە شاشەی گۆڕینی هێڵکاریی قەوارە و پەیوەندی بۆ خشتەکان هەن: دەبێتە ، دەبێتە لە لای N، دەبێتە و تایبەت بە خۆی وەردەگرێت. پێکهاتەکانی EER ئەمانە زیاد دەکەن:
| In the EER diagramلە هێڵکاریی EERـدا | In the tablesلە |
|---|---|
Recursive 1:N 1:N | A foreign key to the same table: staff.supervisor_no → staff بۆ هەمان : staff.supervisor_no بۆ staff |
Recursive M:N M:N | A table with two foreign keys to the same table: prerequisite(course_code, prereq_code) بە دوو بۆ هەمان : prerequisite(course_code, prereq_code) |
| Ternary | A table with the three keys as foreign keys; its key is all three, or two of them when one side has a 1 کە هەر سێ وەک لەخۆ دەگرێت؛ هەر سێکیانە، یان دووانیان کاتێک لایەک 1ـی هەبێت |
| Attribute of a relationship | A column of the relationship's table: grade, price_iqd, qty : grade، price_iqd، qty |
| ISA (ISA) | Three options, next screensسێ ڕێگە، لە شاشەکانی داهاتوودا |
| Category (union type) | A table for the category with a surrogate key; each superclass table gets that key as a foreign key (example) بۆ بە ؛ هەر ئەو وەک وەردەگرێت (نموونە) |
| Aggregation | The outer relationship's table references the key of the inner relationship's table دەرەوە ئاماژە بە ناوەوە دەکات |
Recursive: a foreign key to the same tableخۆیی: کلیلێکی بیانی بۆ هەمان خشتە
A recursive 1:N becomes a foreign key that points to the same table. The role becomes the column name: supervisor_no holds the supervisor's staff_no. Hana has no supervisor, so her supervisor_no is NULL.
1:N دەبێتە کە ئاماژە بۆ هەمان دەکات. ڕۆڵەکە دەبێتە ناوی : supervisor_no staff_noـی سەرپەرشتیارەکە هەڵدەگرێت. هانا سەرپەرشتیاری نییە، بۆیە supervisor_noـەکەی NULLـە.
PRAGMA foreign_keys = ON;
CREATE TABLE staff (
staff_no INTEGER PRIMARY KEY,
name TEXT NOT NULL,
supervisor_no INTEGER REFERENCES staff(staff_no) ON DELETE SET NULL
);
INSERT INTO staff VALUES (1, 'Hana', NULL);
INSERT INTO staff VALUES (2, 'Aram', 1);
INSERT INTO staff VALUES (3, 'Shilan', 1);
INSERT INTO staff VALUES (4, 'Karwan', 2);
INSERT INTO staff VALUES (5, 'Dilan', 9);
SELECT s.name AS subordinate, m.name AS supervisor
FROM staff AS s LEFT JOIN staff AS m ON s.supervisor_no = m.staff_no
ORDER BY s.staff_no;
DELETE FROM staff WHERE staff_no = 2;
SELECT * FROM staff;Error: FOREIGN KEY constraint failed subordinate | supervisor Hana | NULL Aram | Hana Shilan | Hana Karwan | Aram staff_no | name | supervisor_no 1 | Hana | NULL 3 | Shilan | 1 4 | Karwan | NULL
Deleting Aram did not delete Karwan: his supervisor_no became NULL.
سڕینەوەی ئارام کاروانی نەسڕییەوە: supervisor_noـی کاروان بوو بە NULL.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
The foreign key points to the same table; ON DELETE SET NULL keeps the subordinates when a supervisor leaves.
ئاماژە بۆ هەمان دەکات؛ ON DELETE SET NULL ژێردەستەکان دەهێڵێتەوە کاتێک سەرپەرشتیارێک دەڕوات.
- Lineدێڕی 11
Refused: there is no staff member 9.
ڕەت دەکرێتەوە: فەرمانبەری ژمارە 9 نییە.
- Lineدێڕی 12-14
The table is joined to itself under two names, one per role: a self-join.
لەگەڵ خۆیدا بە دوو ناو دەلکێنرێت، بۆ هەر ڕۆڵێک یەک ناو: لێکبەستنێکی خۆیی.
Why SET NULL? A subordinate should not disappear with the supervisor, and the default (refuse the delete) would block every change of manager. The choices are compared in Deleting or changing a referenced row.
بۆچی SET NULL؟ ژێردەستێک نابێت لەگەڵ سەرپەرشتیارەکەیدا نەمێنێت و بنەڕەتەکەش (ڕەتکردنەوەی سڕینەوە) هەموو گۆڕینێکی بەڕێوەبەر ڕادەگرێت. هەڵبژاردنەکان لە سڕینەوە یان گۆڕینی ڕیزێکی ئاماژەپێکراو بەراورد کراون.
Rows that point to rows of the same table (an employee's manager), and a self join with aliases and LEFT JOIN that lists them.
کاتێک ئاماژە بە تری هەمان دەکات (بەڕێوەبەری کارمەندێک)، هەروەها بە و LEFT JOIN بۆ لیستکردنیان.
Whole video: employee table with a manager column pointing to the same table, the self join, a category example and LEFT JOIN for the top manager.
هەموو ڤیدیۆکە: کارمەندان بە بۆ بەڕێوەبەر کە ئاماژە بە هەمان دەکات، ، نموونەیەکی پۆلێنەکان و LEFT JOIN بۆ بەڕێوەبەری هەرە سەرەوە.
Recursive: a foreign key to the same tableخۆیی: کلیلێکی بیانی بۆ هەمان خشتە
Recursive M:N: prerequisitesخۆیی M:N: پێشمەرجەکان
When a course may have several prerequisites, the recursive relationship is M:N and gets its own table with two foreign keys to course, one per role. A CHECK stops a course from being its own prerequisite.
کاتێک وانەیەک دەکرێت چەند پێشمەرجێکی هەبێت، M:Nـە و تایبەت بە خۆی وەردەگرێت بە دوو بۆ course، بۆ هەر ڕۆڵێک یەکێک. CHECKـێک ڕێگە نادات وانەیەک پێشمەرجی خۆی بێت.
CREATE TABLE course (code TEXT PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE prerequisite (
course_code TEXT REFERENCES course(code),
prereq_code TEXT REFERENCES course(code),
PRIMARY KEY (course_code, prereq_code),
CHECK (course_code <> prereq_code)
);
INSERT INTO course VALUES ('DB101','Databases 1'), ('DB201','Databases 2'),
('WP101','Web Programming 1'), ('WP201','Web Programming 2');
INSERT INTO prerequisite VALUES ('DB201','DB101'), ('WP201','WP101'), ('WP201','DB101');
INSERT INTO prerequisite VALUES ('DB101','DB101');
SELECT c.title AS course, p.title AS needs_first
FROM prerequisite AS r
JOIN course AS c ON c.code = r.course_code
JOIN course AS p ON p.code = r.prereq_code
ORDER BY c.code, p.code;Error: CHECK constraint failed: course_code <> prereq_code course | needs_first Databases 2 | Databases 1 Web Programming 2 | Databases 1 Web Programming 2 | Web Programming 1
Ternary: a table with three foreign keysسێیانی: خشتەیەک بە سێ کلیلی بیانی
A ternary becomes a table with one foreign key per entity type. With no 1, all three together are the key. With a 1 at Supplier, the pair (part, project) is the key, and the database itself enforces 'one supplier per pair':
دەبێتە بە یەک بۆ هەر جۆرێکی . بەبێ 1، هەر سێکیان پێکەوە . بە 1 لە لای دابینکەر، جووتی (پارچە، پڕۆژە) و خۆی «یەک دابینکەر بۆ هەر جووتێک» دەسەپێنێت:
CREATE TABLE supply_rule (
part TEXT,
project TEXT,
supplier TEXT NOT NULL,
PRIMARY KEY (part, project)
);
INSERT INTO supply_rule VALUES ('bolts','Bridge','Aram Trading');
INSERT INTO supply_rule VALUES ('bolts','Bridge','Dilan Trading');
INSERT INTO supply_rule VALUES ('bolts','School','Dilan Trading');
SELECT * FROM supply_rule;Error: UNIQUE constraint failed: supply_rule.part, supply_rule.project part | project | supplier bolts | Bridge | Aram Trading bolts | School | Dilan Trading
A second supplier for (bolts, Bridge) is refused.
دابینکەری دووەم بۆ (bolts, Bridge) ڕەت دەکرێتەوە.
(The part and project columns would also be foreign keys to their tables; they are left out here to keep the example short.) The hospital keeps only the latest prescription for the same doctor, patient and drug, so there (doctor, patient, drug) is the key and the date and quantity are ordinary columns (the hospital exercise).
( part و projectیش دەبن بە بۆ خۆیان؛ لێرەدا بۆ کورتیی نموونەکە لابراون.) نەخۆشخانەکە تەنها دوایین ڕەچەتە بۆ هەمان دکتۆر، نەخۆش و دەرمان دەهێڵێتەوە، بۆیە لەوێ (دکتۆر، نەخۆش، دەرمان) و بەروار و بڕ ئاسایین (ڕاهێنانی نەخۆشخانە).
ISA: three ways to make tablesISA: سێ ڕێگە بۆ دروستکردنی خشتەکان
There are three common ways to store ACCOUNT with SAVINGS, CURRENT and FIXED DEPOSIT:
سێ ڕێگەی باو هەن بۆ هەڵگرتنی «هەژمار» لەگەڵ «پاشەکەوت»، «ڕۆژانە» و «دانراوی ماوەدار»:
| Optionڕێگە | Tables | Goodباش | Badخراپ | Use it forبۆ چی |
|---|---|---|---|---|
| 1. One table with a type column1. یەک بە جۆر | account(acct_no, holder, balance_iqd, acct_type, interest_rate, overdraft_iqd, start_date, maturity_date) | one table, no joinsیەک ، بێ | many NULLs; subclass rules need CHECKsزۆر NULL؛ یاساکانی پێویستیان بە CHECK هەیە | disjoint, few subclass attributesجیاکراوە، کەمێک |
| 2. A superclass table plus one table per subclass2. لەگەڵ یەک بۆ هەر | account(acct_no, holder, balance_iqd)
savings_account(acct_no, interest_rate) … | no NULLs, nothing repeatedبێ NULL، هیچ دووبارە نابێتەوە | a join to see a whole account; keys do not stop overlapبۆ بینینی هەژمارێکی تەواو پێویستە؛ ڕێگە لە بەیەکداچوون ناگرن | every kind: the safe defaultهەموو جۆرێک: هەڵبژاردنی سەلامەت |
| 3. One table per subclass only3. تەنها یەک بۆ هەر | savings(acct_no, holder, balance_iqd, interest_rate)
current(…)
fixed(…) | no joins inside one kindلە ناو یەک جۆردا نییە | inherited columns repeated; acct_no not unique across tables; a UNION to list all accounts دووبارە دەبنەوە؛ acct_no لە نێوان یەکتا نییە؛ بۆ لیستکردنی هەموو هەژمارەکان UNION پێویستە | only total and disjointتەنها تەواو و جیاکراوە |
Option 2 follows a rule from database design guides: a subset table — a more specific version of another table's subject — must use its parent's primary key. Here savings_account.acct_no is both its primary key and a foreign key to account. Each fact is stored once, the same aim as Normalization.
ڕێگەی 2 پەیڕەوی یاسایەکی ڕێبەرەکانی دیزاینی دەکات: بەشەکی — وەشانێکی تایبەتتری بابەتی تر — دەبێت باوکەکەی بەکار بهێنێت. لێرەدا savings_account.acct_no هەم هەم بۆ account. هەر ڕاستییەک تەنها یەکجار هەڵدەگیرێت، هەمان ئامانجی وانەی نۆرماڵکردن.
ISA: three ways to make tablesISA: سێ ڕێگە بۆ دروستکردنی خشتەکان
Option 1 in SQL: one tableڕێگەی 1 بە SQL: یەک خشتە
CREATE TABLE account1 (
acct_no INTEGER PRIMARY KEY,
holder TEXT NOT NULL,
balance_iqd INTEGER NOT NULL,
acct_type TEXT NOT NULL CHECK (acct_type IN ('savings','current','fixed')),
interest_rate REAL,
overdraft_iqd INTEGER,
start_date TEXT,
maturity_date TEXT,
CHECK (acct_type <> 'savings' OR interest_rate IS NOT NULL),
CHECK (acct_type <> 'fixed' OR (start_date IS NOT NULL AND maturity_date IS NOT NULL))
);
INSERT INTO account1 VALUES (1001, 'Aram', 2500000, 'savings', 3.5, NULL, NULL, NULL);
INSERT INTO account1 VALUES (1002, 'Shilan', 400000, 'current', NULL, 1000000, NULL, NULL);
INSERT INTO account1 VALUES (1003, 'Karwan', 10000000, 'fixed', NULL, NULL, '2026-01-01', '2027-01-01');
INSERT INTO account1 VALUES (1004, 'Dilan', 150000, 'savings', NULL, NULL, NULL, NULL);
SELECT * FROM account1;Error: CHECK constraint failed: acct_type <> 'savings' OR interest_rate IS NOT NULL acct_no | holder | balance_iqd | acct_type | interest_rate | overdraft_iqd | start_date | maturity_date 1001 | Aram | 2500000 | savings | 3.5 | NULL | NULL | NULL 1002 | Shilan | 400000 | current | NULL | 1000000 | NULL | NULL 1003 | Karwan | 10000000 | fixed | NULL | NULL | 2026-01-01 | 2027-01-01
Count the NULLs: three of the four special columns are empty in every row.
NULLـەکان بژمێرە: لە هەر سێ لە چوار بەتاڵن.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
The type column says which subclass the row belongs to.
جۆر دەڵێت سەر بە کام .
- Lineدێڕی 10-11
CHECKs replace the subclass rules: a savings account must have a rate, a fixed deposit both dates.
CHECKـەکان جێگەی یاساکانی دەگرنەوە: هەژماری پاشەکەوت دەبێت ڕێژەی هەبێت، دانراوی ماوەدار هەردوو بەروار.
- Lineدێڕی 16
Refused: a savings account without an interest rate.
ڕەت دەکرێتەوە: هەژماری پاشەکەوت بەبێ ڕێژەی سوود.
ISA: three ways to make tablesISA: سێ ڕێگە بۆ دروستکردنی خشتەکان
Option 2 in SQL: a table per classڕێگەی 2 بە SQL: خشتە بۆ هەر پۆلێک
CREATE TABLE account (
acct_no INTEGER PRIMARY KEY,
holder TEXT NOT NULL,
balance_iqd INTEGER NOT NULL
);
CREATE TABLE savings_account (
acct_no INTEGER PRIMARY KEY REFERENCES account(acct_no) ON DELETE CASCADE,
interest_rate REAL NOT NULL
);
CREATE TABLE current_account (
acct_no INTEGER PRIMARY KEY REFERENCES account(acct_no) ON DELETE CASCADE,
overdraft_iqd INTEGER NOT NULL
);
CREATE TABLE fixed_deposit (
acct_no INTEGER PRIMARY KEY REFERENCES account(acct_no) ON DELETE CASCADE,
start_date TEXT NOT NULL,
maturity_date TEXT NOT NULL
);
INSERT INTO account VALUES (1001,'Aram',2500000), (1002,'Shilan',400000), (1003,'Karwan',10000000);
INSERT INTO savings_account VALUES (1001, 3.5);
INSERT INTO current_account VALUES (1002, 1000000);
INSERT INTO fixed_deposit VALUES (1003, '2026-01-01', '2027-01-01');
INSERT INTO savings_account VALUES (1009, 4.0);
SELECT a.acct_no, a.holder, a.balance_iqd, s.interest_rate
FROM account AS a JOIN savings_account AS s ON s.acct_no = a.acct_no;
DELETE FROM account WHERE acct_no = 1001;
SELECT COUNT(*) AS savings_rows FROM savings_account;Error: FOREIGN KEY constraint failed acct_no | holder | balance_iqd | interest_rate 1001 | Aram | 2500000 | 3.5 savings_rows 0
ON DELETE CASCADE removed the savings row together with account 1001.
ON DELETE CASCADE پاشەکەوتەکەی لەگەڵ هەژماری 1001دا سڕییەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
The subclass key is also a foreign key to the superclass: the subset-table rule.
بۆ : یاسای بەشەکی.
- Lineدێڕی 23
Refused: there is no account 1009 to specialise.
ڕەت دەکرێتەوە: هەژماری 1009 نییە تا تایبەت بکرێت.
- Lineدێڕی 24-25
A whole savings account needs a join.
هەژمارێکی پاشەکەوتی تەواو پێویستی بە هەیە.
ISA: three ways to make tablesISA: سێ ڕێگە بۆ دروستکردنی خشتەکان
Option 3 in SQL: subclass tables onlyڕێگەی 3 بە SQL: تەنها خشتەکانی ژێرپۆل
CREATE TABLE savings3 (acct_no INTEGER PRIMARY KEY, holder TEXT NOT NULL,
balance_iqd INTEGER NOT NULL, interest_rate REAL NOT NULL);
CREATE TABLE current3 (acct_no INTEGER PRIMARY KEY, holder TEXT NOT NULL,
balance_iqd INTEGER NOT NULL, overdraft_iqd INTEGER NOT NULL);
CREATE TABLE fixed3 (acct_no INTEGER PRIMARY KEY, holder TEXT NOT NULL,
balance_iqd INTEGER NOT NULL, start_date TEXT NOT NULL, maturity_date TEXT NOT NULL);
INSERT INTO savings3 VALUES (1001, 'Aram', 2500000, 3.5);
INSERT INTO current3 VALUES (1001, 'Rezan', 90000, 500000);
SELECT acct_no, holder, balance_iqd, 'savings' AS kind FROM savings3
UNION ALL SELECT acct_no, holder, balance_iqd, 'current' FROM current3
UNION ALL SELECT acct_no, holder, balance_iqd, 'fixed' FROM fixed3;acct_no | holder | balance_iqd | kind 1001 | Aram | 2500000 | savings 1001 | Rezan | 90000 | current
No table sees the others, so nothing stops a duplicate account number.
هیچ ئەوانی تر نابینێت، بۆیە هیچ شتێک ڕێگە لە ژمارە هەژماری دووبارە ناگرێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
Accepted: number 1001 now exists twice, as two different accounts.
وەرگیرا: ژمارە 1001 ئێستا دوو جار هەیە، وەک دوو هەژماری جیاواز.
- Lineدێڕی 9-11
All accounts are found only with UNION ALL over every subclass table.
هەموو هەژمارەکان تەنها بە UNION ALL بەسەر هەموو دەدۆزرێنەوە.
Category: a table with a surrogate keyجۆری یەکگرتن: خشتەیەک بە کلیلێکی دەستکرد
A category gets its own table with a new surrogate key, because its superclasses have different keys. Each superclass table gets that key as a UNIQUE foreign key (a person is at most one owner). The vehicle points to owner, never straight to person or company:
جۆری یەکگرتن تایبەت بە خۆی بە نوێ وەردەگرێت، چونکە جیاوازیان هەیە. هەر ئەو وەک UNIQUE وەردەگرێت (هەر کەسێک لانیزۆر یەک خاوەنە). ئۆتۆمبێل ئاماژە بە owner دەکات، هەرگیز ڕاستەوخۆ بە person یان company نا:
CREATE TABLE owner (owner_id INTEGER PRIMARY KEY);
CREATE TABLE person (
national_id TEXT PRIMARY KEY,
name TEXT NOT NULL,
owner_id INTEGER UNIQUE REFERENCES owner(owner_id));
CREATE TABLE company (
reg_no TEXT PRIMARY KEY,
name TEXT NOT NULL,
owner_id INTEGER UNIQUE REFERENCES owner(owner_id));
CREATE TABLE vehicle (
plate_no TEXT PRIMARY KEY,
owner_id INTEGER NOT NULL REFERENCES owner(owner_id));
INSERT INTO owner VALUES (1), (2);
INSERT INTO person VALUES ('N-1001', 'Aram', 1), ('N-1002', 'Lana', NULL);
INSERT INTO company VALUES ('R-500', 'Soran Trading', 2);
INSERT INTO vehicle VALUES ('ERB-1234', 1), ('SUL-5678', 2);
INSERT INTO vehicle VALUES ('DHK-9999', 7);
SELECT v.plate_no, COALESCE(p.name, c.name) AS owner_name,
CASE WHEN p.owner_id IS NULL THEN 'company' ELSE 'person' END AS kind
FROM vehicle AS v
LEFT JOIN person AS p ON p.owner_id = v.owner_id
LEFT JOIN company AS c ON c.owner_id = v.owner_id
ORDER BY v.plate_no;Error: FOREIGN KEY constraint failed plate_no | owner_name | kind ERB-1234 | Aram | person SUL-5678 | Soran Trading | company
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
The category's key as a UNIQUE foreign key in the superclass table (the same in
company).وەک UNIQUE لە (هەمان شت لە
companyـدا).- Lineدێڕی 12
The vehicle references the category, not a superclass.
ئۆتۆمبێلەکە ئاماژە بە دەکات، نەک بە .
- Lineدێڕی 14
Lana's owner_id is NULL: she owns no vehicle (a partial category).
owner_idـی لانا NULLـە: هیچ ئۆتۆمبێلێکی نییە ( بەشەکی).
- Lineدێڕی 17
Refused: there is no owner 7.
ڕەت دەکرێتەوە: خاوەنی ژمارە 7 نییە.
The keys cannot stop one owner_id from appearing in both person and company; a trigger or the application checks that each owner is exactly one of them.
ناتوانن ڕێگە لەوە بگرن کە یەک owner_id هەم لە person و هەم لە companyـدا دەربکەوێت؛ یان بەرنامەکە دەپشکنێت کە هەر خاوەنێک ڕێک یەکێکیانە.
Aggregation: referencing a relationship's keyگردکردنەوە: ئاماژەکردن بە کلیلی پەیوەندییەک
The box becomes the inner relationship's table, works_on(emp_no, project_no). The outer relationship references the box's key, so uses has a two-column foreign key to works_on:
سندوقەکە دەبێتە ناوەوە، works_on(emp_no, project_no). دەرەوە ئاماژە بە سندوقەکە دەکات، بۆیە uses دوو- بۆ works_on هەیە:
CREATE TABLE works_on (
emp_no INTEGER REFERENCES employee(emp_no),
project_no INTEGER REFERENCES project(project_no),
hours INTEGER,
PRIMARY KEY (emp_no, project_no)
);
CREATE TABLE uses (
emp_no INTEGER,
project_no INTEGER,
machine_no INTEGER REFERENCES machine(machine_no),
PRIMARY KEY (emp_no, project_no, machine_no),
FOREIGN KEY (emp_no, project_no) REFERENCES works_on(emp_no, project_no)
);
INSERT INTO works_on VALUES (1, 10, 120), (2, 20, 80);
INSERT INTO uses VALUES (1, 10, 7);
INSERT INTO uses VALUES (1, 20, 8);Error: FOREIGN KEY constraint failed
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 12
The two-column foreign key points to the box: an existing work assignment.
(بە دوو ) ئاماژە بۆ سندوقەکە دەکات: کارێکی دیاریکراوی هەبوو.
- Lineدێڕی 16
Refused: Aram (1) does not work on project 20.
ڕەت دەکرێتەوە: ئارام (1) لەسەر پڕۆژەی 20 کار ناکات.
The department-chair rule works the same way. chairs references member_of(lecturer_no, dept_code), so the chair's department and the membership's department are the same column. SQLite refuses Rezan (L03), who is not a member of IT:
یاسای سەرۆکی بەش بە هەمان شێوە کار دەکات. chairs ئاماژە بە member_of(lecturer_no, dept_code) دەکات، بۆیە بەشی سەرۆکەکە و بەشی ئەندامێتییەکە هەمان . SQLite ڕێزان (L03) ڕەت دەکاتەوە، کە ئەندامی IT نییە:
CREATE TABLE chairs (
dept_code TEXT PRIMARY KEY,
lecturer_no TEXT NOT NULL UNIQUE,
FOREIGN KEY (lecturer_no, dept_code) REFERENCES member_of(lecturer_no, dept_code)
);
-- members: L01 in CS and IT; L02 in IT; L03 in no department
INSERT INTO chairs VALUES ('CS','L01');
INSERT INTO chairs VALUES ('IT','L03');
INSERT INTO chairs VALUES ('IT','L01');
INSERT INTO chairs VALUES ('IT','L02');Error: FOREIGN KEY constraint failed Error: UNIQUE constraint failed: chairs.lecturer_no
Only the first and the last INSERT succeed: Soran (L01) chairs CS and Hana (L02) chairs IT.
تەنها یەکەم و دوایین INSERT سەردەکەون: سۆران (L01) سەرۆکی CSـە و هانا (L02) سەرۆکی IT.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
One chair per department (the primary key).
یەک سەرۆک بۆ هەر بەشێک ().
- Lineدێڕی 3
Nobody chairs two departments (UNIQUE on lecturer_no).
کەس سەرۆکی دوو بەش نییە (UNIQUE لەسەر lecturer_no).
- Lineدێڕی 8
Refused by the foreign key: L03 is not a member of IT.
ڕەتی دەکاتەوە: L03 ئەندامی IT نییە.
- Lineدێڕی 9
Refused by UNIQUE: L01 is a member of IT but already chairs CS.
UNIQUE ڕەتی دەکاتەوە: L01 ئەندامی ITـە، بەڵام پێشتر سەرۆکی CSـە.
How many tables?چەند خشتە؟
Exam questions often ask how many tables a diagram needs. Count with the rules: one table per entity type; one per M:N relationship (recursive ones too); one per multi-valued attribute; one per ternary; 1:1 and 1:N relationships add only foreign keys. For ISA, say which option you use.
پرسیارەکانی تاقیکردنەوە زۆرجار دەپرسن هێڵکارییەک پێویستی بە چەند هەیە. بە یاساکان بژمێرە: یەک بۆ هەر جۆرێکی ؛ یەک بۆ هەر (خۆییەکانیش)؛ یەک بۆ هەر ؛ یەک بۆ هەر ؛ 1:1 و 1:N تەنها زیاد دەکەن. بۆ ISA بڵێ کام ڕێگە بەکار دەهێنیت.
Worked example: PERSON is specialised into STAFF and STUDENT (overlapping); STAFF works in DEPARTMENT (N:1); STUDENT takes COURSE (M:N, with a grade); COURSE is a prerequisite of COURSE (M:N); a student has several phone numbers. With option 2: person, staff, student, department and course (5) + takes and prerequisite (2) + student_phone (1) = 8 tables. Works in adds only staff.dept_code.
نموونەی کارکراو: «کەس» دەکرێتە «فەرمانبەر» و «قوتابی» (بەیەکداچوو)؛ فەرمانبەر لە بەشدا کار دەکات (N:1)؛ قوتابی وانە دەخوێنێت (M:N، بە نمرە)؛ وانە پێشمەرجی وانەیە (M:N)؛ قوتابییەک چەند ژمارە مۆبایلێکی هەیە. بە ڕێگەی 2: person، staff، student، department و course (5) + takes و prerequisite (2) + student_phone (1) = 8 . «کار دەکات لە» تەنها staff.dept_code زیاد دەکات.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What is rightڕاستییەکە |
|---|---|
| Storing an M:N relationship inside one entity's table.هەڵگرتنی لە ناو یەک . | M:N needs its own table, also when it is recursive.M:N تایبەت بە خۆی پێویستە، تەنانەت کاتێک خۆییە. |
| Storing a ternary as three junction tables.هەڵگرتنی وەک سێ . | Use one table with the three foreign keys; three pair tables rebuild false triples.یەک بە سێ بەکار بهێنە؛ سێ جووت سێیانەی هەڵە دروست دەکەنەوە. |
| Using 'a table per subclass only' for an overlapping or partial specialisation.بەکارهێنانی «تەنها بۆ هەر » بۆ بەیەکداچوو یان بەشەکی. | That option works only for total, disjoint specialisations; otherwise use a superclass table plus subclass tables.ئەو ڕێگەیە تەنها بۆ تەواو و جیاکراوە کار دەکات؛ ئەگەر نا لەگەڵ بەکار بهێنە. |
| Giving a subclass table a new key of its own.دانانی نوێ بۆ . | A subclass table uses its superclass's primary key, which is also its foreign key. بەکار دەهێنێت، کە . |
Referencing only one column of a composite key (for example only emp_no in uses).ئاماژەکردن بە تەنها یەک لە (بۆ نموونە تەنها emp_no لە usesـدا). | Reference the whole key of the inner relationship: FOREIGN KEY (emp_no, project_no).ئاماژە بە هەموو ناوەوە بکە: FOREIGN KEY (emp_no, project_no). |
Testing foreign keys in SQLite without PRAGMA foreign_keys = ON;.تاقیکردنەوەی لە SQLite بەبێ PRAGMA foreign_keys = ON;. | Without it SQLite accepts every wrong row; switch it on for each connection.بەبێ ئەو، SQLite هەموو هەڵە وەردەگرێت؛ هەر جارێک دەکەیتەوە چالاکی بکە. |
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
A hospital in Sulaymaniyah wants a database for its patients, doctors and the drugs they prescribe. These are its eleven rules, rewritten from a homework of the course:
نەخۆشخانەیەک لە سلێمانی بۆ نەخۆش، دکتۆر و ئەو دەرمانانەی دەینووسن دەوێت. ئەمانە یانزە یاساکەیەتی، کە لە ئەرکێکی ماڵەوەی کۆرسەکەوە دووبارە نووسراونەتەوە:
- Patients are identified by a hospital file number; their name, address and date of birth are recorded.
- Doctors are identified by a staff number; their name, specialty and years of experience are recorded.
- Each pharmaceutical company has a unique name and a phone number.
- Each drug has a trade name and a formula. It is made by one company, and its trade name is unique only among that company's drugs. When a company is deleted, its drugs are no longer needed.
- Each pharmacy has a name, an address and a phone number.
- Every patient has one primary doctor, and every doctor is the primary doctor of at least one patient.
- A pharmacy sells many drugs, each at its own price; a drug can be sold in many pharmacies at different prices.
- Doctors prescribe drugs for patients: a doctor prescribes for many patients, and a patient can get prescriptions from many doctors.
- A prescription has a date and a quantity. If a doctor prescribes the same drug to the same patient again, only the latest prescription is kept.
- Companies sign long-term contracts with pharmacies (a company with many pharmacies, a pharmacy with many companies). Each contract has a start date, an end date and its text.
- The pharmacy names a supervisor for each contract. There is always one, but the supervisor can change during the contract.
- نەخۆشەکان بە ژمارەی دۆسیەی نەخۆشخانە دەناسرێنەوە؛ ناو، ناونیشان و ڕێکەوتی لەدایکبوونیان تۆمار دەکرێت.
- دکتۆرەکان بە ژمارەی فەرمانبەری دەناسرێنەوە؛ ناو، پسپۆڕی و ساڵانی ئەزموونیان تۆمار دەکرێت.
- هەر کۆمپانیایەکی دەرمان ناوێکی تایبەت و ژمارە تەلەفۆنێکی هەیە.
- هەر دەرمانێک ناوی بازرگانی و فۆرمولایەکی هەیە. یەک کۆمپانیا دروستی دەکات و ناوە بازرگانییەکەی تەنها لە ناو دەرمانەکانی ئەو کۆمپانیایەدا تایبەتە. کاتێک کۆمپانیایەک دەسڕدرێتەوە، دەرمانەکانی ئیتر پێویست نین.
- هەر دەرمانخانەیەک ناو، ناونیشان و ژمارە تەلەفۆنێکی هەیە.
- هەموو نەخۆشێک یەک دکتۆری سەرەکیی هەیە و هەموو دکتۆرێک دکتۆری سەرەکیی لانیکەم یەک نەخۆشە.
- دەرمانخانەیەک چەندین دەرمان دەفرۆشێت، هەر یەکەیان بە نرخی خۆی؛ دەرمانێک دەکرێت لە چەندین دەرمانخانەدا بە نرخی جیاواز بفرۆشرێت.
- دکتۆرەکان دەرمان بۆ نەخۆشەکان دەنووسن: دکتۆرێک بۆ چەندین نەخۆش دەنووسێت و نەخۆشێک دەتوانێت لە چەندین دکتۆرەوە ڕەچەتە وەربگرێت.
- ڕەچەتە بەروار و بڕێکی هەیە. ئەگەر دکتۆرێک دووبارە هەمان دەرمان بۆ هەمان نەخۆش بنووسێت، تەنها دوایین ڕەچەتە دەهێڵدرێتەوە.
- کۆمپانیاکان گرێبەستی درێژخایەن لەگەڵ دەرمانخانەکاندا واژۆ دەکەن (کۆمپانیایەک لەگەڵ چەندین دەرمانخانە، دەرمانخانەیەک لەگەڵ چەندین کۆمپانیا). هەر گرێبەستێک ڕێکەوتی دەستپێک، ڕێکەوتی کۆتایی و دەقەکەی هەیە.
- دەرمانخانەکە سەرپەرشتیارێک بۆ هەر گرێبەستێک دیاری دەکات. هەمیشە یەکێک هەیە، بەڵام سەرپەرشتیارەکە دەتوانێت لە ماوەی گرێبەستەکەدا بگۆڕدرێت.
Draw the EER diagram for the eleven rules, then write the tables with their primary and foreign keys. Say which rule each part comes from.
هێڵکاریی EER بۆ یانزە یاساکە بکێشە، پاشان لەگەڵ و بنووسە. بڵێ هەر بەشێک لە کام یاساوە هاتووە.
Hintئاماژە
Underline each noun (entity?) and each verb (relationship?), as in From a text to a diagram. Then ask, rule by rule: how many on each side, must every one take part, and does a fact join two or three things?
ژێر هەر ناوێک (کە ؟) و هەر کارێک (کە ؟) هێڵ بکێشە، وەک شاشەی «لە دەقەوە بۆ هێڵکاری» فێری دەکات. پاشان یاسا بە یاسا بپرسە: لە هەر لایەک چەند، دەبێت هەموو یەکێک بەشدار بێت و ئایا ڕاستییەک دوو شت دەبەستێتەوە یان سێ؟
Show answerوەڵامەکە پیشان بدە
- Rules 1–2: PATIENT and DOCTOR are entities. The age is stored as
date_of_birthand derived (dashed oval). - Rules 3–4: DRUG is a weak entity of PHARMA COMPANY: its partial key is
trade_name, its full key (company_name, trade_name), and deleting a company deletes its drugs (identifying relationship makes, ON DELETE CASCADE). - Rule 6: primary doctor is N:1 with total participation on both sides: (1,1) next to PATIENT, (1,N) next to DOCTOR.
- Rule 7: sells is M:N with the attribute
price_iqd. - Rules 8–9: prescribes is a ternary relationship with
presc_dateandquantity. Because only the latest prescription is kept, (doctor, patient, drug) is its key. - Rules 10–11: contract is M:N with
start_date,end_date,contract_textandsupervisor(NOT NULL; a change is an UPDATE). If the supervisor's history were needed, it would become a table of its own. - PHARMACY gets a surrogate key
pharmacy_id: two pharmacies in different cities may share a name.
- یاساکانی 1 و 2: «نەخۆش» و «دکتۆر» . تەمەن وەک
date_of_birthهەڵدەگیرێت و داتاشراوە (هێلکەیی پچڕپچڕ). - یاساکانی 3 و 4: «دەرمان» «کۆمپانیای دەرمان»ـە:
trade_nameـە، تەواوەکەی (company_name، trade_name) و سڕینەوەی کۆمپانیایەک دەرمانەکانی دەسڕێتەوە ( «بەرهەمی دەهێنێت»، ON DELETE CASCADE). - یاسای 6: «دکتۆری سەرەکی»
N:1ـە بە لە هەردوو لا: (1,1) لە تەنیشت «نەخۆش»، (1,N) لە تەنیشت «دکتۆر». - یاسای 7: «دەفرۆشێت»
M:Nـە بەprice_iqd. - یاساکانی 8 و 9: «دەنووسێت» بە
presc_dateوquantity. چونکە تەنها دوایین ڕەچەتە دەمێنێتەوە، (دکتۆر، نەخۆش، دەرمان) . - یاساکانی 10 و 11: «گرێبەست»
M:Nـە بەstart_date،end_date،contract_textوsupervisor(NOT NULL؛ گۆڕین UPDATEـێکە). ئەگەر مێژووی سەرپەرشتیاران پێویست بوایە، تایبەت بە خۆی دەبوو. - «دەرمانخانە»
pharmacy_idوەردەگرێت: دوو دەرمانخانە لە دوو شاری جیاوازدا دەکرێت هەمان ناویان هەبێت.
CREATE TABLE doctor (doctor_id TEXT PRIMARY KEY, name TEXT NOT NULL,
specialty TEXT NOT NULL, years_exp INTEGER NOT NULL CHECK (years_exp >= 0));
CREATE TABLE patient (file_no TEXT PRIMARY KEY, name TEXT NOT NULL, address TEXT,
date_of_birth TEXT NOT NULL,
primary_doctor_id TEXT NOT NULL REFERENCES doctor(doctor_id));
CREATE TABLE pharma_company (company_name TEXT PRIMARY KEY, phone TEXT);
CREATE TABLE drug (
company_name TEXT REFERENCES pharma_company(company_name) ON DELETE CASCADE,
trade_name TEXT,
formula TEXT NOT NULL,
PRIMARY KEY (company_name, trade_name));
CREATE TABLE pharmacy (pharmacy_id INTEGER PRIMARY KEY, name TEXT NOT NULL,
address TEXT NOT NULL, phone TEXT);
CREATE TABLE sells (
pharmacy_id INTEGER REFERENCES pharmacy(pharmacy_id),
company_name TEXT, trade_name TEXT,
price_iqd INTEGER NOT NULL CHECK (price_iqd > 0),
PRIMARY KEY (pharmacy_id, company_name, trade_name),
FOREIGN KEY (company_name, trade_name) REFERENCES drug ON DELETE CASCADE);
CREATE TABLE prescription (
doctor_id TEXT REFERENCES doctor(doctor_id),
file_no TEXT REFERENCES patient(file_no),
company_name TEXT, trade_name TEXT,
presc_date TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (doctor_id, file_no, company_name, trade_name),
FOREIGN KEY (company_name, trade_name) REFERENCES drug ON DELETE CASCADE);
CREATE TABLE contract (
company_name TEXT REFERENCES pharma_company(company_name) ON DELETE CASCADE,
pharmacy_id INTEGER REFERENCES pharmacy(pharmacy_id),
start_date TEXT NOT NULL, end_date TEXT NOT NULL,
contract_text TEXT NOT NULL,
supervisor TEXT NOT NULL,
PRIMARY KEY (company_name, pharmacy_id),
CHECK (end_date > start_date));Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
Rule 6: every patient has a primary doctor (NOT NULL foreign key).
یاسای 6: هەموو نەخۆشێک دکتۆری سەرەکیی هەیە ( NOT NULL).
- Lineدێڕی 7-11
Rule 4: the weak entity DRUG, keyed by its owner's key plus its partial key.
یاسای 4: «دەرمان»، بە خاوەنەکەی و دەناسرێتەوە.
- Lineدێڕی 26
Rule 9: the key of the ternary keeps one row per doctor, patient and drug.
یاسای 9: سێیانییەکە بۆ هەر دکتۆر، نەخۆش و دەرمانێک یەک دەهێڵێتەوە.
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
The hospital rules at workیاساکانی نەخۆشخانە لە کاردا
With the tables above and a few rows, SQLite shows three rules working: the latest prescription replaces the old one, a doctor without patients is found by a query, and deleting a company removes its drugs, everything that hangs on them, and its contracts.
بە سەرەوە و چەند ، SQLite سێ یاسا لە کاردا پیشان دەدات: دوایین ڕەچەتە جێگەی کۆنەکە دەگرێتەوە، دکتۆرێکی بێ نەخۆش بە دەدۆزرێتەوە و سڕینەوەی کۆمپانیایەک دەرمانەکانی، هەموو ئەوەی بەوانەوە بەستراوە و گرێبەستەکانی لادەبات.
INSERT INTO prescription VALUES ('D01','P-1001','Lana Medical','Feverol','2026-10-01',10);
INSERT INTO prescription VALUES ('D01','P-1001','Lana Medical','Feverol','2026-10-20',20)
ON CONFLICT (doctor_id, file_no, company_name, trade_name)
DO UPDATE SET presc_date = excluded.presc_date, quantity = excluded.quantity;
SELECT doctor_id, file_no, trade_name, presc_date, quantity FROM prescription;
SELECT d.doctor_id, d.name FROM doctor AS d
WHERE NOT EXISTS (SELECT 1 FROM patient AS p WHERE p.primary_doctor_id = d.doctor_id);
DELETE FROM pharma_company WHERE company_name = 'Lana Medical';
SELECT COUNT(*) AS drugs, (SELECT COUNT(*) FROM sells) AS sells_rows,
(SELECT COUNT(*) FROM prescription) AS prescriptions,
(SELECT COUNT(*) FROM contract) AS contracts FROM drug;doctor_id | file_no | trade_name | presc_date | quantity D01 | P-1001 | Feverol | 2026-10-20 | 20 doctor_id | name D03 | Karwan drugs | sells_rows | prescriptions | contracts 1 | 1 | 0 | 0
Sample rows: doctors Hana (D01), Dilan (D02) and Karwan (D03); companies Soran Pharma (one drug, Cofix) and Lana Medical (Cofix and Feverol); one contract between Lana Medical and pharmacy 1. After the delete, only Soran Pharma's Cofix and its one price remain.
نموونە: دکتۆرەکان هانا (D01)، دیلان (D02) و کاروان (D03)؛ کۆمپانیاکان Soran Pharma (یەک دەرمان، Cofix) و Lana Medical (Cofix و Feverol)؛ یەک گرێبەست لە نێوان Lana Medical و دەرمانخانەی 1. دوای سڕینەوەکە تەنها Cofixـی Soran Pharma و یەک نرخەکەی دەمێننەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
Rule 9: the same doctor, patient and drug again: the row is updated, not added (an upsert).
یاسای 9: دووبارە هەمان دکتۆر، نەخۆش و دەرمان: نوێ دەکرێتەوە، زیاد ناکرێت.
- Lineدێڕی 7-8
Rule 6, second half: a foreign key cannot say 'every doctor has at least one patient', so a query checks it.
نیوەی دووەمی یاسای 6: ناتوانێت بڵێت «هەموو دکتۆرێک لانیکەم یەک نەخۆشی هەیە»، بۆیە دەیپشکنێت.
- Lineدێڕی 10
Rule 4: the company's drugs go, and with them their sells and prescription rows; its contract rows go through their own ON DELETE CASCADE.
یاسای 4: دەرمانەکانی کۆمپانیاکە دەڕۆن و لەگەڵیاندا sells و prescription؛ contractـی کۆمپانیاکەش بە ON DELETE CASCADEـی خۆیان دەڕۆن.
Notice that both companies sell a drug called Cofix: the trade name alone is not a key, exactly as rule 4 says. Karwan (D03) is nobody's primary doctor yet, so the design rule is broken until a patient is assigned to him.
تێبینی بکە هەردوو کۆمپانیاکە دەرمانێک بە ناوی Cofix دەفرۆشن: ناوی بازرگانی بە تەنها نییە، ڕێک وەک یاسای 4 دەڵێت. کاروان (D03) هێشتا دکتۆری سەرەکیی هیچ کەسێک نییە، بۆیە یاسای دیزاینەکە شکاوە تا نەخۆشێکی بۆ دیاری دەکرێت.
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
Exercise: a package-delivery companyڕاهێنان: کۆمپانیایەکی گەیاندنی پاکەت
Design a database for a worldwide package-delivery company. Customers send packages and customers receive them; some do both. Every package must be identifiable and trackable: where it is now and every place it has been. The places are trucks, planes, airports and warehouses. Give the E-R diagram, the tables and their constraints, including primary and foreign keys.
بۆ کۆمپانیایەکی جیهانیی گەیاندنی پاکەت دیزاین بکە. کڕیاران پاکەت دەنێرن و کڕیاران وەریدەگرن؛ هەندێکیان هەردووکیان دەکەن. هەموو پاکەتێک دەبێت بناسرێتەوە و بەدوای بچێت: ئێستا لە کوێیە و لە هەموو ئەو شوێنانەی پێیدا تێپەڕیوە. شوێنەکان بارهەڵگر، فڕۆکە، فڕۆکەخانە و کۆگان. ، و ڕێساکانیان، لەگەڵ و ، بنووسە.
Hintئاماژە
Two relationships between the same two entity types need role names. Ask what all the kinds of place have in common.
دوو لە نێوان هەمان دوو جۆری پێویستیان بە هەیە. بپرسە هەموو جۆرەکانی شوێن چییان هاوبەشە.
Show answerوەڵامەکە پیشان بدە
- CUSTOMER is one entity type with two relationships to PACKAGE, sends and receives (each N:1 from the package's side). The role names sender and receiver become the columns
sender_idandreceiver_id. - LOCATION is the generalisation of truck, plane, airport and warehouse: disjoint and total (d and a double line).
- The history is the M:N relationship visits with
arrival_timeanddeparture_time; one row per stop. Its key is (package_id, arrival_time), because a package can come back to the same place. - The current location is derived: the visit with no departure time yet. A partial unique index allows only one such open visit per package.
- «کڕیار» یەک جۆری بە دوو لەگەڵ «پاکەت»: «دەینێرێت» و «وەریدەگرێت» (هەر یەکەیان لە لای پاکەتەوە
N:1). نێرەر و وەرگر دەبنەsender_idوreceiver_id. - «شوێن» بارهەڵگر، فڕۆکە، فڕۆکەخانە و کۆگایە: جیاکراوە و تەواو (d و هێڵی دووانە).
- مێژووەکە
M:Nـی «سەردان دەکات»ـە بەarrival_timeوdeparture_time؛ هەر وێستگەیەک یەک . (package_id، arrival_time)ە، چونکە پاکەتێک دەتوانێت بگەڕێتەوە بۆ هەمان شوێن. - شوێنی ئێستا داتاشراوە: ئەو سەردانەی هێشتا کاتی ڕۆیشتنی نییە. یەکتای بەشەکی تەنها ڕێگە بە یەک سەردانی کراوە بۆ هەر پاکەتێک دەدات.
CREATE TABLE package (
package_id INTEGER PRIMARY KEY,
weight_kg REAL NOT NULL CHECK (weight_kg > 0),
sent_date TEXT NOT NULL,
sender_id INTEGER NOT NULL REFERENCES customer(cust_id),
receiver_id INTEGER NOT NULL REFERENCES customer(cust_id));
CREATE TABLE location (
location_id INTEGER PRIMARY KEY,
loc_type TEXT NOT NULL CHECK (loc_type IN ('truck','plane','airport','warehouse')));
CREATE TABLE airport (location_id INTEGER PRIMARY KEY REFERENCES location,
iata_code TEXT NOT NULL UNIQUE, city TEXT NOT NULL);
-- truck, plane and warehouse are built the same way
CREATE TABLE visit (
package_id INTEGER REFERENCES package(package_id),
arrival_time TEXT NOT NULL,
location_id INTEGER NOT NULL REFERENCES location(location_id),
departure_time TEXT,
PRIMARY KEY (package_id, arrival_time),
CHECK (departure_time IS NULL OR departure_time > arrival_time));
CREATE UNIQUE INDEX one_open_visit ON visit(package_id) WHERE departure_time IS NULL;
SELECT v.arrival_time, l.loc_type, v.departure_time
FROM visit AS v JOIN location AS l ON l.location_id = v.location_id
WHERE v.package_id = 5001 ORDER BY v.arrival_time;arrival_time | loc_type | departure_time 2026-10-20 09:00 | warehouse | 2026-10-20 15:00 2026-10-20 15:00 | truck | 2026-10-20 18:30 2026-10-20 18:30 | airport | 2026-10-21 07:10 2026-10-21 07:10 | plane | 2026-10-21 10:00 2026-10-21 10:00 | airport | NULL
Aram's package from Sulaymaniyah to Karwan in Istanbul: it is now at the Istanbul airport (the open visit).
پاکەتی ئارام لە سلێمانییەوە بۆ کاروان لە ئەستەنبوڵ: ئێستا لە فڕۆکەخانەی ئەستەنبوڵە (سەردانە کراوەکە).
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5-6
Two foreign keys to the same customer table, one per role.
دوو بۆ هەمان customer، بۆ هەر ڕۆڵێک یەکێک.
- Lineدێڕی 20
At most one visit without a departure time per package: the current location.
بۆ هەر پاکەتێک لانیزۆر یەک سەردان بەبێ کاتی ڕۆیشتن: شوێنی ئێستا.
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
Exercise: an online marketڕاهێنان: بازاڕێکی ئۆنلاین
An online market in Kurdistan has members, each with a member number, an e-mail address, a name, a password, a home address and a phone number. A member can be a buyer (with a shipping address) or a seller (with a bank account). Sellers put items up for auction: each item has a number, a title, a description, a starting price, a bid step and start and end dates, and belongs to a category in a fixed category tree. Buyers bid on items (price and time); at the end the highest bidder wins and the sale goes ahead. After a sale, buyer and seller may each rate the other (1–10) with a comment. Draw the EER diagram and write the tables.
بازاڕێکی ئۆنلاین لە کوردستان ئەندامی هەیە و هەر ئەندامێک ژمارەی ئەندامێتی، ئیمەیڵ، ناو، وشەی نهێنی، ناونیشانی ماڵ و ژمارە تەلەفۆنی هەیە. ئەندامێک دەتوانێت کڕیار بێت (بە ناونیشانی ناردن) یان فرۆشیار (بە هەژماری بانک). فرۆشیاران کاڵا بۆ مەزاد دادەنێن: هەر کاڵایەک ژمارە، ناونیشان، وەسف، نرخی دەستپێک، هەنگاوی نرخپێدان و ڕێکەوتی دەستپێک و کۆتایی هەیە و سەر بە پۆلێکە لە دارێکی جێگیری پۆلەکان. کڕیاران نرخ بۆ کاڵاکان دەدەن (نرخ و کات)؛ لە کۆتاییدا بەرزترین نرخدەر دەیباتەوە و فرۆشتنەکە ئەنجام دەدرێت. دوای فرۆشتن، کڕیار و فرۆشیار هەر یەکەیان دەتوانن ئەوی تر هەڵبسەنگێنن (1 تا 10) لەگەڵ تێبینییەک. هێڵکاریی EER بکێشە و بنووسە.
Hintئاماژە
Look for an ISA (members), a recursive relationship (categories), an M:N with attributes (bids) and a derived fact (the winner).
بەدوای ISA (ئەندامەکان)، (پۆلەکان)، M:N بە (نرخپێدانەکان) و ڕاستییەکی داتاشراو (براوە) بگەڕێ.
Show answerوەڵامەکە پیشان بدە
- MEMBER is specialised into BUYER and SELLER: overlapping (one member can buy and sell) and total if every member must be at least one of them. Option 2:
buyerandsellertables whose key is also a foreign key tomember. - The category tree is a recursive relationship on CATEGORY (is inside), stored as
parent_id. - SELLER offers ITEM (1:N); ITEM belongs to CATEGORY (N:1).
- BUYER bids on ITEM is M:N with
priceandbid_time; its key includes the time, because a buyer can bid several times. - The winner is derived (the highest bid); the finished sale is stored once per item, and FEEDBACK hangs on the sale with (item, from member) as its key and a CHECK for 1–10.
- Store a password hash, never the password itself.
- «ئەندام» دەکرێتە «کڕیار» و «فرۆشیار»: بەیەکداچوو (یەک ئەندام دەتوانێت بکڕێت و بفرۆشێت) و تەواو ئەگەر هەموو ئەندامێک دەبێت لانیکەم یەکێکیان بێت. ڕێگەی 2:
buyerوsellerکە بۆmember. - دارەکەی پۆلەکان لەسەر «پۆل» (لە ناوی دایە) کە وەک
parent_idهەڵدەگیرێت. - «فرۆشیار» «کاڵا» پێشکەش دەکات (
1:N)؛ «کاڵا» سەر بە «پۆل»ـە (N:1). - «کڕیار» نرخ دەدات بۆ «کاڵا»
M:Nـە بەpriceوbid_time؛ کاتەکەش لەخۆ دەگرێت، چونکە کڕیارێک دەتوانێت چەند جار نرخ بدات. - براوەکە داتاشراوە (بەرزترین نرخ)؛ فرۆشتنی تەواوبوو بۆ هەر کاڵایەک یەکجار هەڵدەگیرێت و «هەڵسەنگاندن» بە فرۆشتنەکەوە بەستراوە بە (کاڵا، لە ئەندامەوە) وەک و CHECKـێک بۆ 1 تا 10.
- هاشی وشەی نهێنی هەڵبگرە، هەرگیز خودی وشەی نهێنی نا.
CREATE TABLE member (member_no INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL, password_hash TEXT NOT NULL, home_address TEXT, phone TEXT);
CREATE TABLE buyer (member_no INTEGER PRIMARY KEY REFERENCES member, shipping_address TEXT NOT NULL);
CREATE TABLE seller (member_no INTEGER PRIMARY KEY REFERENCES member, bank_account TEXT NOT NULL, bank_code TEXT NOT NULL);
CREATE TABLE category (category_id INTEGER PRIMARY KEY, name TEXT NOT NULL,
parent_id INTEGER REFERENCES category(category_id));
CREATE TABLE item (item_no INTEGER PRIMARY KEY, seller_no INTEGER NOT NULL REFERENCES seller,
category_id INTEGER NOT NULL REFERENCES category, title TEXT NOT NULL, description TEXT,
start_price INTEGER NOT NULL, bid_increment INTEGER NOT NULL,
start_date TEXT NOT NULL, end_date TEXT NOT NULL, CHECK (end_date > start_date));
CREATE TABLE bid (buyer_no INTEGER REFERENCES buyer, item_no INTEGER REFERENCES item,
bid_time TEXT, price INTEGER NOT NULL, PRIMARY KEY (buyer_no, item_no, bid_time));
CREATE TABLE sale (item_no INTEGER PRIMARY KEY REFERENCES item,
buyer_no INTEGER NOT NULL REFERENCES buyer, price INTEGER NOT NULL);
CREATE TABLE feedback (item_no INTEGER REFERENCES sale, from_no INTEGER REFERENCES member,
rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 10), comment TEXT,
PRIMARY KEY (item_no, from_no));
WITH RECURSIVE up(category_id, name, parent_id, depth) AS (
SELECT category_id, name, parent_id, 0 FROM category WHERE category_id = 3
UNION ALL
SELECT c.category_id, c.name, c.parent_id, up.depth + 1
FROM category AS c JOIN up ON c.category_id = up.parent_id)
SELECT depth, name FROM up ORDER BY depth;depth | name 0 | Phone cases 1 | Phones 2 | Electronics
Sample rows: Electronics ⊃ Phones ⊃ Phone cases. In the test run SQLite also refused an item from a member who is not a seller, and a rating of 11.
نموونە: Electronics ⊃ Phones ⊃ Phone cases. لە تاقیکردنەوەکەدا SQLite کاڵای ئەندامێکی نافرۆشیار و هەڵسەنگاندنی 11ـیشی ڕەت کردەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-2
Only a hash of the password is stored.
تەنها هاشی وشەی نهێنی هەڵدەگیرێت.
- Lineدێڕی 5-6
The recursive relationship: a category points to its parent category.
: پۆلێک ئاماژە بۆ پۆلی باوکی دەکات.
- Lineدێڕی 19-24
A recursive query follows parent_id from 'Phone cases' to the top.
خۆیی بەدوای parent_idـدا دەچێت لە «Phone cases»ـەوە تا سەرەوە.
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
Exercise: a university's ISA hierarchyڕاهێنان: پلەبەندیی ISAـی زانکۆیەک
A university keeps PERSON with the subclasses LECTURER, ADMINISTRATOR and STUDENT. LECTURER is split into PERMANENT, CONTRACT and VISITING; STUDENT into UNDERGRADUATE, POSTGRADUATE and NON-DEGREE. For each of the three specialisations say: disjoint or overlapping, total or partial. Then say what is wrong with drawing every ISA line with '1:n', and choose a table option for each specialisation.
زانکۆیەک «کەس» لەگەڵ «مامۆستا»، «کارگێڕ» و «قوتابی» هەڵدەگرێت. «مامۆستا» دابەش دەبێت بۆ «هەمیشەیی»، «گرێبەست» و «میوان»؛ «قوتابی» بۆ «بەکالۆریۆس»، «خوێندنی باڵا» و «بێ بڕوانامە». بۆ هەر یەک لە سێ بڵێ: جیاکراوەیە یان بەیەکداچوو، تەواوە یان بەشەکی. پاشان بڵێ کێشانی هەموو هێڵێکی ISA بە «1:n» چ هەڵەیەکی تێدایە و بۆ هەر ڕێگەیەکی هەڵبژێرە.
Show answerوەڵامەکە پیشان بدە
- PERSON → LECTURER, ADMINISTRATOR, STUDENT: overlapping (a lecturer can study for a PhD at the same university) and partial (graduates and visitors are persons in the system but none of the three).
- LECTURER → PERMANENT, CONTRACT, VISITING: disjoint (one kind of appointment at a time) and total (every lecturer has one).
- STUDENT → UNDERGRADUATE, POSTGRADUATE, NON-DEGREE: disjoint and total.
- '1:n' on ISA lines is wrong: a permanent lecturer is one lecturer, which is one person (1:1). Use d/o in the circle and single/double lines.
- Tables: PERSON is overlapping and partial, so use option 2 (a person table plus lecturer, administrator and student tables keyed by person_id). The two lower specialisations are total and disjoint, so any option works; option 2 keeps the whole hierarchy consistent.
- «کەس» ← مامۆستا، کارگێڕ، قوتابی: بەیەکداچوو (مامۆستایەک دەتوانێت لە هەمان زانکۆدا دکتۆرا بخوێنێت) و بەشەکی (دەرچووان و میوانان لە سیستەمەکەدا کەسن بەڵام هیچ یەک لە سێکەیان نین).
- «مامۆستا» ← هەمیشەیی، گرێبەست، میوان: جیاکراوە (لە یەک کاتدا یەک جۆر دامەزراندن) و تەواو (هەموو مامۆستایەک یەکێکی هەیە).
- «قوتابی» ← بەکالۆریۆس، خوێندنی باڵا، بێ بڕوانامە: جیاکراوە و تەواو.
- «1:n» لەسەر هێڵەکانی ISA هەڵەیە: مامۆستایەکی هەمیشەیی خودی یەک مامۆستایە و ئەویش خودی یەک کەسە (
1:1). d/o لە ناو بازنەکەدا و هێڵی تاک/دووانە بەکار بهێنە. - : «کەس» بەیەکداچوو و بەشەکییە، بۆیە ڕێگەی 2 بەکار بهێنە ( بۆ person لەگەڵ lecturer، administrator و student کە بە person_id دەناسرێنەوە). دوو خوارەوە تەواو و جیاکراوەن، بۆیە هەر ڕێگەیەک کار دەکات؛ ڕێگەی 2 هەموو پلەبەندییەکە یەکگرتوو دەهێڵێتەوە.
Exercise: the hospital and its pharmaciesڕاهێنان: نەخۆشخانەکە و دەرمانخانەکانی
Exam-style questions: tablesپرسیاری شێوەی تاقیکردنەوە: خشتەکان
Summary and reviewکورتە و پێداچوونەوە
- Recursive 1:N → a foreign key to the same table, named after the role; recursive M:N → a table with two foreign keys to the same table.
- Ternary → one table with three foreign keys; its key is all three, or two of them when the third side has a 1.
- ISA → one table with a type column, a superclass table plus subclass tables (the safe default), or subclass tables only (total and disjoint only).
- Category → its own table with a surrogate key; aggregation → the outer relationship references the whole key of the inner relationship's table.
- Count tables: one per entity type, M:N relationship, ternary and multi-valued attribute; 1:1 and 1:N add only foreign keys.
- خۆیی
1:N← بۆ هەمان بە ناوی ڕۆڵەکەوە؛ خۆییM:N← بە دوو بۆ هەمان . - سێیانی ← یەک بە سێ ؛ هەر سێکیانە، یان دووانیان کاتێک لای سێیەم 1ـی هەبێت.
- ISA ← یەک بە جۆر، لەگەڵ (هەڵبژاردنی سەلامەت)، یان تەنها (تەنها بۆ تەواو و جیاکراوە).
- ← تایبەت بە ؛ ← دەرەوە ئاماژە بە هەموو ناوەوە دەکات.
- ژماردنی : یەک بۆ هەر جۆرێکی ،
M:N، سێیانی و ؛1:1و1:Nتەنها زیاد دەکەن.
Which ISA mapping is the safe default, and why?کام ڕێگەی گۆڕینی ISA هەڵبژاردنی سەلامەتە و بۆچی؟
A superclass table plus one table per subclass whose primary key is also a foreign key to the superclass. It works for every kind of specialisation and repeats nothing.
لەگەڵ یەک بۆ هەر کە بۆ . بۆ هەموو جۆرێکی کار دەکات و هیچ دووبارە ناکاتەوە.
How do the tables enforce 'only a member may chair the department'?خشتەکان چۆن «تەنها ئەندامێک دەتوانێت سەرۆکی بەش بێت» دەسەپێنن؟
chairs has a two-column foreign key (lecturer_no, dept_code) to member_of, so a chair must be an existing membership of that same department.
chairs دوو- (lecturer_no, dept_code) بۆ member_of هەیە، بۆیە سەرۆک دەبێت ئەندامێتییەکی هەبووی هەمان بەش بێت.
Why does a recursive foreign key such as supervisor_no often use ON DELETE SET NULL?بۆچی کلیلێکی بیانیی خۆیی وەک supervisor_no زۆرجار ON DELETE SET NULL بەکار دەهێنێت؟
So that the subordinates stay when their supervisor leaves; their supervisor_no simply becomes NULL until a new one is set.
بۆ ئەوەی ژێردەستەکان بمێننەوە کاتێک سەرپەرشتیارەکەیان دەڕوات؛ supervisor_noـیان دەبێتە NULL تا یەکێکی نوێ دادەنرێت.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Database Keysکلیلەکانی بنکەدراوە
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Silberschatz, Korth and Sudarshan: Database System Concepts, 7th ed., Chapter 6 slides (roles, non-binary relationships, l..h limits, specialisation, aggregation, reduction to schemas, crow's foot and UML)
- University of Houston–Clear Lake, CSCI 5333: ER- and EER-to-relational mapping (steps 1–9)
- Clark University, CS220: ER to relational mapping slides (options 8A–8D, union types)
- SQLite: Foreign Key Support (PRAGMA foreign_keys, ON DELETE actions, composite foreign keys)
- Wikipedia: Enhanced entity–relationship model (subclasses, specialisation, generalisation, inheritance, categories, aggregation; cites Elmasri & Navathe, Fundamentals of Database Systems, ch. 8–9)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.