Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Model Chapter 3 CIS 458 Sungchul Hong.

Similar presentations


Presentation on theme: "The Relational Model Chapter 3 CIS 458 Sungchul Hong."— Presentation transcript:

1 The Relational Model Chapter 3 CIS 458 Sungchul Hong

2 Last Class ERD –Entities, Relations, Attributes –It is not model specific

3 This Class Relational Model & Schema Conversion –Cf) –Hierarchical model –Network model –Object Oriented Model Cartesian product Integrity –Relational integrity –Entity integrity

4 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.

5

6 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

7 Relations

8 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.

9 Mathematical Relations Sets D 1 and D 2 where D 1 = {2, 4} and D 2 ={1, 3, 5}. Cartesian product D 1  D 2, is the set of all ordered pairs such that the first element is a member of D 1 and the second element is a member of D 2. –D 1  D 2 ={(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}

10 Exercise E 1 = {a, b, c} E 2 = {3, 4, 5} E 1  E 2 = {(a,3), (a, 4), (a,5) (b,3), (b, 4), (b,5) (c,3), (c, 4), (c,5)}

11 Mathematical Relations (2) Any subset of this Cartesian product is a relation. D 1 = {2, 4}, D 2 ={1, 3, 5} –Relation R = {(2, 1), (4, 1)} –R = { (x, y) | x  D 1, y  D 2, and y = 1} –You want to find out some records not entire table. –S = {(x, y) | x  D 1, y  D 2, and x= 2y} –S = { (2,1) }

12 Mathematical Relations (3) D 1 = {1, 3}, D 2 = {2, 4}, D 3 = {5, 6} D 1  D 2  D 3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6) (3,2,5), (3,2,6), (3,4,5), (3,4,6)}

13 Database Relations Relation Schema: A named relation defined by a set of attribute and domain name pairs. Let A 1, A 2, …, A n be attributes with domain D 1, D 2, …, D n. Then the set {A 1 :D 1, A 2 :D 2, …, A n :D n } 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. (A 1 :d 1, A 2 :d 2, …, A n :d n ) such that d 1  D 1, d 2  D 2, …, d n  D n

14 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 R 1, R 2, …, R n are a set of relation schemas, then we can write the relational database schema or relational schema R, as: R= {R 1, R 2, …, R n }

15 BranchNostreetcitypostcode B00522 Deer RdLondonSW14EH B00716 Argyll St.AberdeenAB2 3SU B Main StGlasgowG11 9QX B00432 Manse RdBristolBS99 1NZ B00256 Clover Dr.LondonNW10 6EU Branch A1A1 A2A2 A3A3 A4A4 D1D1 D2D2 D3D3 D4D4 Domain

16 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R={R 1, R 2, R 3, R 4, R 5, R 6 }

17 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.

18 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

19 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

20 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.

21 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.

22

23 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

24 (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)

25 (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

26 staffNo fNamelNamemanagessexDOBsalarybranchNo SL21JohnWhiteB005MOct/1/ B005 SG37AnnBeechFNov/10/ B003 SG14DavidFordMMar/24/ B003 SA9MaryHoweF19/Feb/709000B007 SG5SusanBrandB003FJun/3/ B003 SL41JulieLeeFJun/13/659000B005 Staff BranchNostreetcitypostcode B00522 Deer RdLondonSW14EH B00716 Argyll St.AberdeenAB2 3SU B Main StGlasgowG11 9QX B00432 Manse RdBristolBS99 1NZ B00256 Clover Dr.LondonNW10 6EU Branch (1..*) (1..1)

27 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..*) dateAdvert Cost propertyNo{PK} Street City Postcode Rooms Rent privateOwnerNo …

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

29 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R={R 1, R 2, R 3, R 4, R 5, R 6 }

30 Exercise

31 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)

32 Schema –Lease (leaseNo, PropertyNo, clientNo) –Preference (clientNo, preference) –Newspaper(newspaperName) –Advertisement (newspaperName, PropertyNo, dateAdvert, cost) –PrivateOwner (privateOwnerNo, fName, lName, address, telNo) –BusinessOwner (businessOwnerNo, businessName)

33 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

34 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.

35 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.

36 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.

37 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.

38 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.

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

40 ENROLL► STUDENTS SECTION 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

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

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

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

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

45 Student SSN{pk} Name Major Bdate Course Enroll Course#Cname Dept Text ISBN{pk} Publisher Author Adopt Grade Quarter Quarter{pk} 1 N 1 1 1N

46 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


Download ppt "The Relational Model Chapter 3 CIS 458 Sungchul Hong."

Similar presentations


Ads by Google