the relational model chapter 3 cis 458 sungchul hong

46
The Relational Model Chapter 3 CIS 458 Sungchul Hong

Upload: kristian-hurd

Post on 14-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

The Relational Model

Chapter 3

CIS 458

Sungchul Hong

Last Class

• ERD– Entities, Relations,

Attributes

– It is not model specific

This Class

• Relational Model & Schema Conversion– Cf)– Hierarchical model– Network model– Object Oriented Model

• Cartesian product• Integrity

– Relational integrity– Entity integrity

Relational Database Management Systems

• Dominant data processing software– The estimates sales is between $15 billion - $20 billion

per year.

• Relational model– In the relational model, all data is logically structured

within relations (tables).

– Each relation has a name and is made up of named attributes (columns) of data.

– Each tuple (row) contains one value per attribute.

Relational Data Structure

• The relational model is based on the mathematical concept of a relation.

• Relation is a table with columns and rows.• Attribute: An attribute is a named column

of a relation• Domain: A domain is the set of allowable

values for one or more attributes.• Tuple: a tuple is a row of a relation

Relations

Relational Data Structure (2)

• Degree: The degree of a relation is the number of attributes it contains.– n-ary

• Cardinality: The cardinality of a relation is the number of tuples it contains.

• Relational database: A collection of normalized relations with distinct relation names.

Mathematical Relations

• Sets D1 and D2 where D1 = {2, 4} and D2={1, 3, 5}.

• Cartesian product D1 D2, is the set of all ordered pairs such that the first element is a member of D1 and the second element is a member of D2.

– D1 D2 ={(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}

Exercise

• E1 = {a, b, c}

• E2 = {3, 4, 5}

• E1 E2 = {(a,3), (a, 4), (a,5)

• (b,3), (b, 4), (b,5)

• (c,3), (c, 4), (c,5)}

Mathematical Relations (2)

• Any subset of this Cartesian product is a relation. D1= {2, 4} , D2={1, 3, 5}

– Relation R = {(2, 1), (4, 1)}

– R = { (x, y) | x D1, y D2, and y = 1}

– You want to find out some records not entire table.

– S = {(x, y) | x D1, y D2, and x= 2y}

– S = { (2,1) }

Mathematical Relations (3)

• D1 = {1, 3}, D2 = {2, 4}, D3 = {5, 6}

• D1 D2 D3 =

• {(1,2,5), (1,2,6), (1,4,5), (1,4,6)

• (3,2,5), (3,2,6), (3,4,5), (3,4,6)}

Database Relations

• Relation Schema: A named relation defined by a set of attribute and domain name pairs.

• Let A1, A2, …, An be attributes with domain D1, D2, …, Dn. Then the set {A1:D1, A2:D2, …, An:Dn} is a relation schema.

• A relation R defined by a relation schema S is a set of mappings from the attribute names to their corresponding domains.

• (A1:d1, A2:d2, … , An:dn) such that d1 D1, d2 D2, … , dn Dn

Database Relations (2)

• Example– Branch table– {branchNo: B005, street: 22Deer Rd, city: London,

postcode: SW1 4EH}– Relational instance

• Relational database schema: A set of relation schemas each with a distinct name.– If R1, R2, … , Rn are a set of relation schemas, then we

can write the relational database schema or relational schema R, as: R= {R1, R2, …, Rn}

BranchNo street city postcode

B005 22 Deer Rd London SW14EH

B007 16 Argyll St. Aberdeen AB2 3SU

B003 163 Main St Glasgow G11 9QX

B004 32 Manse Rd Bristol BS99 1NZ

B002 56 Clover Dr. London NW10 6EU

Branch

A1 A2A3 A4

D1 D2 D3 D4Domain

R1

R2

R3

R4

R5

R6

R={R1, R2, R3, R4, R5, R6}

Properties of Relations (1/2)

• The relation has a name that is distinct from all other relation names in the relational schema.

• Each cell of the relation contains exactly one atomic value.

• Each attribute has a distinct name.• The values of an attribute are all from the

same domain.

Properties of Relations (2/2)

• Each tuple is distinct: there are no duplicate tuples.

• The order of attributes has no significance.– Practically has significance.

• The order of tuples has no significance, theoretically.

• Normalized or first normal form

Relational Keys

• Superkey: An attribute, or set of attributes, that uniquely identifies a tuple within a relation.

• Candidate key: A superkey such that no proper subset is a superkey within the relation.– Uniqueness (need semantic information)– Irreducibility

Example: The CAR relation schema:

CAR(State, Reg#, SerialNo, Make, Model, Year)

has two keys

Key1 = {State, Reg#}, Key2 = {SerialNo},

which are also superkeys. {SerialNo, Make} is a superkey but not a key.

Relational Keys (2)

• Primary Key: The candidate key that is selected to identify tuples uniquely within the relation.

• The candidate keys that are not selected to be the primary key are called alternate keys.

• Foreign key: An attribute, or set of attributes, within one relation that matches the candidate key of some relation.– When an attribute appears in more than one relation, its

appearance usually represents a relationship between tuples of the two relations.

Schema Conversion

• Create a table for each entity– 1:1 choose one side and put a foreign key– 1:* put a foreign key in many side– *:* create a table for the relation

• Relation– *:* create a table – Ternary or higher degree

• Attribute– Multi-valued attribute

(1..1) (0..1)

Staff(staffNo, name, position, salary, branchNo)

Branch(branchNo, street, city, postcode)

Telephone (branchNo, telNo)OR

Staff(staffNo, name, position, salary,)

Branch(branchNo, street, city, postcode, staffNo)

Telephone (branchNo, telNo)

(1..*) (1..1)

Staff(staffNo, name, position, salary, manages, belongs)

Branch(branchNo, street, city, postcode)

Telephone (branchNo, telNo)

Domain [manages]= Branch.branchNo

Domain [belongs] = Branch.branchNo

staffNo fName

lName

manages

sex DOB salary branchNo

SL21 John White B005 M Oct/1/45 30000

B005

SG37 Ann Beech F Nov/10/60

12000

B003

SG14 David Ford M Mar/24/58

18000

B003

SA9 Mary Howe F 19/Feb/70

9000 B007

SG5 Susan Brand B003 F Jun/3/40 24000

B003

SL41 Julie Lee F Jun/13/65

9000 B005

Staff

BranchNo street city postcode

B005 22 Deer Rd London SW14EH

B007 16 Argyll St. Aberdeen AB2 3SU

B003 163 Main St Glasgow G11 9QX

B004 32 Manse Rd Bristol BS99 1NZ

B002 56 Clover Dr. London NW10 6EU

Branch (1..*)

(1..1)

PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent, privateOwnerNo,

businessOwnerNo, staffNo, branchNo)

Newspaper(newspaperName)

Advertisement (newspaperName, PropertyNo, dateAdvert, cost)

PropertyForRent NewsPaper

newsPaperName {PK}

◄Advertises

(1..*) (0..*)

dateAdvertCost

propertyNo{PK}StreetCityPostcodeRoomsRentprivateOwnerNo…

EmployeeSSN{pk}FNameMINITLNameBDateAddressSexSalary

DepartmentDnumber{pk}DnameMgrstartdateLocation[1..n]

Manages ►

◄Has1..1 1..0

1..* 1..1

ProjectPnumber{pk}PnamePlocation

Dependent

Dependent_NameSexBdateRelationship

Exercise

◄Depend

On1..1

0..*

◄Supervises

Supervisor

Supervisee

0..1

1..* In Charge of

Works_on ►

0..*

1..*

0..*

1..1

R1

R2

R3

R4

R5

R6

R={R1, R2, R3, R4, R5, R6}

Exercise

Representing Relational Database Schemas

• DreamHome case study– Branch (branchNo, street, city, postcode, manager)– Staff (staffNo, fName, lName, position, sex, DOB salary,

branchNo, supervisor)– PropertyForRent (propertyNo, street, city, postcode, type,

rooms, rent, privateOwnerNo, businessOwnerNo, staffNo, branchNo)

– Client (clientNo, fName, lName, telNo, prefType, maxRent)– Registration (clientNo, branchNo, staffNo, dateJoined)– Manager(staffNo, branchNo, MgrStartDate, bonus)

Schema

– Lease (leaseNo, PropertyNo, clientNo)– Preference (clientNo, preference)– Newspaper(newspaperName)– Advertisement (newspaperName, PropertyNo,

dateAdvert, cost)– PrivateOwner (privateOwnerNo, fName, lName,

address, telNo)– BusinessOwner (businessOwnerNo, businessName)

Relational Integrity

• Data Model– Definition, manipulative part

• Domain constraints form restrictions on the set of values allowed for the attributes of relation.

• Integrity rules– Constraints or restrictions that apply to all instances of

the database.

– Entity integrity, referential integrity

Relational Integrity (2)

• Null represents a value for an attribute that is currently unknown or is not applicable for this tuple.– Unknown, incomplete or exceptional data– Null is not zero nor white space.

Entity Integrity

• Base Relation: A named relation corresponding to an entity in the conceptual schema, whose tuples are physically stored in the database.– View (Virtual relation)

• Entity integrity: In a base relation, no attribute of a primary key can be null.

Referential Integrity

• Referential Integrity: If a 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 the foreign key value must be wholly null.

Enterprise Constraints

• Additional rules specified by the users or database administrators of a database.– A employee can not work more than 65 hours

per week.– Salary value must be a positive number.

Views

• A view is a virtual or derived relation.• A relation that does not necessarily exist in its

own right, but may be dynamically derived from one or more base relations.– Provide a powerful and flexible security mechanism.– Customized user data access– It can simplify complex operations.

• Updates are allowed on simple query with a single base relation.

• Not allowed with multiple base relations.• Not allowed with aggregation or grouping operations.

IS ASSIGNED TO►

Chairs►

PROFESSOR DEPARTMENT0..11..1

1..11..N

Exercise

PROFESSOR(PID, FName, MINIT, LName, DOB, Rank)

DEPARTMENT (DID, DName)

ENROLL►STUDENTS SECTION

1..60..35

Exercise

STUDENTS (SID, FName, LName, DOB, Major[1..3], Minor)

COURSE (COURSE#, C_Name, Description)

SECTION (Section_Num, Max_size)

COURSE

Has▼

o..*

1..1

Painter Painting GalleryDisplayed(1,1) (0..*)

(0..*) (1..1)Paint ► ►

Exercise

Painter (Painter_ID, FName, LName)

Paining (Painting_ID, Title, Date)

Gallery (Gallery_Name, Location)

Employee SkillsLearn ►

0..* 0..*

Expert Level

Exercise

Employee (EID, FName, LName)

Skill (Skill_Name)

Student ClassesTake ►

1..* 0..*

Grade

Exercise

STUDENTS (SID, FName, LName, DOB)CLASS (CLASS#, C_Name, Description)

Course Section1..1 0..*

Student

Take ▼

1..*

0..*

Has ►

◄Prerequisite

0..*0..*

Grade

Exercise (Recursive and many-to-many)

STUDENTS (SID, FName, LName, DOB)COURSE (COURSE#, C_Name, Description)SECTION (Section_Num, Max_size)

StudentSSN{pk}

Name

Major

Bdate

CourseEnroll

Course# Cname Dept

Text

ISBN{pk}

Publisher

Author

Adopt

Grade

Quarter

Quarter{pk}

1

N1

1

1 N

Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course:

STUDENT(SSN, Name, Major, Bdate)

COURSE(Course#, Cname, Dept)

ENROLL(SSN, Course#, Quarter, Grade)

BOOK_ADOPTION(Course#, Quarter, Book_ISBN)

TEXT(Book_ISBN, Book_Title, Publisher, Author)

Draw a relational schema diagram specifying the foreign keys for this schema.

Exercise