Intro to Relational Modelمقدمة في النموذج العلائقي
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- The structure of a relation: attributes, tuples, domains, atomic values and null.
- Relation schema and instance; relations are unordered.
- Keys: superkey, candidate key, primary key and foreign key.
- Relational algebra: select, project, union, set difference, intersection, Cartesian product, rename, composition and natural join.
أقسام الفصل
- 1Relations, attributes and tuplesالعلاقات والصفات والصفوف
- 2Relation schema and instanceمخطط العلاقة ونسختها
- 3Keysالمفاتيح
- 4Relational query languagesلغات الاستعلام العلائقية
- 5Select and projectالاختيار والإسقاط
- 6Union, set difference and intersectionالاتحاد والفرق والتقاطع
- 7Cartesian product, rename and compositionالضرب الديكارتي وإعادة التسمية وتركيب العمليات
- 8Natural joinالربط الطبيعي
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Attributes, tuples and domainsالصفات والصفوف
Relations, attributes and tuples
| ID | name | dept_name | salary |
|---|---|---|---|
| 10101 | Srinivasan | Comp. Sci. | 65000 |
| 12121 | Wu | Finance | 90000 |
| 15151 | Mozart | Music | 40000 |
| 22222 | Einstein | Physics | 95000 |
| 32343 | El Said | History | 60000 |
| 33456 | Gold | Physics | 87000 |
| 45565 | Katz | Comp. Sci. | 75000 |
| 58583 | Califieri | History | 62000 |
| 76543 | Singh | Finance | 80000 |
| 76766 | Crick | Biology | 72000 |
| 83821 | Brandt | Comp. Sci. | 92000 |
| 98345 | Kim | Elec. Eng. | 80000 |
The columns of a relation.
The rows of a relation.
The set of allowed values for each attribute (e.g., gender).
- Attribute values are (normally) required to be atomic; that is, indivisible.
- The special value null is a member of every domain; it indicates that the value is "unknown".
- The null value causes complications in the definition of many operations.
Relation schema and instanceالمخطط والنسخة
If A1, A2, …, An are attributes, R = (A1, A2, …, An) is a relation schema — e.g., instructor = (ID, name, dept_name, salary).
- Formally, given sets D1, D2, …, Dn, a relation r is a subset of D1 × D2 × … × Dn — a set of n-tuples (a1, …, an) where each ai ∈ Di.
- The current values (relation instance) are specified by a table; an element t of r is a tuple, represented by a row.
- Relations are unordered: the order of tuples is irrelevant (tuples may be stored in an arbitrary order).
| ID | name | dept_name | salary |
|---|---|---|---|
| 22222 | Einstein | Physics | 95000 |
| 12121 | Wu | Finance | 90000 |
| 32343 | El Said | History | 60000 |
| 45565 | Katz | Comp. Sci. | 75000 |
| 98345 | Kim | Elec. Eng. | 80000 |
| 76766 | Crick | Biology | 72000 |
| 10101 | Srinivasan | Comp. Sci. | 65000 |
| 58583 | Califieri | History | 62000 |
| 83821 | Brandt | Comp. Sci. | 92000 |
| 15151 | Mozart | Music | 40000 |
| 33456 | Gold | Physics | 87000 |
| 76543 | Singh | Finance | 80000 |
Superkeys, candidate keys and primary keysالمفاتيح
K ⊆ R is a superkey if values for K are sufficient to identify a unique tuple of each possible relation r(R). Example: {ID} and {ID, name} are both superkeys of instructor.
A superkey K is a candidate key if K is minimal. Example: {ID}.
One of the candidate keys is selected to be the primary key.
Foreign keysالمفتاح الأجنبي
A value in one relation must appear in another. The relation holding the foreign key is the referencing relation; the other is the referenced relation.
Schema of the university databaseمخطط قاعدة الجامعة
Keys
| Relation | Attributes | Arrows on slide 7 |
|---|---|---|
| classroom | building, room_number, capacity | لا تذكر الشرائح شيئًا هنا. |
| department | dept_name, building, budget | لا تذكر الشرائح شيئًا هنا. |
| course | course_id, title, dept_name, credits | dept_name → department |
| instructor | ID, name, dept_name, salary | dept_name → department |
| section | course_id, sec_id, semester, year, building, room_number, time_slot_id |
|
| teaches | ID, course_id, sec_id, semester, year |
|
| student | ID, name, dept_name, tot_cred | dept_name → department |
| takes | ID, course_id, sec_id, semester, year, grade |
|
| advisor | s_ID, i_ID |
|
| time_slot | time_slot_id, day, start_time, end_time | لا تذكر الشرائح شيئًا هنا. |
| prereq | course_id, prereq_id |
|
Relational query languagesلغات الاستعلام
- Query languages are procedural vs. non-procedural (declarative).
- "Pure" languages: relational algebra, tuple relational calculus, domain relational calculus — equivalent in computing power.
- Relational algebra is not Turing-machine equivalent.
The 6 basic operations of relational algebra (slide 9)
- select
- project
- union
- set difference
- Cartesian product
- rename
Notes (slide 23)
- Each query input is a table (or set of tables); each query output is a table.
- All data in the output table appears in one of the input tables.
- Aggregate functions: SUM, AVG, MAX, MIN.
Select (σ): choosing rowsالاختيار σ
Select and project
Selection of rows (tuples): returns rows of the input relation that satisfy the predicate.
| A | B | C | D |
|---|---|---|---|
| α | α | 1 | 7 |
| α | β | 5 | 7 |
| β | β | 12 | 3 |
| β | β | 23 | 10 |
المسألة
σ A=B ∧ D > 5 (r)
خطوات الحل
- Keep a row only if A equals B and D is greater than 5.
- (α, α, 1, 7): A = B and 7 > 5 → kept. (α, β, 5, 7): A ≠ B. (β, β, 12, 3): 3 is not > 5. (β, β, 23, 10): kept.
النتيجة
(α, α, 1, 7) and (β, β, 23, 10)
Project (Π): choosing columnsالإسقاط Π
Select and project
Selection of columns (attributes): outputs the specified attributes from all rows of the input relation and removes duplicate tuples from the output.
| A | B | C |
|---|---|---|
| α | 10 | 1 |
| α | 20 | 1 |
| β | 30 | 1 |
| β | 40 | 2 |
المسألة
Π A,C (r)
خطوات الحل
- Keep only columns A and C: (α, 1), (α, 1), (β, 1), (β, 2).
- Remove the duplicate (α, 1).
النتيجة
(α, 1), (β, 1), (β, 2)
Union, difference and intersectionالاتحاد والفرق والتقاطع
Union, set difference and intersection
| A | B |
|---|---|
| α | 1 |
| α | 2 |
| β | 1 |
| A | B |
|---|---|
| α | 2 |
| β | 3 |
| Operationالعملية | Resultالناتج |
|---|---|
| r ∪ s (union) | (α, 1), (α, 2), (β, 1), (β, 3) |
| r − s (set difference) | (α, 1), (β, 1) |
| r ∩ s (intersection) | (α, 2) |
- Note (slide 14): r ∩ s = r − (r − s).
Cartesian product (×)الضرب الديكارتي
The Cartesian product r × s pairs every row of r with every row of s (slide 15).
| A | B |
|---|---|
| α | 1 |
| β | 2 |
| C | D | E |
|---|---|---|
| α | 10 | a |
| β | 10 | a |
| β | 20 | b |
| γ | 10 | b |
| A | B | C | D | E |
|---|---|---|---|---|
| α | 1 | α | 10 | a |
| α | 1 | β | 10 | a |
| α | 1 | β | 20 | b |
| α | 1 | γ | 10 | b |
| β | 2 | α | 10 | a |
| β | 2 | β | 10 | a |
| β | 2 | β | 20 | b |
| β | 2 | γ | 10 | b |
Cartesian product: naming issueمشكلة التسمية
Cartesian product, rename and composition
If both relations have an attribute with the same name (here B), the result of r × s keeps both and names them r.B and s.B.
| A | B |
|---|---|
| α | 1 |
| β | 2 |
| B | D | E |
|---|---|---|
| α | 10 | a |
| β | 10 | a |
| β | 20 | b |
| γ | 10 | b |
| A | r.B | s.B | D | E |
|---|---|---|---|---|
| α | 1 | α | 10 | a |
| α | 1 | β | 10 | a |
| α | 1 | β | 20 | b |
| α | 1 | γ | 10 | b |
| β | 2 | α | 10 | a |
| β | 2 | β | 10 | a |
| β | 2 | β | 20 | b |
| β | 2 | γ | 10 | b |
Rename (ρ) and compositionإعادة التسمية والتركيب
Allows us to refer to a relation (say E) by more than one name: ρ x (E) returns the expression E under the name X. Example (slide 17): r × ρ s (r) has the attributes r.A, r.B, s.A, s.B.
| r.A | r.B | s.A | s.B |
|---|---|---|---|
| α | 1 | α | 1 |
| α | 1 | β | 2 |
| β | 2 | α | 1 |
| β | 2 | β | 2 |
المسألة
Composition (slide 18): σ A=C (r × s), with r and s from slide 15
خطوات الحل
- First build r × s (8 rows).
- Then keep the rows where A = C.
النتيجة
(α, 1, α, 10, a), (β, 2, β, 10, a), (β, 2, β, 20, b)
Natural join (⋈)الربط الطبيعي
For r on schema R and s on schema S, r ⋈ s is a relation on schema R ∪ S: for each pair of tuples tr from r and ts from s, if they have the same value on each of the attributes in R ∩ S, add a tuple t with tr's values on r and ts's values on s.
| A | B | C | D |
|---|---|---|---|
| α | 1 | α | a |
| β | 2 | γ | a |
| γ | 4 | β | b |
| α | 1 | γ | a |
| δ | 2 | β | b |
| B | D | E |
|---|---|---|
| 1 | a | α |
| 3 | a | β |
| 1 | a | γ |
| 2 | b | δ |
| 3 | b | ε |
المسألة
r ⋈ s = Π A, r.B, C, r.D, E (σ r.B = s.B ∧ r.D = s.D (r × s))
خطوات الحل
- Pair rows with the same B and the same D.
- (α, 1, α, a) matches (1, a, α) and (1, a, γ); (α, 1, γ, a) matches the same two; (δ, 2, β, b) matches (2, b, δ); the other rows of r match nothing.
النتيجة
(α, 1, α, a, α), (α, 1, α, a, γ), (α, 1, γ, a, α), (α, 1, γ, a, γ), (δ, 2, β, b, δ)
Worked example: courses and instructorsمثال المقررات
Natural join
| Course# | Course_Name | Credit_Hours | InstructorID |
|---|---|---|---|
| 320 | Database | 3 | 111 |
| 200 | Introduction to IS | 4 | 222 |
| 350 | DSS | 3 | 222 |
| InstructorID | Instructor_Name | Hours_Load | Salary |
|---|---|---|---|
| 111 | Sara | 12 | 9000 |
| 222 | Reem | 16 | 10000 |
| 333 | Haya | 14 | 12000 |
المسألة
Π Course#, Course_name (σ credit_hours < 4 (Course))
خطوات الحل
- σ keeps the courses with fewer than 4 credit hours: 320 and 350.
- Π keeps the columns Course# and Course_name.
النتيجة
(320, Database), (350, DSS) — slide 22
المسألة
Course ⋈ instructor
خطوات الحل
- The common attribute is InstructorID.
- Each course is paired with its instructor; instructor 333 (Haya) teaches no course, so she does not appear.
النتيجة
(320, Database, 3, 111, sara, 12, 9000), (200, Introduction to IS, 4, 222, reem, 16, 10000), (350, DSS, 3, 222, reem, 16, 10000) — slide 22
Summary of relational algebra operatorsملخص العمليات
Natural join
| Symbol (name)الرمز (الاسم) | Example of useمثال | What it outputsالناتج |
|---|---|---|
| σ (Selection) | σ salary >= 85000 (instructor) | Rows of the input relation that satisfy the predicate |
| Π (Projection) | Π ID, salary (instructor) |
|
| × (Cartesian product) | instructor × department | Every pair of rows from the two input relations (slide 15) |
| ∪ (Union) | Π name (instructor) ∪ Π name (student) | The union of tuples from the two input relations |
| − (Set difference) | Π name (instructor) − Π name (student) | The set difference of tuples from the two input relations |
| ⋈ (Natural join) | instructor ⋈ department | Pairs of rows that have the same value on all attributes that have the same name |
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Relations, attributes and tuplesالعلاقات والصفات والصفوف
- Relation schema and instanceمخطط العلاقة ونسختها
- Keysالمفاتيح
- Relational query languagesلغات الاستعلام العلائقية
- Select and projectالاختيار والإسقاط
- Union, set difference and intersectionالاتحاد والفرق والتقاطع
- Cartesian product, rename and compositionالضرب الديكارتي وإعادة التسمية وتركيب العمليات
- Natural joinالربط الطبيعي