0% found this document useful (0 votes)
10 views

Lecture 1 - The Relational Model - template

DBMS NOTES

Uploaded by

myotherface16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 1 - The Relational Model - template

DBMS NOTES

Uploaded by

myotherface16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 1: The Relational Model

COMP 519: Advanced Databases

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 , . . . , 𝐷𝑛

The Cartesian product of 𝐷1 , 𝐷2 , . . . , 𝐷𝑛 , written

Notes:

Example: instructor Relation


Def’n: tuple variable

Notation:

Relational Database Schemas

Def’n: relation schema:

Notation:

Example:

Schema Diagram for the University database:

Note:
Keys

Def'n: Superkey:

Course-schema (course_id, title, dept_name, credits)

Q: Which of the following could be superkeys?

{course_id}
{course_id, title, dept_name, credits}
{title, dept_name}
{title, dept_name, credits}

Def'n: Candidate key:

Q: For relations on the Course-schema, what are possible candidate keys?

Def'n: Primary key:

Example:

Note:

Artificial Attributes:

Notation:

Fundamental Property of a Superkey

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

Referencing relation of the foreign key:

Referenced relation:

Example: Describe {dept_name} in terms of its role in the course relation and the department relation.

Referential Integrity Constraints:

Formally, if K is a foreign key in relation r1 referencing relation r2, then for each tuple t1 ∈ r1,
Relational Algebra

Def'n: Relational algebra:

Two kinds of relational operations:

• Fundamental Operations

• Nonfundamental Operations

Fundamental Operations of Relational Algebra

The Select Operation:

Notation: σpredicate(relation)

Example: Select all courses in the math department:

Selection Predicate can contain:


Example: Select all three credit courses in the math department.

The Project Operation:

Notation: attribute list(relation)

Example: Retrieve unique department names from the course 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.

Could we write this composition a different way?

The Union Operation

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:

Note that the standard set union symbol is used.

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)

Question: What is the |r1 x r2|, i.e. the cardinality of r1 x r2?

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

σbuilding.capacity = building.capacity(building × capacity(σroom_no = “244E”(building)))

Solution:

Notation: ρnew_name(old_name)

Example: Find a list of all of the classrooms with the same capacity as 244E:

Example: Find the class with the highest number of credits

Strategy
Non-Fundamental Operations of Relational Algebra

The Set Intersection Operation

Example: to find the buildings that have both classrooms and department offices:

Note:

Q: Why intersection is not fundamental?

r∩s=

The Natural Join Operation

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.

Natural Join Operation:

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

The Theta Join:

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.

Approach with the Division Operator:

1. Find a list of all classroom buildings:

2. Get a list of all course id’s and classrooms

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

Notation: assignment is written ←

Example: Suppose we want to compute takes ⋈ course:

Notes:
Outer Joins

r⟕s

r⟖s

r⟗s

r ⋉ s
Aggregation

Example:

Express the following SQL query as a relational algebra expression

select name, sum(credit)


from student left natural join takes
group by id, name;

You might also like