Lecture 02
Lecture 02
Lecture 02
Relational Model and Entity-Relationship Model
Bharatesh Chakravarthi
1
Review Relational Model
2
What is a Relation?
• Relation: made up of 2 parts:
• Schema: specifies name of relation, plus name and type of each column.
• Instance: a table with rows and columns.
• #rows = cardinality
• #columns = arity
arity
cardinality
3
Take-away Messages
• A database is a collection of relations (or tables)
• Each relation has a set of attributes (or columns)
• Each attribute has a name and a domain (or type)
• Each relation contains a set of tuples (or rows)
4
What is a key?
• A set of attributes K is a key for a relation R if
• In no instance of R will two different tuples agree on all attributes of K
• That is, K can serve as a “tuple identifier”
• If K can serve as a “tuple identifier”, it is called as a Superkey
• No proper subset of K satisfies the above condition
• That is, K is minimal
• Minimal superkey is called as a key
• Example:
Student (sid, name, login, age, gpa)
{sid, name}: superkey
{sid}: superkey, AND key
{name}: not superkey
5
More examples
6
More examples
8
More examples
9
More examples
10
More examples
12
Primary Keys
• Example:
• Address (street_address, city, state, zip)
• {street_address, city, state}
• {street_address, zip}
• DBA typically picks one as the “primary” key, and underline all its attributes,
e.g., Address (street_address, city, state, zip)
• Other keys are called candidate keys.
e.g., {street_address, city, state}
13
What is a Database?
A database is a collection of relations (or tables)
Example 1
14
What is a Database?
A database is a collection of relations (or tables)
Example 2
15
What is a Database?
A database is a
collection of relations
(or tables)
Example 3
16
Foreign Keys, Referential Integrity
• Foreign Key: Set of attributes ‘referring’ to a tuple in another
relation.
• Must correspond to the primary key of the other relation
• Like a ‘logical pointer’
• Foreign key constraints enforce referential integrity (i.e., no dangling
references)
17
Data Definition Language
(DDL)
18
Data Definition Language (DDL)
20
Common Data Types
• CHAR(n) (fixed length), VARCHAR(n) (variable length)
• TINYINT (1 byte), SMALLINT (2 bytes), INT (4 bytes), , BIGINT (8 bytes),
• NUMERIC(p,d) (precision (no. of digits) and scale (digits to right of the
decimal point), FLOAT, DOUBLE, REAL
• DATE, TIME
• BINARY(n), VARBINARY(n), BLOB (image, video, files)
21
Useful Non-standard Types
• TEXT
• BOOLEAN
• ARRAY
• Geometric primitives
• XML/JSON
• Some systems also support user-defined types.
22
Integrity Constraints
23
Primary Keys
• Single-column primary key:
24
Key declarations are part of the schema
CREATE TABLE enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid, cid))
25
Foreign Key References
• Single-column reference:
• Multi-column reference:
26
Foreign Keys in SQL
CREATE TABLE Enrolled (
sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid)
REFERENCES Students )
27
Data Manipulation Language
Basic SQL
28
Basic Queries: SFW statements
• SELECT A1, A2, …, An
FROM R1, R2, …, Rm
WHERE condition;
• Also called as SPJ (selection-projection-join) query - (selecting
rows, projecting columns, and joining tables) Select (σ), Project (π), Join (⋈)
29
Example Database
30
Example: reading a table
• SELECT * FROM User;
• Single-table query, so no cross product here
• WHERE clause is optional
• * is a short hand for “all columns”
31
Example: selection and projection
• Name of users under 18
• SELECT name
FROM User
WHERE age < 18
• When was Lisa born?
• SELECT 2025 - age
FROM User
WHERE name = 'Lisa’;
• SELECT list can contain expressions
• Can also use built-in functions such as SUBSTR, ABS, etc.
• String literals (case sensitive) are enclosed in single quotes
32
Example: join
• ID’s and names of groups with a user whose name contains
“Simpson”
33
Example: join
• ID’s and names of groups with a user whose name contains
“Simpson”
• SELECT Group.gid, Group.name
FROM User, Member, Group
WHERE User.uid = Member.uid
AND Member.gid = Group.gid
AND User.name LIKE '%Simpson%’;
• LIKE matches a string against a pattern
• % matches any sequence of zero or more characters
• Okay to omit table_name in table_name.column_name if column_name is
unique
34
Assignment 0
35
https://asu.instructure.com/courses/213606/assignments/6030571
36
37
Challenge:
How to Design a Database?
38
Scenario
• http://www.myimdb.com wants to store information about movies
• The steps:
• Requirement Analysis
• Film (title, type, year, actors, director)
• Movie person (ID, name, address, birthday)
• Actor(ID, picture, salary for each film)
• Director (ID)
• Award (name, year, film, organization)
• Organization (name, phone number)
• Conceptual Database Design: High level description of data to be stored (ER model)
• Logical Database Design: Translation of ER diagram to a relational database schema
(description of tables)
39
Scenario
• http://www.myimdb.com wants to store information about movies
• The steps:
• Requirement Analysis
• Conceptual Database Design: High level description of data to be stored using
Entity-Relationship model (ER model)
• Logical Database Design: Translation of ER diagram to a relational database
schema (description of tables)
40
The Output ER Model of
Conceptual Database Design
• Will explain how to
understand and
design the ER model
41
Entities, Entity Sets
• Entity: An object in
the world that can
be distinguished
from other objects
• Entity set: A set of
similar entities
• Examples of entity
sets:
42
Entities, Entity Sets
• Entity: An object in
the world that can
be distinguished
from other objects
• Entity set: A set of
similar entities
• Examples of entity
sets:
43
Attributes
• Attributes: Used to
describe entities
• All entities in the set
have the same
attributes
• A minimal set of
attributes that
uniquely identify an
entity is called a key
Attributes are drawn using
ovals
The names of the
attributes which make up
a key are underlined
44
Attributes
• Attributes: Used to
describe entities
• All entities in the set
have the same
attributes
• A minimal set of
attributes that
uniquely identify an
entity is called a key
Attributes are drawn using
ovals
The names of the attributes
which make up a primary key
are underlined
45
Example
id birthday
Actor
name address
46
Another Option for a Key?
birthday
id
Actor
name address
47
Relationships, Relationship Sets
• Relationship:
Association among two
or more entities
• Relationships may have
attributes
• Relationship Set: Set of
similar relationships
48
Relationships, Relationship Sets
• Relationship:
Association among two
or more entities
• Relationships may have
attributes
• Relationship Set: Set of
similar relationships
49
Example
title
birthday
id
Actor Acted In Film year
name
address type
phone
number
manager
id
Employee Work for
worker
name
address
51
n-ary Relationship
• An n-ary relationship set R involves exactly n entity sets: E1, …, En.
• Each relationship in R involves exactly n entities: e1 E1, …, en En
id Director name
id
Actor Produced Film title
name
Ternary Relationship
52
Key Constraints
• Key constraints specify
whether an entity can
participate in one, or more
than one, relationships in a
relationship set
• When there is no key
constraint, an entity can
participate any number of
times
• When there is a key
constraint, the entity can
participate at most one time
Key constraints are drawn
using an arrow from the entity
set to the relationship set
53
Key Constraints
• Key constraints specify
whether an entity can
participate in one, or more
than one, relationships in a
relationship set
• When there is no key
constraint, an entity can
participate any number of
times
• When there is a key
constraint, the entity can
participate at most one time
Key constraints are drawn
using an arrow from the entity
set to the relationship set
54
One-to-Many
✓A film is directed by at most one director
✓A director can direct any number of films
id
Director Directed Film title
name
id
Director Directed Film title
name
id
Director Directed Film title
name