Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases...
-
Upload
wesley-adams -
Category
Documents
-
view
234 -
download
1
Transcript of Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases...
Chapter 3: Relational Chapter 3: Relational Model IModel I
Structure of Relational Structure of Relational DatabasesDatabases
Convert a ER Design to a Convert a ER Design to a Relational DatabaseRelational Database
RelationRelation
Another name for Another name for tabletable Columns – attributesColumns – attributes Rows – tuplesRows – tuples Content of a table – instance of a relationContent of a table – instance of a relation
Attribute TypesAttribute Types
Each attribute of a relation has a nameEach attribute of a relation has a name The set of allowed values for each attribute is The set of allowed values for each attribute is
called the called the domaindomain of the attribute of the attribute Attribute values are (normally) required to be Attribute values are (normally) required to be
atomicatomic, that is, indivisible, that is, indivisible E.g. multivalued attribute values are not atomicE.g. multivalued attribute values are not atomic E.g. composite attribute values are not atomicE.g. composite attribute values are not atomic
The special value The special value nullnull is a member of every is a member of every domaindomain
Example of a RelationExample of a Relation
FormallyFormally
Given sets Given sets DD11, , DD22, …. , …. DDnn a a relationrelation r r is a is a subset of subset of DD11 x x DD2 2 x … x x … x DDnn
Thus a relation is a set of n-tuples (Thus a relation is a set of n-tuples (aa11,, a a22, , …, …, aann) where each ) where each aaii DDii
Relation Relates ThingsRelation Relates Things
Things:Things:customer-namecustomer-name = {Jones, Smith, Curry, = {Jones, Smith, Curry,
Lindsay}Lindsay}customer-streetcustomer-street = {Main, North, Park} = {Main, North, Park}customer-citycustomer-city = {Harrison, Rye, Pittsfield} = {Harrison, Rye, Pittsfield}
RelationRelationThen Then rr = { (Jones, Main, Harrison), = { (Jones, Main, Harrison),
(Smith, North, Rye), (Smith, North, Rye), (Curry, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} (Lindsay, Park, Pittsfield)} is a relation over is a relation over customer-name x customer-customer-name x customer-street x customer-citystreet x customer-city
Relation SchemaRelation Schema
AA11, , AA22, …, , …, AAnn are are attributesattributes
RR = ( = (AA11, , AA22, …, , …, AAnn ) is a ) is a relation schemarelation schema
E.g. E.g. Customer-schemaCustomer-schema = = ( (customer-name, customer-street, customer-name, customer-street, customer-citycustomer-city))
rr((RR) is a ) is a relationrelation on the on the relation schema Rrelation schema R
E.g.E.g. customer (Customer-schema)customer (Customer-schema)
Relation InstanceRelation Instance The current values (The current values (relation instancerelation instance) )
of a relation are specified by a tableof a relation are specified by a table An element An element tt of of rr is a is a tupletuple, ,
represented by a represented by a row row in a tablein a table
JonesSmithCurryLindsay
customer-name
MainNorthNorthPark
customer-street
HarrisonRyeRye
Pittsfield
customer-city
customer
attributes(or columns)
tuples(or rows)
Relations are UnorderedRelations are Unordered
Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
DatabaseDatabase
In relational database, a database In relational database, a database consists of many relationsconsists of many relations Both things and their relationships are Both things and their relationships are
represented by relationsrepresented by relations Normalization theory (Chapter 7) deals Normalization theory (Chapter 7) deals
with how to design relational schemaswith how to design relational schemas
KeysKeys Let K Let K R R K K is a is a superkeysuperkey of of RR if values for if values for KK are are
sufficient to identify a unique tuple of each sufficient to identify a unique tuple of each possible relation possible relation r(R)r(R) by “possibleby “possible r r” we mean a relation ” we mean a relation rr that could that could
exist in the enterprise we are modeling.exist in the enterprise we are modeling. Example: {Example: {customer-name, customer-streetcustomer-name, customer-street} }
andand { {customer-namecustomer-name} } are both superkeys of are both superkeys of CustomerCustomer, if no two , if no two customers can possibly have the same name.customers can possibly have the same name.
Candidate KeysCandidate Keys
KK is a is a candidate keycandidate key if if KK is minimal is minimal
Example: {Example: {customer-namecustomer-name} is a candidate key } is a candidate key for for CustomerCustomer, since it is a superkey (assuming , since it is a superkey (assuming no two customers can possibly have the same no two customers can possibly have the same name), and no subset of it is a superkey.name), and no subset of it is a superkey.
Convert ER to Relational Convert ER to Relational DatabaseDatabase
Entity Entity relationrelation Attributes attributesAttributes attributes Primary key primary key Primary key primary key
Relationship Relationship relationrelation Attributes attributesAttributes attributes We will talk about primary key later We will talk about primary key later
Weak entity set Weak entity set relationrelation Attributes attributesAttributes attributes We will talk about primary key laterWe will talk about primary key later
Representing Entity Sets as Representing Entity Sets as TablesTables
A strong entity set reduces to a table A strong entity set reduces to a table with the same attributes.with the same attributes.
The primary key of the entity set The primary key of the entity set becomes the primary key of the becomes the primary key of the relation.relation.
Composite AttributesComposite Attributes
Composite attributes are flattened out by Composite attributes are flattened out by creating a separate attribute for each creating a separate attribute for each component attributecomponent attribute E.g. given entity set E.g. given entity set customecustomer with composite r with composite
attribute attribute namename with component attributes with component attributes first-first-name name and and last-namelast-name the table corresponding to the table corresponding to the entity set has two attributesthe entity set has two attributes name.first-namename.first-name and and name.last-name.last-namename
Multivalued AttributesMultivalued Attributes
A multivalued attribute M of an entity E is A multivalued attribute M of an entity E is represented by a separate table EMrepresented by a separate table EM Table EM has attributes corresponding to the primary Table EM has attributes corresponding to the primary
key of E and an attribute corresponding to multivalued key of E and an attribute corresponding to multivalued attribute Mattribute M
E.g. Multivalued attribute E.g. Multivalued attribute dependent-namesdependent-names of of employeeemployee is represented by a table is represented by a table employee-dependent-namesemployee-dependent-names(( employee-id, dname employee-id, dname))
Each value of the multivalued attribute maps to a Each value of the multivalued attribute maps to a separate row of the table EMseparate row of the table EM
E.g., an employee entity with primary key John and E.g., an employee entity with primary key John and dependents Johnson and Johndotir maps to two rows: dependents Johnson and Johndotir maps to two rows: (John, Johnson) and (John, Johndotir) (John, Johnson) and (John, Johndotir)
ExampleExample
The relation(s) the ER mapped to?
1. customer(customer-id, first-name, last-name, middle-initial,date-of-birth,age,street-number,street-name,apartment-number,city,state,zip-code)
2. customer-phone(customer-id,phone-number)
Representing Weak Entity Representing Weak Entity SetsSets
A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set
The primary key of the relation consists of The primary key of the relation consists of the union of the primary key of the strong the union of the primary key of the strong entity set and the discriminator of the weak entity set and the discriminator of the weak entity set.entity set.
Weak Entity ExampleWeak Entity Example
Representing Representing Relationship Sets as Relationship Sets as
TablesTables A many-to-many relationship set is A many-to-many relationship set is
represented as a table with attributes from represented as a table with attributes from the primary keys of the two participating the primary keys of the two participating entity sets, and any descriptive attributes of entity sets, and any descriptive attributes of the relationship set. the relationship set. E.g.: table for relationship set E.g.: table for relationship set borrowerborrower The union of the primary keys of the related entity sets The union of the primary keys of the related entity sets
becomes a super key of the relation.becomes a super key of the relation.
Many-to-many relationship
What is the relationship borrower has an attribute “date”?
Representing Relationship Representing Relationship Sets as TablesSets as Tables
Many-to-one and one-to-many relationship sets that are total on the many-side can be represented by adding an extra attribute to the many side, containing the primary key of the one side E.g.: Instead of creating a table for relationship account-branch, add an attribute branch to the entity set account
the primary key of the “many” entity set the primary key of the “many” entity set becomes the primary key that represents becomes the primary key that represents the relationship and the many sidethe relationship and the many side
If participation is If participation is partialpartial on the many side, on the many side, replacing a table by an extra attribute in the replacing a table by an extra attribute in the relation corresponding to the “many” side relation corresponding to the “many” side could result in null valuescould result in null values
account(account-number,balance)
branch(branch-name,branch-city,assets)
account-branch(account-number,branch-name)
account(account-number,balance,branch-name)
branch(branch-name,branch-city,assets)
Redundancy!
?
Representing Relationship Representing Relationship Sets as TablesSets as Tables
For one-to-one relationship sets, either For one-to-one relationship sets, either side can be chosen to act as the “many” side can be chosen to act as the “many” sideside That is, extra attribute can be added to either That is, extra attribute can be added to either
of the tables corresponding to the two entity of the tables corresponding to the two entity sets sets
Determining Keys from E-R Determining Keys from E-R SetsSets
Strong entity setStrong entity set. The primary key of . The primary key of the entity set becomes the primary key of the entity set becomes the primary key of the relation.the relation.
Weak entity setWeak entity set. The primary key of the . The primary key of the relation consists of the union of the relation consists of the union of the primary key of the strong entity set and primary key of the strong entity set and the discriminator of the weak entity set.the discriminator of the weak entity set.
Determining Keys from E-R Determining Keys from E-R SetsSets
Relationship setRelationship set. The union of the . The union of the primary keys of the related entity sets primary keys of the related entity sets becomes a super key of the relation.becomes a super key of the relation. For binary many-to-one relationship sets, the For binary many-to-one relationship sets, the
primary key of the “many” entity set becomes primary key of the “many” entity set becomes the primary key that represent both the the primary key that represent both the relationship and the many side. Why?relationship and the many side. Why?
What about one-to-one relationship sets. Why?What about one-to-one relationship sets. Why? For many-to-many relationship sets, the union For many-to-many relationship sets, the union
of the primary keys becomes the relation’s of the primary keys becomes the relation’s primary key. Why?primary key. Why?
Representing Representing Specialization as TablesSpecialization as Tables
Method 1: Method 1: Form a table for the higher level entity Form a table for the higher level entity Form a table for each lower level entity set, Form a table for each lower level entity set,
include primary key of higher level entity set include primary key of higher level entity set and local attributesand local attributes
Person(Person(namename, street, city), street, city)
Customer(Customer(namename, credit-, credit-rating)rating)
Employee(Employee(namename, salary), salary)
Drawback: getting Drawback: getting information about, information about, e.g., e.g., employeeemployee requires accessing requires accessing two tablestwo tables
Representing Specialization as Representing Specialization as TablesTables
Method 2: Method 2: Form a table for each entity set with all local and Form a table for each entity set with all local and
inherited attributesinherited attributes If specialization is total, table for generalized If specialization is total, table for generalized
entity (entity (personperson) not required to store information) not required to store information Can be defined as a “view” relation containing union of Can be defined as a “view” relation containing union of
specialization tablesspecialization tables But explicit table may still be needed for foreign key But explicit table may still be needed for foreign key
constraintsconstraints
Person(Person(namename, street, city), street, city)
Customer(Customer(namename, street, city,credit-, street, city,credit-rating)rating)
Employee(Employee(namename, street,citysalary), street,citysalary)
Drawback: street Drawback: street and city may be and city may be stored redundantly stored redundantly for persons who are for persons who are both person and both person and customers/employeecustomers/employeess
ER for Banking EnterpriseER for Banking Enterprise
Schema Diagram for the Schema Diagram for the Banking EnterpriseBanking Enterprise
Convert the ER diagram to relational Convert the ER diagram to relational models.models.