Fundamentos de SQL
Fundamentos de SQL
2
Announcements!
• Jupyter: setup on your laptops, we start using next week
• HW3 solutions: posted earlier (no FMs)
• Confidential or anonymous questions on HW?
- please post on Piazza "visible to instructors only"
• Anonymous question to instructor only: Google feedback form
• Exam1 (Mon Feb 12): Laptop, BlackBoard, Postgres, SQL only. Vote:
- Variant 1: closed book, one letter cheatsheet allowed
- Variant 2: open book, more time-constrained, graded very carefully
• Outline
- HW2
- ER modeling
3
HW2
4
Company/Product 395
Company Product
cid pname
cname price
city cid
5
Big IMDB schema (Postgres)
Casts
pid
mid
Actor
role
id
fname
lname
gender
6
ER modeling
7
Data modeling and Database Design Process
name
1. ER Diagram
Conceptual Model: Patient patient_of Doctor
("technology independent")
describe main data items zip name dno
1. Requirements analysis
- What is going to be stored? Technical and non-
technical people are
- How is it going to be used? involved
- What are we going to do with the data?
- Who should access the data?
10
Database Design Process
1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.
2. Conceptual Design
- A high-level description of the database
- Sufficiently precise that technical people can understand it
- But, not so precise that non-technical people can’t participate
3. More:
- Logical Database Design
- Physical Database Design
- Security Design
12
Database Design Process
1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.
14
Some comments on
Notations
15
Different sources, different notations
16
Comparison of ERD frameworks A variant of
"UML"
Chen's Crow's Feet
Attribute name
Attribute name Attribute name Attribute name
Attribute name
Attribute name
17
Comparison of ERD frameworks A variant of
"UML"
Chen's Crow's Feet
Attribute name
Attribute name Attribute name Attribute name
Attribute name
Attribute name
Color is not part
of the standard…
18
Attributes
Chen's Crow's Feet
19
Relationships
20
Types of Binary Relationships
21
Redo this ER diagram with Crow's feet notation
Source: http://en.wikipedia.org/wiki/Entity-relationship_model
27
IMDB movie database in Lucidchart
28
Entities
29
Entities and Entity Sets
• Entities & entity sets are the primitive unit of the E/R model
Product
These represent entity sets
Person
30
Entities and Entity Sets
• An entity set has attributes
- Represented by ovals attached to an entity set
31
Entities vs. Entity Sets
Entities are not explicitly
• Example: represented in E/R diagrams!
Entity
name category
Name: Xbox
price
Category: Total Name: My Little Pony Doll Entity
Multimedia System Category: Toy
Attribute Product
Price: $250 Price: $25
Product
Entity Set
32
Keys
• A key is a minimal set of attributes that uniquely identifies an entity.
The E/R model forces us to designate a single primary key, though there
may be multiple candidate keys 33
33
Identifiers (Keys)
• Identifier (Key): An attribute (or combination of attributes) that
uniquely identifies individual instances of an entity type
- Can be simple or composite
- Will not be null
- Will not change in value
• e.g., family name, or telephone number, or street address, if those can change over
time (say through marriage...)
- Substitute new, simple keys for long, composite keys
• Candidate Key: an attribute (or set of) that could be a key…satisfies
the requirements for being a key
• Primary Key
34
Naming Entities
Poor Examples Good Examples
FormerStudentFromIowa Student
Customers Customer
ClientsWhoCameToBigEvent Employee
ObscureRecmdForFrtherAction Invoice
Order Purchase Order
Flight
• Guidelines for naming entity types:
- Use singular nouns
- Names should be specific to the organization
- Be concise
- Abbreviations are ok, as long as they are standardized
- Event entity types should be named for the result of the event (e.g., "Order")
- Be consistent
35
Exercise (Part I): Entities / Attributes
• Identify the entities that appear on the report card
• Identify the attributes of each previously identified entity
Source: Hoffer, Ramesh, Topi, "Modern database management," 10th ed, 2010.
36
Attributes
• A property or characteristic of an entity type
• Classifications of attributes:
- Identifier Attributes
- Required versus Optional
- Stored versus Derived
- Single-Valued versus
Multivalued Attribute
- Simple versus Composite
Source: Hoffer, Ramesh, Topi, "Modern database management," 10th ed, 2010.
37
Example: Describe the Attributes
Multivalued
Attribute (e.g.,
SQL, Python, ...)
Derived
Attribute
Source: Hoffer, Ramesh, Topi, "Modern database management," 10th ed, 2010.
38
Naming Attributes
Poor Examples Good Examples
TheDayThatThisPersonEnrolled Date
Birth_Date
NumEnrollInSpecificClass NumberEnrolled
Student_Names StudentName
ClientLastName CourseID
Employee_ID
40
Example: modeling flights
• Assume you want to model "flights"
• Attributes: FlightNumber, Date, NumberOfPassengers
• What would be the key / identifier?
41
Identifier Examples: Simple and Composite
• Simple identifiers:
- Single attribute uniquely
identifies each entity instance
- Identifier attribute underlined
• Composite identifiers:
- Multiple attributes required to
uniquely identifies each entity
instance
- Identifier attribute underlined
and composite attributes listed
below in (parentheses)
Source: Hoffer, Ramesh, Topi, "Modern database management," 10th ed, 2010.
42
Example: modeling time-dependent data
• Assume you have an entity "product"
• The price can change over time
• You would like to preserve the history of prices and the time period
43
Example: modeling time-dependent data
• Assume you have an entity "product"
• The price can change over time
• You would like to preserve the history of prices and the time period
44
Example: modeling time-dependent data
• Assume you have an entity "product"
• The price can change over time
• You would like to preserve the history of prices and the time period
Time-stamping is
commonly done with
a multi-valued and
composite attribute
(or associative
entities: see later)
Source: Hoffer, Ramesh, Topi, "Modern database management," 10th ed, 2010.
45
Relationships
46
The R in E/R: Relationships
• A relationship is between two or more entities
name name
category
price
Product Makes Company
47
name category
name
price
makes Company
Product
stockprice
buys employs
Person
- Let A, B be sets 2 b
• A={1,2,3}, B={a,b,c,d} c
3
d
49
What is a Relationship?
• A mathematical definition: B=
A= 1 a
- Let A, B be sets 2 b
• A={1,2,3}, B={a,b,c,d} c
3
d
- A x B (the cross-product) is the set of all pairs (a,b)
• A B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), (3,a), (3,b), (3,c), (3,d)}
50
What is a Relationship?
• A mathematical definition: B=
A= 1 a
- Let A, B be sets 2 b
• A={1,2,3}, B={a,b,c,d}, c
3
d
- A x B (the cross-product) is the set of all pairs (a,b)
• A B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), (3,a), (3,b), (3,c), (3,d)}
51
What is a Relationship?
• A mathematical definition: B=
- Let A, B be sets A= 1 a
makes Company
Product
52
What is a Relationship?
name name
category
price
Product Makes Company
53
What is a Relationship?
Company Product
name name
category
price
Product Makes Company
Company C × Product P
Company Product
Company C × Product P
Company Product
C.name P.name
A relationship between entity sets P and C is a GizmoWorks Gizmo
subset of all possible pairs of entities in P and C, with GizmoWorks GizmoLite
tuples uniquely identified by P and C’s keys GadgetCorp Gadget
56
What is a Relationship?
• There can only be one relationship (instance) for every
unique combination of entities This follows from our
mathematical
definition of a
• This also means that the relationship is uniquely relationship- it’s a SET!
since name
name category
price
Product Makes Company
58
Decision: Relationship vs. Entity?
• Q: What does this say?
date
price
• A: A person can only buy a specific product once (on one date)
name
name category Purchase
price
ProductOf BuyerOf
Product Person
60
Overview: 3 important concepts for relationships
• Participation constraints:
mandatory or optional (equivalent to
minimum cardinality 0 or 1)
A B
Definition of Function:
1 1 4 X X X For nonempty sets A
and B, a function f from
3 X X A to B, denoted f:AB,
2 2 is a relation from A to B
2 X in which every element
of A appears exactly
3 3 B 1 once as the first
component of an
R 1 2 3 4 ordered pair in the
relation.
4 4
A
62