Upload
alvin-singleton
View
222
Download
0
Embed Size (px)
Citation preview
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.TuparovINF 280 Database Systems:
Relational Model 2
Relational Model: Definition and Properties
In original definition of Relational Model:Tables are called relations;Rows – tuples;Column-names – attributes;Data-type – domain.
D. Christozov / G.TuparovINF 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 terminologyFormal terms Alternative 1 Alternative 2Relation Table FileTuple Row RecordAttribute Column Field
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 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.TuparovINF 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.TuparovINF 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 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 (possibly the same) relation.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 7
Selection of PK affects performance!
Defining foreign keys ensures database integrity!
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.TuparovINF 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.TuparovINF 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.TuparovINF 280 Database Systems:
Relational Model 10
Company Database: Relational Schema
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 11
EMPLOYEE
Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO
DEPARTMENTDname Dnumber MGRSSN MGRSDate
PROJECT
Pname Pnumber Plocation DNum
DEPENDENT
WORKS_ON
ESSN PNO Hours
ESSN D_name Sex Bdate Relationship
LOCATION
D_num D_location
Company Database: Relational State
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 12
EMPLOYEE
Fname Minit Lname SSN Bdate Address Sex Salary SupSSN DNO
DEPARTMENTDname Dnumber MGRSSN MGRSDate
Research 5 333445555 1988-05-22
Administration 4 987654321 1995-01-01
Headquarter 1 888665555 1981-06-19
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
Transition Constraints: InsertThe 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.TuparovINF 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.TuparovINF 280 Database Systems:
Relational Model 14
Transition Constraints: Insert – Examples (2)
<‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘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-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000, null, 4> This insertion satisfies all constraints Accepted!
D. Christozov / G.TuparovINF 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.TuparovINF 280 Database Systems:
Relational Model 16
Transition Constraints: Delete - ExamplesDelete 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: UpdateThe 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.
• ExamplesUpdate the SALARY of the EMPLOYEE tuple where SSN = ‘999887777’ to 28000Acceptable! (Why?)
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 18
Transition Constraints: Update - ExamplesUpdate 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.TuparovINF 280 Database Systems:
Relational Model 19
Q & AAttention ! Next class
Quiz 1 Basic concepts, ER and EER
D. Christozov / G.TuparovINF 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.TuparovINF 280 Database Systems:
Relational Model 21
Step 1: Mapping of Regular Entity TypesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 22
Sal
Fn
Nam
Min Ln
AddrSex
BD
SSN EMPLOYEE
Fname Minit Lname SSN Bdate Address Sex Salary
EMPLOYEE
Step 2: Mapping of Weak Entity TypesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 23
Sal
Fn
Nam
Min Ln
AddrSex
BD
SSN EMPLOYEE
Name Sex BDate Relation
DEPENDENT
ESSN D_name Sex Bdate Relationship
DEPENDENT
Step 3: Mapping of Binary 1:1 Relationship TypesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 24
Dname Dnumber MGRSSN MGRSDate
DEPARTMENT
Sal
Fn
Nam
Min Ln
AddrSex
BD
SSN EMPLOYEE
Number
LocationName
DEPARTMENT
NumEmp
Manage11
Start_Date
TS
Step 4: Mapping of Binary 1:N Relationship TypesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 25
Fname Minit Lname SSN Bdate Address Sex Salary DNO
EMPLOYEE
Sal
Fn
Nam
Min Ln
AddrSex
BD
SSN EMPLOYEE
Number
LocationName
DEPARTMENT
NumEmp
Works_forN
1
ST
Step 5: Mapping of Binary M:N Relationship TypesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 26
Fname Minit Lname SSN Bdate Address Sex Salary DNO
Pname Pnumber Plocation DNumESSN PNO Hours
EMPLOYEE
Works_onN M
hours
Name
Location
Number
PROJECTSal
Fn
Nam
Min Ln
AddrSex
BD
SSN EMPLOYEE
PROJECTWORKS_ON
Step 6: Mapping of Multivalued AttributesFor 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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 27
Number
LocationName
DEPARTMENT
NumEmp
Dname Dnumber MGRSSN MGRSDate
DEPARTMENT
D_num D_location
LOCATION
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.
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 28
Correspondence between ER and Relational Models
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 29D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 29
ER MODEL RELATIONAL MODELEntity type Entity relation1:1 or 1:N relationship type Foreign key (or relationship relation)M:N relationship type Relationship relation and two foreign keysn-ary relationship type Relationship relation and n foreign keysSimple attribute AttributeComposite attribute Set of simple component attributesMultivalued attribute Relation and foreign keyValue set DomainKey attribute Primary (or secondary) key
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.TuparovINF 280 Database Systems:
SQL - Single Table queries 30D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 30
Step 8: Option A Multiple relations—superclass and subclasses
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 31D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 31
Create a relation L for C
Create a relation Li
for each subclass Si, with the attributes
Step 8: Option 8BMultiple relations—subclass relations only
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 32D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 32
Create a relation Li for each subclass Si, with the attributes
Step 8: Option 8CSingle relation with one type attribute
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 33D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 33
Create a single relation L, such that
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.
Step 8: Option 8DSingle relation with multiple type attributes
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 34D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 34
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.
Step 9: Mapping of Union Types (Categories)
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 35D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 35
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.
u
owner
bank company person
OwnerID
OWNER
PERSON
SSN … OwnerID
BName … OwnerID CName … OwnerID
BANK COMPANY
Example from Lecture 2: Map ER R
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 36D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 36
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.
Example from Lecture 2: Map ER R
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 37D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 37
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 and phone. Orders defined the quantities of given item ordered by a customer
Example from Lecture 2: Map ER R
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 38D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 38
Retailers
M
Id
Name
Price
On_hand
Products
Codes
D_codeD_Price
PurchaseDate
Quantity
C_Id
Name
address Customers Dealersd
N
Quantity
D_code
orders Date
N
M
N
M
Example from Lecture 2: Map ER R
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 39D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 39
RecipesR_Id
Description
Activities
IngredientsI_Id
Name
On_hand
GoodsName
Price
Cook
N
1 M
Suppliers
Supplies
N
M
Price
Quantity
Name
Address
Example from Lecture 2: Map ER R
D. Christozov / G.TuparovINF 280 Database Systems:
SQL - Single Table queries 40D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 40
BANK BRANCH BANK OFFICE
LOAN
CUSTOMER
ACCOUNT DEBIT CARD
A - C
GRANTS
L - C
OPENS
BRANCHES
Code
Name Address Address Name
ISSUES
Loan No
Type
Amount
Rate
Period
Clinum
SSN Telephone
Name
Employer
Home Mobile Business
Acct No
Comfort InsAcct No
Type
Start Date
N1
1
1
1
N
N
N
M
N
M
N
Entity-Relationship
Database Diagram
Address
Q & AAttention ! Next class
Quiz 2: ER R
D. Christozov / G.TuparovINF 280 Database Systems:
Relational Model 41