Intro to Relational Modelمقدمة في النموذج العلائقي

الشريحة 1 من 17

مقدمة الفصل

ماذا ستتعلم في هذا الفصل

  1. The structure of a relation: attributes, tuples, domains, atomic values and null.
  2. Relation schema and instance; relations are unordered.
  3. Keys: superkey, candidate key, primary key and foreign key.
  4. Relational algebra: select, project, union, set difference, intersection, Cartesian product, rename, composition and natural join.
تفاصيل المصدرالفصل 2 · الشريحة ⁦2⁩
المصدر
الفصل 2 · الشريحة ⁦2⁩

Attributes, tuples and domainsالصفات والصفوف

Relations, attributes and tuples

Example of a relation: instructor (slide 2) — the attributes are the columns, the tuples are the rows
IDnamedept_namesalary
10101SrinivasanComp. Sci.65000
12121WuFinance90000
15151MozartMusic40000
22222EinsteinPhysics95000
32343El SaidHistory60000
33456GoldPhysics87000
45565KatzComp. Sci.75000
58583CalifieriHistory62000
76543SinghFinance80000
76766CrickBiology72000
83821BrandtComp. Sci.92000
98345KimElec. Eng.80000
Attributes (columns)الصفات
The columns of a relation.
Tuples (rows)الصفوف
The rows of a relation.
Domainالنطاق
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.

A designer states that dept_name may take only the names of existing departments. In the relational model, this collection of permitted dept_name values is the attribute's:

تفاصيل المصدرالفصل 2 · الشرائح ⁦2–3⁩
المصدر
الفصل 2 · الشرائح ⁦2–3⁩

Relation schema and instanceالمخطط والنسخة

Relation schema
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).
instructor relation with unordered tuples (slide 5) — the same twelve tuples as slide 2, in another order
IDnamedept_namesalary
22222EinsteinPhysics95000
12121WuFinance90000
32343El SaidHistory60000
45565KatzComp. Sci.75000
98345KimElec. Eng.80000
76766CrickBiology72000
10101SrinivasanComp. Sci.65000
58583CalifieriHistory62000
83821BrandtComp. Sci.92000
15151MozartMusic40000
33456GoldPhysics87000
76543SinghFinance80000

instructor = (ID, name, dept_name, salary) is an example of a:

تفاصيل المصدرالفصل 2 · الشرائح ⁦4–5⁩
المصدر
الفصل 2 · الشرائح ⁦4–5⁩

Superkeys, candidate keys and primary keysالمفاتيح

Superkeyمفتاح فائق
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.
Candidate keyمفتاح مرشح
A superkey K is a candidate key if K is minimal. Example: {ID}.
Primary keyمفتاح أساسي
One of the candidate keys is selected to be the primary key.

For the instructor relation, {ID} and {ID, name} are both superkeys. Which of them is a candidate key?

تفاصيل المصدرالفصل 2 · الشريحة ⁦6⁩
المصدر
الفصل 2 · الشريحة ⁦6⁩

Foreign keysالمفتاح الأجنبي

Foreign key constraint
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.

من الشرائح

  • dept_name in instructor is a foreign key from instructor referencing department.

dept_name in instructor is a foreign key referencing department. Which statement is correct?

تفاصيل المصدرالفصل 2 · الشريحة ⁦6⁩
المصدر
الفصل 2 · الشريحة ⁦6⁩

Schema of the university databaseمخطط قاعدة الجامعة

Keys

Figure 2.9 (slide 8) and the schema diagram (slide 7). **Bold** = underlined on the slides (the primary key). The last column lists the arrows of slide 7 (foreign key → referenced relation).
RelationAttributesArrows on slide 7
classroombuilding, room_number, capacityلا تذكر الشرائح شيئًا هنا.
departmentdept_name, building, budgetلا تذكر الشرائح شيئًا هنا.
coursecourse_id, title, dept_name, creditsdept_name → department
instructorID, name, dept_name, salarydept_name → department
sectioncourse_id, sec_id, semester, year, building, room_number, time_slot_id
  • course_id → course
  • building, room_no → classroom
teachesID, course_id, sec_id, semester, year
  • ID → instructor
  • course_id, sec_id, semester, year → section
studentID, name, dept_name, tot_creddept_name → department
takesID, course_id, sec_id, semester, year, grade
  • ID → student
  • course_id, sec_id, semester, year → section
advisors_ID, i_ID
  • s_id → student
  • i_id → instructor
time_slottime_slot_id, day, start_time, end_timeلا تذكر الشرائح شيئًا هنا.
prereqcourse_id, prereq_id
  • course_id → course
  • prereq_id → course
تفاصيل المصدرالفصل 2 · الشرائح ⁦7–8⁩
المصدر
الفصل 2 · الشرائح ⁦7–8⁩
ملاحظات حول المصدر (1)
  • الشريحة 7 تكتب room_no والشريحة 8 تكتب room_number للصفة نفسها؛ أُبقي كل اسم كما في شريحته.

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.

Which of the following is NOT one of the 6 basic operations of relational algebra?

تفاصيل المصدرالفصل 2 · الشرائح ⁦9, 23⁩
المصدر
الفصل 2 · الشرائح ⁦9, 23⁩

Select (σ): choosing rowsالاختيار σ

Select and project

Select (σ)
Selection of rows (tuples): returns rows of the input relation that satisfy the predicate.
Relation r (slide 10)
ABCD
αα17
αβ57
ββ123
ββ2310

المسألة

σ A=B ∧ D > 5 (r)

r(A, B, C, D) = (α, α, 1, 7), (α, β, 5, 7), (β, β, 12, 3), (β, β, 23, 10). How many rows does σ A=B ∧ D>5 (r) return?

تفاصيل المصدرالفصل 2 · الشريحة ⁦10⁩
المصدر
الفصل 2 · الشريحة ⁦10⁩

Project (Π): choosing columnsالإسقاط Π

Select and project

Project (Π)
Selection of columns (attributes): outputs the specified attributes from all rows of the input relation and removes duplicate tuples from the output.
Relation r (slide 11)
ABC
α101
α201
β301
β402

المسألة

Π A,C (r)

r(A, B, C) = (α, 10, 1), (α, 20, 1), (β, 30, 1), (β, 40, 2). How many rows does Π A,C (r) return?

تفاصيل المصدرالفصل 2 · الشريحة ⁦11⁩
المصدر
الفصل 2 · الشريحة ⁦11⁩

Union, difference and intersectionالاتحاد والفرق والتقاطع

Union, set difference and intersection

Relation r (slides 12–14)
AB
α1
α2
β1
Relation s (slides 12–14)
AB
α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).

r(A, B) = (α, 1), (α, 2), (β, 1) and s(A, B) = (α, 2), (β, 3). What is r − s?

تفاصيل المصدرالفصل 2 · الشرائح ⁦12–14⁩
المصدر
الفصل 2 · الشرائح ⁦12–14⁩

Cartesian product (×)الضرب الديكارتي

The Cartesian product r × s pairs every row of r with every row of s (slide 15).

Relation r (slide 15)
AB
α1
β2
Relation s (slide 15)
CDE
α10a
β10a
β20b
γ10b
r × s (slide 15): 2 × 4 = 8 rows
ABCDE
α1α10a
α1β10a
α1β20b
α1γ10b
β2α10a
β2β10a
β2β20b
β2γ10b

r has 2 rows and s has 4 rows. How many rows does r × s have?

تفاصيل المصدرالفصل 2 · الشريحة ⁦15⁩
المصدر
الفصل 2 · الشريحة ⁦15⁩
ملاحظات حول المصدر (1)
  • جدول الشريحة 24 يضع أمام الضرب الديكارتي وصف الربط الطبيعي («أزواج الصفوف التي لها القيمة نفسها في الصفات المتشابهة الاسم»)، بينما مثال الشريحة 15 يُخرج كل الأزواج. الدرس يتبع الشريحة 15.

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.

Relation r (slide 16)
AB
α1
β2
Relation s (slide 16)
BDE
α10a
β10a
β20b
γ10b
r × s (slide 16)
Ar.Bs.BDE
α1α10a
α1β10a
α1β20b
α1γ10b
β2α10a
β2β10a
β2β20b
β2γ10b
تفاصيل المصدرالفصل 2 · الشريحة ⁦16⁩
المصدر
الفصل 2 · الشريحة ⁦16⁩

Rename (ρ) and compositionإعادة التسمية والتركيب

Rename (ρ)
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 × ρs(r) (slide 17), with r = (A, B): (α, 1), (β, 2)
r.Ar.Bs.As.B
α1α1
α1β2
β2α1
β2β2

المسألة

Composition (slide 18): σ A=C (r × s), with r and s from slide 15

What does ρ x (E) return?

تفاصيل المصدرالفصل 2 · الشرائح ⁦17–18⁩
المصدر
الفصل 2 · الشرائح ⁦17–18⁩

Natural join (⋈)الربط الطبيعي

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.
Relation r (slide 20)
ABCD
α1αa
β2γa
γ4βb
α1γa
δ2βb
Relation s (slide 20)
BDE
1aα
3aβ
1aγ
2bδ
3bε

المسألة

r ⋈ s = Π A, r.B, C, r.D, E (σ r.B = s.B ∧ r.D = s.D (r × s))

Which statement describes the natural join r ⋈ s?

تفاصيل المصدرالفصل 2 · الشرائح ⁦19–20⁩
المصدر
الفصل 2 · الشرائح ⁦19–20⁩

Worked example: courses and instructorsمثال المقررات

Natural join

Course (slide 21) — InstructorID is marked FK
Course#Course_NameCredit_HoursInstructorID
320Database3111
200Introduction to IS4222
350DSS3222
Instructor (slide 21) — InstructorID is marked PK
InstructorIDInstructor_NameHours_LoadSalary
111Sara129000
222Reem1610000
333Haya1412000

المسألة

Π Course#, Course_name (σ credit_hours < 4 (Course))

المسألة

Course ⋈ instructor

Course(Course#, Course_Name, Credit_Hours, InstructorID) = (320, Database, 3, 111), (200, Introduction to IS, 4, 222), (350, DSS, 3, 222). What is Π Course#, Course_name (σ credit_hours < 4 (Course))?

تفاصيل المصدرالفصل 2 · الشرائح ⁦21–22⁩
المصدر
الفصل 2 · الشرائح ⁦21–22⁩

Summary of relational algebra operatorsملخص العمليات

Natural join

Summary of relational algebra operators (slide 24; × follows slide 15 — see the source note)
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)
  • Specified attributes from all rows
  • duplicate tuples removed
× (Cartesian product)instructor × departmentEvery 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 ⋈ departmentPairs of rows that have the same value on all attributes that have the same name
تفاصيل المصدرالفصل 2 · الشريحة ⁦24⁩
المصدر
الفصل 2 · الشريحة ⁦24⁩
ملاحظات حول المصدر (1)
  • في الشريحة 24 يظهر وصف الربط الطبيعي أمام الضرب الديكارتي أيضًا؛ صُحح هنا وفق مثال الشريحة 15.

الخلاصة

تفاصيل المصدر
المصدر
الفصل 2

خريطة الفصل