0% found this document useful (0 votes)
19 views32 pages

2 Relational Model Intro

The document outlines the agenda for a database systems course, focusing on the relational model and its components such as relations, schemas, and keys. It emphasizes the importance of data models, superkeys, foreign keys, and integrity constraints in database design. The session also includes exercises related to the Movies schema and introduces future topics like SQL and relational algebra.

Uploaded by

hokumura032
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)
19 views32 pages

2 Relational Model Intro

The document outlines the agenda for a database systems course, focusing on the relational model and its components such as relations, schemas, and keys. It emphasizes the importance of data models, superkeys, foreign keys, and integrity constraints in database design. The session also includes exercises related to the Movies schema and introduces future topics like SQL and relational algebra.

Uploaded by

hokumura032
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/ 32

CS377: Intro.

to Database Systems
The Relational Model
Instructor: Prof. Nosayba El-Sayed
Spring 2025
Good Morning(ish)!
Today’s Agenda…

▪ The Relational Model


▪ Worksheet (Example): The Movies Schema
Announcements
▪ You’ve been auto-enrolled in Piazza and Gradescope
– If you didn’t receive a notification, let me know!

▪ Office hours schedule to be updated this evening on


Canvas; meanwhile, feel free to email me with any
questions you have!

▪ TA Info updated: Yuxin Zhu is a brilliant and very


friendly PhD student in our CS/Bioinformatics
program, with experience in database systems
Last week…
DB systems to the rescue
▪ Databases were primarily designed to provide
Physical Data Independence
▪ “The three-schema architecture” [Textbook Sec 2.2]
Separates user programs from the physical database!

Hides details of physical storage! describes


the structure of the data types, entities,
operations, etc. to the DB users

Describes physical storage structure of the DB


=> Uses a physical data model

5
What do we mean by a “data model”?
▪ Every DBMS is based on some data model:
a notation for describing data, including
• the structure of the data
• constraints on the content of the data
• operations on the data

▪ Some specific data models:


• network & hierarchical data models — of historic interest
• relational data model
• semistructured data model

6
But first… note today’s worksheet page3

7
We’re interested in the relational data
model
▪ Main concept is a “relation.”
Based on the concept of relations in math.
▪ Can think of as tables of rows and columns.
Teams Name Home Field Coach
Rangers Runnymede CI Tarvo Sinervo
Ducks Humber Public Maeve Mahar
Tom Cole
Choppers High Park

Games Home team Away team Home goals Away goals


Rangers Ducks 3 0
Ducks Choppers 1 1
Rangers Choppers 4 2
Choppers Ducks 0 5
8
Relations in Math
▪ A domain is a set of values.
▪ Suppose D1, D2, ... Dn are domains.
– The Cartesian (cross) product D1 x D2 x ... x Dn
is the set of all tuples <d1, d2, ..., dn> such that
d1 ∊ D1, d2 ∊ D2, ..., dn ∊ Dn.
– I.e., every combination of
a value from D1, a value from D2 etc.

▪ A (mathematical) relation on D1, D2, ... Dn


is a subset of the Cartesian product.

9
Example
▪ Example of a mathematical relation
– Let A = {p, q, r, s}, B = {1, 2, 3} and C = {100, 200}.
– R = {<q, 2, 100>, <s, 3, 200>, <p, 1, 200>}
is a relation on A, B, C.
tuple/row
▪ Our database tables are relations too.

▪ Example
Games Home team Away team Home goals Away goals
Rangers Ducks 3 0
Ducks Choppers 1 1
Rangers Choppers 4 2
Choppers Ducks 0 5
10
Example
▪ Example of a mathematical relation
– Let A = {p, q, r, s}, B = {1, 2, 3} and C = {100, 200}.
– R = {<q, 2, 100>, <s, 3, 200>, <p, 1, 200>}
is a relation on A, B, C.
tuple/row
▪ Our database tables are relations too.

▪ Example
{<Rangers, Ducks, 3, 0>, <Ducks, Choppers, 1, 1>,
<Rangers, Choppers, 4, 2>, <Choppers, Ducks, 0, 5>}

11
Relation schemas vs instances
▪ Schema: definition of the structure of the relation.
▪ Example:
-Teams have 3 attributes: name, home field, coach.
-No two teams can have the same name.
▪ Notation for expressing a relation’s schema
Teams(Name, HomeField, Coach)

▪ Instance: particular data in the relation.


▪ Instances change constantly; schemas rarely.

12
Terminology
Name Home Field Coach
Teams
Rangers Runnymede CI Tarvo Sinervo
Ducks Humber Public Maeve Mahar
Tom Cole
Choppers High Park

Crullers WTCS Anna Liu

▪ relation (table)
▪ attribute (column)
Optionally, we can specify that attributes have domains; like
types in a programming language
▪ tuple (row)
▪ arity of a relation: number of attributes (columns)
▪ cardinality of a relation: number of tuples (rows)
13
Relations are sets
▪ A relation is a set of tuples, which means:
– there can be no duplicate tuples

– order of the tuples doesn't matter

▪ In another model, relations are bags — a


generalization of sets that allows duplicates.
▪ Commercial DBMSs use this model.
▪ But for now, we will stick with relations as sets.

14
Database schemas and instances

▪ Database schema: a set of relation schemas

▪ Database instance: a set of relation instances

15
Superkeys
▪ Superkey: a set of one or more attributes whose combined
values are unique:
– I.e., no two tuples can have the same values
on all of these attributes.
▪ Example:
– A relation called Course, with attributes: department code, course
number, and course name.
– One tuple might be
<“cs”, “377”, “Introduction to Databases”>
– What is a superkey for this relation?

▪ Questions..
– Does every relation have a superkey?
– Can a relation have more than one superkey?

16
Superkeys
▪ Superkey: a set of one or more attributes whose combined
values are unique:
– I.e., no two tuples can have the same values
on all of these attributes.
▪ Example:
– A relation called Course, with attributes: department code, course
number, and course name.
– One tuple might be
<“cs”, “377”, “Introduction to Databases”>
– What is a superkey for this relation?

▪ Questions..
– Does every relation have a superkey? (yes)
– Can a relation have more than one superkey? (yes)

17
Keys
▪ Key: a minimal superkey.
– I.e., you may not remove an attribute from a key, and still have
a set of attributes whose combined values are unique.

▪ What is a key for the Course relation?


Course(deptCode, cNumber, cName)
▪ Can a relation have more than one key?
Student(student#, netID, surname, firstname, gpa)
▪ We underline attributes in the schema to indicate that they
form a key.
Teams(Name, HomeField, Coach)
▪ Aside: Called “superkey” because it is a superset of some
key. (Not necessarily a proper superset.)
18
Coincidence vs key
▪ If a set of attributes is a key for a relation:
– It does not mean merely that "there are no duplicates” in
a particular instance of the relation
– It means that in principle there cannot be any.
▪ Often we have to invent an artificial new attribute
to ensure all tuples will be unique.
– This predates databases. E.g., SSN, ISBN number.

▪ A key is a kind of integrity constraint.

19
Example: Movies schema

20
Example: Movies schema

21
References between relations
▪ Relations often refer to each other.
▪ Example:
In the Roles relation, the tuple about Han Solo needs to say he is played
by Ford.
▪ Rather than repeat information already in the Artists table, we store
Ford’s key.

22
References between relations

Q: If aID is a key for Artists, does that mean a particular aID can
appear only once in Roles?

23
Foreign keys
▪ If the referring attribute refers to a key attribute in
another table, it is called a foreign key

Roles[aID] ⊆ Artists[aID]

▪ This gives us a way to refer to a single tuple in that


relation!
▪ A foreign key may need to have several attributes.
24
Declaring foreign keys
▪ We use this notation to express relationships
between attribute values in two relations:
R1[X] ⊆ R2[Y]
▪ Example: Roles[aID] ⊆ Artists[aID]
▪ R[A] notation:
– R is a relation and
A is a list of attributes in R.
– R[A] is the set of all tuples from R,
but with only the attributes in list A.

25
Referential integrity constraints
▪ These R1[X] ⊆ R2[Y] relationships are called referential
integrity constraints or inclusion dependencies.
▪ Not all referential integrity constraints are foreign key
constraints.
▪ For example, we could say
Artists[aID] ⊆ Roles[aID]
▪ Notice: we are not referring to a unique tuple.

R1[X] ⊆ R2[Y] is a foreign key constraint iff


Y is a key for relation R2

26
Figuring out integrity constraints is part
of the process: designing a schema
▪ Mapping from the real world to a relational schema is
surprisingly challenging and interesting.
▪ There are always many possible schemas.
▪ Two important goals:
1- Represent the data well.
For example, avoid constraints that prevent expressing things that
occur in the domain.
2- Avoid redundancy.
▪ (Later, we’ll learn some elegant theory that provides sound
principles for good design…)

27
Exercise (Worksheet)
Time!
1. What is the cardinality of relation Movies?

2. What is the arity of relation Artists?

3. What is the key of relation Movies?

4. Change relation Artists in a way that violates its key constraint.

5. Change the database instance so that the constraint Roles[aID] ⊆ Artists[aID] is


violated.

6. Does any actor show up in relation Roles twice with same mID?

7. According to the schema, can an actor show up in relation Roles twice with same
mID?

8. According to the schema, is there any limit on the number of directors a movie can
have?

9. According to the schema, can exactly the same movie title be used for two
different movies?
FAQs
▪ Movies Schema: “Is there a limit to the # of directors a
movie can have?”
o Can’t we just add a new tuple for the same movie, only with
different director names?

o Problem: from the database


system’s point of view, the only
way of identifying a unique movie
is using its mID field.

8 | Star Wars IV | Jack | 1977| 126


30
What’s next…
▪ We will later learn how to use SQL (Structured Query
Language) to
– define a database’s structure,
– put data in it, and
– write queries on it.

▪ First we’ll learn how to write queries in


relational algebra.
– Relational algebra is the foundation for SQL.
– Other important concepts, like query optimization, are
defined in terms of RA.
31
[Break Slide]
Let’s have a 2-minute break…

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy