w03_1_INF280_Relational_Modelx

download report

Transcript w03_1_INF280_Relational_Modelx

Relational Model
DEFINITION AND PROPERTIES
CONSTRAINTS AND SCHEMAS UPDATE
ER | EER  RELATIONAL
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
1
Relational Model: Definition and Properties
Relational Model represents database as a
collection of relations.
• A relation may be considered as a table of values,
where each row represents a collection of related
values, forming an instance, a fact, corresponding
with an entity in an entity-set.
• Table-name and columns-names are used to
interpret the meaning of values in each row and
define relational schema.
• Each value in a column is drawn by from the same
value-set (data-type).
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
2
Relational Model: Definition and Properties
In original definition of Relational Model:
Tables are called
Rows –
Column-names –
Data-type –
D. Christozov / G.Tuparov
relations;
tuples;
attributes;
domain.
INF 280 Database Systems:
Relational Model
3
Relational Model: Definition and Properties
• Domain is a set of allowable values for one or more
attributes
• Degree is a number of attributes in a relation.
• Cardinality is a number of tuples in a relation.
Alternative terminology
Formal terms
Alternative 1
Relation
Table
Tuple
Row
Attribute
Column
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
Alternative 2
File
Record
Field
4
Properties of Relations
• Relation name is distinct from all other relations.
• Each cell of relation contains exactly one atomic
(single) value.
• Each attribute has a distinct name within relation.
• Values of an attribute (column) are all from the
same domain.
• Order of attributes has no significance.
• Each tuple is distinct; there are no duplicate
tuples.
• Order of tuples has no significance, theoretically.
To ensure efficiency, tuples in a table are ordered according
to primary key.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
5
Relational Constraints (1)
Key constraints (uniqueness constraint):
• Superkey (Key) –
– Formally - a set of attributes of a relation R is
called Superkey (SK), if no any two distinct tuples
t1 and t2 in a state r of R have the same value for
attributes belonging to SK:
t1[SK] <> t2[SK]
– Informally - an attribute or a set of attributes that
uniquely identifies a tuple within a relation.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
6
Relational Constraints (2)
• Key (candidate key) is a minimal Superkey — a
Superkey from which we cannot remove an attribute
and still, the remaining set of attributes, is Superkey
(the uniqueness constraint holds).
• Primary Key - candidate key selected to identify tuples
Selection of PK affects performance!
uniquely within relation.
• Alternate Keys - candidate keys that are not selected
to be the primary key.
• Foreign Key - An attribute or set of attributes within
one relation that matches candidate key of some
Defining foreign keys ensures
(possibly the same) relation.
database integrity!
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
7
Relational integrity (1)
• Null
– Represents a value for an attribute that is
currently unknown or is not applicable for
this tuple.
– Deals with incomplete or exceptional data.
– Null represents the absence of a value and
is not the same as zero or spaces, which are
values.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
8
Relational integrity (2)
2 principal rules for the relational model:
• Entity Integrity
– In a base relation, no attribute of a primary key
can be null.
• Referential Integrity
– If foreign key exists in a relation, either the foreign
key value must match a candidate key value of
some tuple in its home relation or foreign key
value must be wholly null.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
9
Relational integrity (3)
• Enterprise Constraints
– Additional rules specified by users or database
administrators.
– Eg., the maximum number of staff in a branch.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
10
Company Database: Relational Schema
EMPLOYEE
Fname
Minit
Lname
SSN
Bdate
Address
Sex
Salary
SupSSN
DNO
WORKS_ON
ESSN
DEPARTMENT
Dname
Dnumber
MGRSSN
PNO
Hours
MGRSDate
PROJECT
Pname
LOCATION
D_num
Pnumber
Plocation
DNum
D_location
DEPENDENT
ESSN
D. Christozov / G.Tuparov
D_name
INF 280 Database Systems:
Relational Model
Sex
Bdate
Relationship
11
Company Database: Relational State
EMPLOYEE
Fname
Minit
Lname
SSN
Bdate
Address
Sex
Salary
SupSSN
DNO
John
B
Smith
123456789
1965-01-09
…TX…
M
30000
3334455555
5
Franklin
T
Wong
333445555
1956-12-08
…TX…
M
40000
888665555
5
Alicia
J
Zelaya
999887777
1968-01-19
…TX…
F
25000
987654321
4
Jennifer
S
Wallace
987654321
1941-06-02
…TX…
F
43000
888665555
4
Ramesh
K
Narayan
666884444
1962-09-15
…TX…
M
38000
333445555
5
Joyce
A
English
453453453
1972-07-31
…TX…
F
25000
333445555
5
Ahmad
V
Jabbar
987987987
1969-03-29
…TX…
M
25000
987654321
4
James
E
Borg
888665555
1937-11-10
…TX…
M
55000
Null
1
DEPARTMENT
Dname
Dnumber
MGRSSN
MGRSDate
Research
5
333445555
1988-05-22
Administration
4
987654321
1995-01-01
Headquarter
1
888665555
1981-06-19
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
12
Transition Constraints: Insert
The Insert Operation can violate any of the four types
of constraints discussed:
• Domain constraints can be violated if an attribute is
given value that does not appear in the corresponding
domain.
• Key constraints can be violated if a key value in the new
tuple t already exists in another tuple in the relation r(R).
• Entity integrity can be violated if the primary key of the
new tuple t is null.
• Referential integrity can be violated if the value of any
foreign key in t refers to a tuple that does not exist in the
referenced relation.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
13
Transition Constraints: Insert – Examples (1)
<‘Cecilia’, ‘F’, ‘Kolonsky’, null, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4>
This insertion violates the entity integrity constraint (null
for the primary key SSN)  Rejected!
<‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000,
‘987654321’, 4>
This insertion violates the key constraint because
another tuple with the same SSN value already exists in
the EMPLOYEE relation  Rejected!
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
14
Transition Constraints: Insert – Examples (2)
<‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-0405’, ‘6357 Windy Lane, Katy, TX’, F, 28000,
‘987654321’, 7>
This insertion violates the referential integrity constraint
specified on DNO because no DEPARTMENT tuple exists
with DNUMBER = 7  Rejected or cascade request for
insertion a new tuple into Department!
<‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-0405’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4>
This insertion satisfies all constraints Accepted!
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
15
Transition Constraints: Delete
• The Delete Operation can violate only referential
integrity: If the tuple being deleted is referenced via a
foreign key from other tuples in the database.
• To specify which tuples to delete, a conditions defined
on relation’s attributes have to be specified
Example:
Delete the tuple from WORKS_ON where
ESSN = ‘999887777’ and PNO = 10
 Accepted!
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
16
Transition Constraints: Delete - Examples
Delete the EMPLOYEE tuple where SSN = ‘999887777’
 Rejected!
This deletion is not acceptable, because FK in
WORKS_ON refers to this tuple. Executing deletion will
result in violation of referential integrity.
Delete the EMPLOYEE tuple where SSN = ‘333445555’
 Rejected!
This deletion will result in referential integrity
violations. The tuple involved is referenced by FK from
the EMPLOYEE, DEPARTMENT, WORKS_ON, and
DEPENDENT relations.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
17
Transition Constraints: Update
The Update Operation
• The Update operation is used to change the values of
one or more attributes in a tuple (or tuples) of some
relation R. It is necessary to specify a condition on
the attributes of the relation to select the tuple (or
tuples) to be modified.
• Examples
Update the SALARY of the EMPLOYEE tuple where
SSN = ‘999887777’ to 28000
Acceptable! (Why?)
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
18
Transition Constraints: Update - Examples
Update the DNO of the EMPLOYEE tuple with
SSN = ‘999887777’ to 1
Acceptable! (Why?)
Update the DNO of the EMPLOYEE tuple with
SSN = ‘999887777’ to 7
Unacceptable, because it violates referential integrity.
Update the SSN of the EMPLOYEE tuple with
SSN = ‘999887777’ to ‘987654321’
Unacceptable, because it violates primary key and
referential integrity constraints.
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
19
Q&A
Attention ! Next class
Quiz 1 Basic concepts, ER and EER
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
20
ER-to-Relational Mapping Algorithms
•
•
•
•
•
•
•
•
Step 1: Mapping of Regular Entity Types
Step 2: Mapping of Weak Entity Types
Step 3: Mapping of Binary 1:1 Relationship Types
Step 4: Mapping of Binary 1:N Relationship Types
Step 5: Mapping of Binary M:N Relationship Types
Step 6: Mapping of Multivalued Attributes
Step 7: Mapping of N-ary Relationship Types
Step 8: Options for Mapping Specialization or
Generalization
• Step 9: Mapping of Union Types (Categories)
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
21
Step 1: Mapping of Regular Entity Types
For each regular (strong) entity type E in the ER schema, create a
relation R that includes all the simple attributes of E. Include
only the atomic component attributes of a composite attribute.
Choose one of the key attributes of E as primary key for R. If the
chosen key of E is composite, the set of simple attributes that
form it will together form the primary key of R.
Fn
Min
Ln
Nam
Sex
SSN
Addr
Sal
EMPLOYEE
BD
EMPLOYEE
Fname
Minit
D. Christozov / G.Tuparov
Lname
SSN
Bdate
Address
INF 280 Database Systems:
Relational Model
Sex
Salary
22
Step 2: Mapping of Weak Entity Types
For each weak entity type W in the ER schema with owner entity
type E, create a relation R, and include all simple attributes of W
as attributes of R. In addition, include as foreign key attributes of R
the primary key attribute(s) of the relation(s) E. The primary key of
R is the combination of the primary key(s) of the owner(s) and the
partial key of the weak entity type W, if any.
Fn
Min
Ln
Nam
Sex
SSN
Addr
Sal
EMPLOYEE
DEPENDENT
BD
Name
Sex
BDate
Relation
DEPENDENT
ESSN
D_name
D. Christozov / G.Tuparov
Sex
Bdate
Relationship
INF 280 Database Systems:
Relational Model
23
Step 3: Mapping of Binary 1:1 Relationship Types
For each binary 1:1 relationship type R in the ER schema, identify
the relations S and T that correspond to the entity types
participating in R. Choose one of the relations—S, and include as
foreign key in S the primary key of T. It is better to choose an
entity type with total participation in R in the role of S. Include all
the simple attributes (or simple components of composite
attributes) of the 1:1 relationship type R as attributes of S.
Fn
Min
Ln
Nam
Sex
SSN
Number
Addr
Location
Name
1
EMPLOYEE
BD
Sal
1
Manage
S
DEPARTMENT
T
Start_Date
NumEmp
DEPARTMENT
Dname
D. Christozov / G.Tuparov
Dnumber
MGRSSN
INF 280 Database Systems:
Relational Model
MGRSDate
24
Step 4: Mapping of Binary 1:N Relationship Types
For each regular binary 1:N relationship type R, identify the relation
S that represents the participating entity type at the N-side of the
relationship type. Include as foreign key in S the primary key of the
relation T that represents the other entity type participating in R;
this is because each entity instance on the N-side is related to at
most one entity instance on the 1-side of the relationship type.
Include any attributes of the relationship type as attributes of S.
Fn
Min
Ln
Nam
Sex
SSN
Number
Addr
Sal
N
EMPLOYEE
1
Works_for
BD
Location
Name
T
DEPARTMENT
S
NumEmp
EMPLOYEE
Fname
Minit
D. Christozov / G.Tuparov
Lname
SSN
Bdate
Address
INF 280 Database Systems:
Relational Model
Sex
Salary
DNO
25
Step 5: Mapping of Binary M:N Relationship Types
For each binary M:N relationship type R, create a new relation S to
represent R. Include as foreign key attributes in S the primary keys
of the relations that represent the participating entity types; their
combination will form the primary key of S. Also include any
simple attributes of the M:N relationship type as attributes of S.
Fn
Min
Ln
Nam
Sex
SSN
Name
Addr
PROJECT
Sal
M
N
EMPLOYEE
Works_on
BD
Location
hours
Number
PROJECT
Pname
Pnumber
Plocation
WORKS_ON
DNum
ESSN
PNO
Hours
EMPLOYEE
Fname
Minit
D. Christozov / G.Tuparov
Lname
SSN
Bdate
Address
INF 280 Database Systems:
Relational Model
Sex
Salary
DNO
26
Step 6: Mapping of Multivalued Attributes
For each multivalued attribute A of entity E, create a new relation
R. This relation R will include an attribute(s) corresponding to A,
plus the primary key K of E as a foreign key in R referencing the
relation created for E. The primary key of R is the combination of A
and K. If the multivalued attribute is composite, we include its
atomic components.
Number
LOCATION
Location
Name
D_num
D_location
DEPARTMENT
NumEmp
DEPARTMENT
Dname
Dnumber
D. Christozov / G.Tuparov
MGRSSN
MGRSDate
INF 280 Database Systems:
Relational Model
27
Step 7: Mapping of N-ary Relationship Types
For each n-ary relationship type R, where
n > 2, create a new relation S to represent R.
Include as foreign key attributes in S the
primary keys of the relations that represent
the participating entity types. Also include any
simple attributes of the n-ary relationship type
as attributes of S. The primary key of S is
usually a combination of all the foreign keys
that reference the relations representing the
participating entity types.
INF 280 Database Systems:
D. Christozov / G.Tuparov
Relational Model
28
Correspondence between ER and Relational Models
ER MODEL
Entity type
RELATIONAL MODEL
Entity relation
1:1 or 1:N relationship type
M:N relationship type
n-ary relationship type
Foreign key (or relationship relation)
Relationship relation and two foreign keys
Relationship relation and n foreign keys
Simple attribute
Composite attribute
Multivalued attribute
Value set
Attribute
Set of simple component attributes
Relation and foreign key
Domain
Key attribute
Primary (or secondary) key
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
29
Step 8: Mapping Specialization or Generalization
Convert each specialization with m subclasses
{S1, S2, . . ., Sm} and (generalized) superclass C, where
the attributes of C are {k, a1, . . ., an} and k is the
(primary) key, into relation schemas using one of the
four following options:
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
30
Step 8: Option A
Multiple relations—superclass and subclasses
Create a
relation L for C
Create a relation Li
for each subclass Si,
with the attributes
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
31
Step 8: Option 8B
Multiple relations—subclass relations only
Create a relation Li for each
subclass Si, with the attributes
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
32
Step 8: Option 8C
Single relation with one type attribute
Create a single relation L, such that
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
This option is for a
specialization
whose
subclasses are disjoint,
and t is
defining
attribute, that indicates
the subclass to which each
tuple belongs.
This option has the
potential for generating a
large number of null
values.
33
Step 8: Option 8D
Single relation with multiple type attributes
Create a single relation schema L such that
This option is for a specialization whose subclasses are
overlapping (but may also work for a disjoint) specialization, and
each ti, 1  i  m, is a Boolean attribute indicating whether a
tuple belongs to subclass Si.
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
34
Step 9: Mapping of Union Types (Categories)
Mapping Shared Subclasses (Multiple Inheritance):
Any of options 8A – 8D may apply.
STEP 9: For mapping of UNION Types (Categories), a new
surrogate key attribute is specified, because the primary keys
of the super-classes are different and may not use any of
them to identify all entities in the category. The surrogate key
is introduce in any of the super-classes relations as a foreign
key to specify the correspondence.
bank
company
PERSON
person
OWNER
SSN
…
OwnerID
…
OwnerID
OwnerID
u
BANK
owner
D. Christozov / G.Tuparov
BName
COMPANY
…
OwnerID
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
CName
35
Example from Lecture 2: Map ER  R
Quack Consulting is a computer consulting firm. It keeps the list of its clients (id, name, phone).
Each of them may have many different projects (id, description, start_date, due_to,
team_leader). A project may consists of several tasks (project_id, task_id, consultant, description,
start, due_to). Only one consultant is ever assigned to a given task. The time a consultant works
for a project is recorded in time table. In order to choose the appropriate consultant for a client
information about consultants and his/her specialty (including the billing_rate) is maintain as
well as some comments about his/her qualities important for Quack Consulting.
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
36
Example from Lecture 2: Map ER  R
Fancy Fruits is a specialty grocery store supplying a variety of boxed fruits to its customers.
The entities are customers, orders, stock, and vendors. The stock keeps track to current
inventory (quantities on-hand of the items and current price). The vendors lists the
wholesalers who supply Fancy Fruits with items, it includes the cost of item for Fancy Fruit
and geographical region, where the wholesaler is located (Hint: Consider that one vendor
could supply a number of items and one item could be supplied by a number of vendors different regions and costs). The customer also store information about the region of the
customer as well as customer_name INF
and
280phone.
Database Orders
Systems: defined the quantities of given item
37
D. Christozov / G.Tuparov
SQL Relational
- Single Table
Model
queries
ordered by a customer
Example from Lecture 2: Map ER  R
D_Price
D_code
Id
Quantity
Name
Price
Codes
M
N
Products
order
s
M
On_hand
Date
D_code
N
M
Date
Purchase
N
Name
address
Quantity
Customers
Retailers
d
Dealers
C_Id
D. Christozov / G.Tuparov
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
38
Example from Lecture 2: Map ER  R
R_Id
I_Id
Recipes
Ingredients
Description
Name
1
M
N
Activities
Cook
On_hand
Supplies
N
Price
Name
Goods
Name
M
Suppliers
Price
D. Christozov / G.Tuparov
Quantity
Address
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
39
Example from Lecture 2: Map ER  R
Name
Address
BANK BRANCH
Address
1
N
BRANCHES
Name
BANK OFFICE
1
Code
GRANTS
1
N
OPENS
Acct No
N
1
ACCOUNT
ISSUES
Type
N
Loan No
Type
Amount
DEBIT CARD
LOAN
Rate
Acct No
M
Start Date
A-C
Comfort Ins
M
Address
L-C
N
N
CUSTOMER
Clinum
Period
Name
SSN
Telephone
Employer
Entity-Relationship
Database
D. Christozov / Diagram
G.Tuparov
Home
Mobile
INF 280 Database Systems:
SQL Relational
- Single Table
Model
queries
Business
40
Q&A
Attention ! Next class
Quiz 2: ER  R
D. Christozov / G.Tuparov
INF 280 Database Systems:
Relational Model
41