Database Lect4 Relational Model
Database Lect4 Relational Model
Relational Model
The Relational Data Model
Complex
E/R diagrams Tables: file organization
column names: attributes and index
rows: tuples structures.
Relational Data Model
Core of majority of modern databases
concept of a Relation.
relation
name StockItem
Attribute Domain
ItemID string(4)
set of Description string(50)
attributes
Price currency/dollars
Taxable boolean
attribute attribute
names domains
Relational schema
birth_date, address)
Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street,
customer_city)
14
Key Constraints
key of R: A set of attributes SK of R such
that no two tuples will have the same value
for SK
If a relation has several candidate keys,
one is chosen arbitrarily to be the primary
key. The primary key attributes are
underlined.
Indicate a key by underlining the key attributes.
Example: If name is a key for Beers:
Beers(name, manf)
Entity Set to Relation
name category
price
Product
Start Year
name
makes Company
Product
Stock price
Start Year
name
makes Company
Product
Stock price
carries
Flight Aircraft
Flight
(flight#, arrival_airport, depart_airport, arrival_time,
depart_time)
uses
Airport (code, city)
Airport
ERD FROM DATASTORES FLIGHTS
Aircraft
Stopover Flight
Departs_from
Stops_at Leaves_from
Arrives_at
Airport
Multi-way Relationships to
Relations
name address
Product
Person
Purchase( , , )
ssn name
name addr name manf
1 2
Buddies Favorite
husband wife
Likes(drinker, beer)
Married Favorite(drinker, beer)
Married(husband, wife)
Buddies(name1, name2)
For one-one relation Married, we can
choose either husband or wife as key.
Weak Entity Sets, Relationships
Relations
Derived
Omit: these values are not stored in our tables
Later, we can produce these values using a query
Multi-valued
Relational model does not directly support!
However, as we have discussed, a multi-valued
attribute can be conceptualized as a new (weak)
entity, thus implying a separate table.
Composite and Multivalued Attributes
Reverse engineer
this relational
schema to find an
equivalent ER
schema.
Converting Binary Relationships
Many-to-many relationships
Relational model does not directly support!
However, each many-to-many relationship can be
conceptualized as a new (associative) entity, thus
implying a separate table.
The identifier for the associative entity is the
combination of the identifiers for the two related
entities.
Thus, for the separate table we create for an M:M
relationship, its primary key columns include the primary key
columns for both of the related tables.
Finding the Keys
If the relation comes from a many-many relationship, the
key of the relation is the set of all attribute keys in the
relations corresponding to the entity sets
name
buys Person
Product
EMPLOYEE(Ssn, Name, …)
FK
DEPARTMENT(Name, Number, Mgr, Mgr_start_date)
Step 3: Binary 1:1 Relationships
Approach 2: Merged Relation
AJB(x, y, p, q, r)
or
AJB(x, y, p, q, r)
Step 4: Binary 1:N Relationships
1:N Relationships become foreign key at N side
any relationship attributes also go to N side
BANK_BRANCH(Bank_code, Branch_No,
Addr)
Step 4: Binary 1:N Relationships
1:N Relationships become foreign key at N side
any relationship attributes also go to N side
BANK_BRANCH(Bank_code, Branch_No,
Addr)
Step 5: Binary M:N Relationships
M:N Relationships must become a new relation
contains FKs to both related entities
combined FKs become PK for new relations
relationship attributes go in new relation
A_C(Acct, Cust)
ACCOUNT(Acct_no, Type,
Balance, Bank, Branch)
Step 6: Multivalued Attributes
Multivalued attributes must become new
relations
FK to associated entity type
PK is whole relation
SUPPLIER(SName)
PROJECT(Proj_name)
PART(Part_no)
SUPPLY(SName, PName, Part, Quantity)
Completed Bank Schema
CUSTOMER(Ssn, Name, Addr, Phone)
BANK(Code, Name, Addr)
ACCOUNT(Acct_no, Type, Balance, Bank, Branch)
LOAN(Loan_no, Type, Amount, Bank, Branch)
BANK_BRANCH(Bank_code, Branch_No, Addr)
A_C(Acct, Cust)
L_C(Loan, Cust)
50
Exercise
51
Exercise
Every professor teaches exactly one course (no more,
no less)
52
Practice
Professors have an SSN, a name, an age, a rank, and a
research specialty.
Projects have a project number, a sponsor name (e.g.,
NSF), a starting date, an ending date, and a budget.
54
Professors work in one or more departments, and for each department that
they work in, a time percentage is associated with their job
Graduate students have one major department in which they are working on
their degree.
Each graduate student must have another, more senior graduate student as
an advisor.
55
56
A company database needs to store information about employees
(identified by ssn, with salary and phone as attributes), departments
(identified by dno, with dname and budget as attributes), and
children of employees (with name and age as attributes).
Exercise
A company database needs to store information about
employees (identified by ssn, with salary and phone as
attributes), departments (identified by dno,
with dname and budget as attributes), and children of
employees (with name and age as attributes).
Employees work in departments; each department
is managed by an employee; a child must be identified uniquely
by name when the parent (who is an employee; assume that
only one parent works for the company) is known.
You set up a database company, ArtBase, that builds a product for art
galleries. The core of this product is a database with a schema that
captures all the information that galleries need to maintain.
Galleries keep information about artists, their names (which are
unique), birthplaces, age, and style of art. For each piece of artwork, the
artist, the year it was made, its unique title, its type of art (e.g., painting,
lithograph, sculpture, photograph), and its price must be stored. Pieces
of artwork are also classified into groups of various kinds, for example,
portraits, still lifes, works by Picasso, or works of the 19th century; a
given piece may belong to more than one group. Each group is
identified by a name (like those just given) that describes the group.
Finally, galleries keep information about customers. For each customer,
galleries keep that person’s unique name, address, total amount of
dollars spent in the gallery (very important!), and the artists and groups
of art that the customer tends to like. Draw the ER diagram for the
database
Galleries keep information about artists, their names (which are unique), •
birthplaces, age, and style of art.
For each piece of artwork, the artist, the year it was made, its unique title, its •
type of art (e.g., painting, lithograph, sculpture, photograph), and its price must
be stored.
• Pieces of artwork are also classified into groups of various kinds, for
example, portraits, still lifes, works by Picasso, or works of the 19th century;
a given piece may belong to more than one group.
• Each group is identified by a name (like those just given) that describes
the group.
• Finally, galleries keep information about customers. For each customer,
galleries keep that person’s unique name, address, total amount of dollars
spent in the gallery (very important!),
• and the artists and groups of art that the customer tends to like
Subclasses Relations
isa
color Ales
OO-Style
nam e manf name manf color
Bud A.B. Sum merBrew Pete's dark
Beers Ales
E/R Style
name manf name Color
Bud A.B. Sum merBrew dark
Sum merBrew Pete's
Beers Ales
Using NULLS
name manf color
Bud A.B. NULL
Sum merBrew Pete's dark
Beers
Design Issues
Use of entity sets vs. attributes