Lecture 1 - The Relational Model - template
Lecture 1 - The Relational Model - template
Topics:
Relations, tuples, and schemas
Keys: Superkeys, Candidate Keys, Primary Keys, and Foreign Keys
Fundamental Operations of Relational Algebra: select, project, rename, Cartesian product, union, set
difference
Nonfundamental operations: set intersection, natural join, division, assignment
Reading:
Chapter 2
Introduction
Relational data model:
A relation on sets 𝐷1 , 𝐷2 , . . . , 𝐷𝑛
Notes:
Notation:
Notation:
Example:
Note:
Keys
Def'n: Superkey:
{course_id}
{course_id, title, dept_name, credits}
{title, dept_name}
{title, dept_name, credits}
Example:
Note:
Artificial Attributes:
Notation:
If R is a relation schema, then K ⊆ R is a superkey for R, if and only if for every relation r(R) with t1 ∈ r
and t2 ∈ r and t1 t2, t1[K] t2[K]
Foreign Keys
Referenced relation:
Example: Describe {dept_name} in terms of its role in the course relation and the department relation.
Formally, if K is a foreign key in relation r1 referencing relation r2, then for each tuple t1 ∈ r1,
Relational Algebra
• Fundamental Operations
• Nonfundamental Operations
Notation: σpredicate(relation)
Note:
We can compose a select and a project operation, because the input for both are relations, and
the output of both are relations.
Example: Create a relation with the names of all departments that have 2 credit courses.
Goal: Get a list of all buildings on campus (from classroom and department schemas).
Since relations are sets of tuples, unioning the relations seems sensible.
Challenge: However, the relations are on different schemas (and so have different types).
Solution:
Q: If a building is in both schemas, will it appear twice in the result of the above query?
The Cartesian Product Operation:
Cartesian product for relational algebra is like Cartesian product for sets, except that:
1)
2)
To restrict the result to only the meaningful tuples, we can use an appropriate selection.
Example: Find a list of all of the students and the names of courses that they have taken
The Rename Operation
Question: Suppose we want to find a list of all of the classrooms with the same capacity as 244E
Solution:
Notation: ρnew_name(old_name)
Example: Find a list of all of the classrooms with the same capacity as 244E:
Strategy
Non-Fundamental Operations of Relational Algebra
Example: to find the buildings that have both classrooms and department offices:
Note:
r∩s=
The result of takes × course puts students together with courses they didn’t take. Both a select
and a project are needed to make the result useful.
Notation: r1 ⋈ r2
To compute r ⋈ s:
1.
2.
3.
Question: If R ∩ S = ∅, what is r ⋈ s?
Example: Create a relation with instructor’s names and the names of the courses that teach
Formally: r ⋈Θ s = σ Θ (r × s)
Example: Using a Theta join, rewrite the query a relation with instructor’s names and the names
of the courses that teach
The Division Operation
Goal: Suppose we wanted to find the ids of the courses that have been taught in every
classroom building on campus.
3. Find course_id’s that are associated with each of the classroom buildings:
Formal Definition
Formally, the result of r ÷ s for r(R) and s(S) is a relation on schema R − S, where we require that
EX: Students that have taken every course offered by the math department.
The Assignment Operation
Notes:
Outer Joins
r⟕s
r⟖s
r⟗s
r ⋉ s
Aggregation
Example: