# Chapter 3 CIS 458 Sungchul Hong

## Presentation on theme: "Chapter 3 CIS 458 Sungchul Hong"— Presentation transcript:

Chapter 3 CIS 458 Sungchul Hong
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 Cartesian product
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}

Branch A3 A4 A1 A2 D1 D2 Domain D3 D4 BranchNo street city postcode
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. NW10 6EU D1 D2 Domain D3 D4

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 Relation Attribute
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

Staff (1..*) Branch (1..1) staffNo fName lName manages sex DOB salary
branchNo SL21 John White B005 M Oct/1/45 30000 SG37 Ann Beech F Nov/10/60 12000 B003 SG14 David Ford Mar/24/58 18000 SA9 Mary Howe 19/Feb/70 9000 B007 SG5 Susan Brand Jun/3/40 24000 SL41 Julie Lee Jun/13/65 (1..*) Branch 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. NW10 6EU (1..1)

Newspaper(newspaperName)
PropertyForRent NewsPaper ◄Advertises newsPaperName {PK} propertyNo{PK} Street City Postcode Rooms Rent privateOwnerNo (1..*) (0..*) dateAdvert Cost PropertyForRent (propertyNo, street, city, postcode, type, rooms, rent, privateOwnerNo, businessOwnerNo, staffNo, branchNo) Newspaper(newspaperName) Advertisement (newspaperName, PropertyNo, dateAdvert, cost)

Exercise Employee Department Project Dependent ◄Supervises Supervisor
0..1 Manages ► Employee SSN{pk} FName MINIT LName BDate Address Sex Salary Department Dnumber{pk} Dname Mgrstartdate Location[1..n] 1..1 1..0 ◄Has 1..* 1..1 1..* 1..1 In Charge of► Supervisee 1..* Works_on ► 1..1 ◄Depend On 0..* 0..* 0..* Project Pnumber{pk} Pname Plocation Dependent Dependent_Name Sex Bdate Relationship

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)

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.

Exercise Chairs► PROFESSOR DEPARTMENT 1..1 0..1 1..1 1..N IS ASSIGNED TO► PROFESSOR(PID, FName, MINIT, LName, DOB, Rank) DEPARTMENT (DID, DName)

Exercise COURSE 1..1 Has▼ o..* ENROLL► SECTION STUDENTS 0..35 1..6 STUDENTS (SID, FName, LName, DOB, Major[1..3], Minor) COURSE (COURSE#, C_Name, Description) SECTION (Section_Num, Max_size)

Painter (Painter_ID, FName, LName) Paining (Painting_ID, Title, Date)
Exercise Paint ► (0..*) Displayed (1..1) Painter (1,1) (0..*) Painting Gallery Painter (Painter_ID, FName, LName) Paining (Painting_ID, Title, Date) Gallery (Gallery_Name, Location)

Employee (EID, FName, LName) Skill (Skill_Name)
Exercise Learn ► 0..* 0..* Employee Skills Expert Level Employee (EID, FName, LName) Skill (Skill_Name)

STUDENTS (SID, FName, LName, DOB) CLASS (CLASS#, C_Name, Description)
Exercise Take ► 1..* 0..* Student Classes Grade STUDENTS (SID, FName, LName, DOB) CLASS (CLASS#, C_Name, Description)

Exercise (Recursive and many-to-many)
STUDENTS (SID, FName, LName, DOB) COURSE (COURSE#, C_Name, Description) SECTION (Section_Num, Max_size) Has ► 1..1 0..* Course Section 0..* 0..* 0..* Take ▼ Grade ◄Prerequisite 1..* Student