Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2

Similar presentations


Presentation on theme: "Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2"— Presentation transcript:

1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2
Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT (860) A large portion of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech. The remainder of these slides have been adapted from the AWL web site for the textbook.

2 Combining Chapters 3 and 6 6ed
What is a Relational Data Model? Schema, Tables, Attributes/Columns, Tuples Characteristics and Examples Referential Integrity Superkeys, Candidate Primary and Foreign Keys Referential Integrity Constrains Relational Algebra Selection, Project, Join, Union, Intersection Advanced Concepts Operations on Relations

3 Essentials of Relational Approach
Relational Model of Data is Based on the Concept of Relations A Relation is a Mathematical Concept Based on Sets Theory of Relations Provides a Formal Foundation for the Relational Data Model The Model Was First Proposed by Dr. E.F. Codd (IBM) in 1970 in the Paper, Entitled "A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970

4 Relational Data Model: Data Structure
Structures a Database as a Set of Relations. A Relation Set of Tuples and Typically Shown as a Table With Columns and Rows. Column (Field) Represents an Attribute Row (Tuple) Represents an Entity or a Relationship Attributes RELATION: A table of values A relation may be thought of as a set of rows. A relation may alternately be though of as a set of columns. Each row of the relation may be given an identifier. Each column typically is called by its column name or column header or attribute name. A relation may be regarded as a set of tuples (rows). Columns in a table are also called as attributes of the relation. A tuple is an ordered set of values Each value is derived from an appropriate domain. Each row in the CUSTOMER table may be called as a tuple in the table and would consist of four values. relation name t1[An] R A1 A2 An ...... v11 v12 v1n Tuples t1 v21 v22 v2n t2 . vm1 vm2 vmn tm

5 Two Versions of a Student Relation

6 Basic Concepts - Relation Schema
A Schema of a Relation Denoted as R(A1:D1, A2:D2 , ..., An:Dn) Set of Attributes That Describe a Relation Denoted by {A1:D1, A2:D2 , ..., An:Dn}, where Ai (i=1, …, n) is Attribute Name and Di is Domain Over Which Ai is Defined Domain The Set of Values From which the Values of an Attribute Aj are Drawn, Denoted by Domain(Aj) Example For Example- CUSTOMER (Cust-id, Cust-name, Address, ). Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, , each of which has a domain or a set of valid values. For example, the domain of Cust-id is 9 digit numbers. Example: < , ”Dana Evan", "101 Emory Rd. Atlanta, GA ", is a tuple or an instance of the CUSTOMER relation. STUDENT (s#, sname, , dept) Domain(s#): Number(9) Domain(sname): Char(30) Domain( ): Char(20) Domain(dept): Char(15)

7 Basic Concepts Relation Scheme - Definition of a Relation
Set of Attributes that Describe a Relation e.g., R( A1, A2 , ..., An) Domain - Set of Values from which the Values of an Attribute Are Drawn Denoted by Domain(aj) Relation (Relation Instance) Subset of the Cartesian Product of Domains that Defines its Schema Occurrence of a Relation Scheme R(r) = {T1, T2, ..., Tm}. Cardinality is the Number of Tuples

8 Basic Concepts Tuple A Row in a Relational Table - ti = {vi1,vi2, ...,vin} Attribute A Column in a Relational Table Projection of an Attribute Aj is {v1j,v2j, ...,vmj}, a Subset of Domain(Aj.) Several Attributes may be Defined on the same Domain (e.g., date of purchase, date of order, etc.) Null Value Special Value Meaning “not known” or “not applicable” … Must be a Value - Even if it is Null Degree - the Number of Attributes

9 What is an Example? R(A, B) is a Relation Schema Defined over A and B
Let domain(A) = {a1, a2} and domain(B) = {0, 1, 2} Tuples are: <a1, 0>, <a1, 2>, <a2, 2> etc. How Many Possible Tuples are there? Entire Relation is a set: {<a1, 0>, <a1, 2>, <a2, 2> etc. } Yes, No, No.

10 Relation Schemes Example EMP(ENO, ENAME, TITLE, SAL)
PROJ (PNO, PNAME, BUDGET) WORKS(ENO, PNO, RESP, DUR) Underlined Attributes are Relation Keys which Uniquely Distinguish Among Tuples (Rows) Tabular Form EMP ENO ENAME TITLE SAL PNO PNAME BUDGET PROJ RESP PNO ENO DUR WORKS

11 Relation Instances PROJ[PNO] P1 P2 P3 P4 P5 EMP[TITLE] Elect.Eng
ENO ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP PNO RESP P1 Manager 12 DUR Analyst 24 P2 6 P3 Consultant 10 P4 Engineer 48 18 36 40 WORKS P5 23 PROJ PNAME BUDGET Instrumentation 150000 CAD/CAM 250000 Database Develop. 135000 Maintenance 310000 500000 PROJ[PNO] P1 P2 P3 P4 P5 EMP[TITLE] Elect.Eng Syst. Anal Mech. Eng Programmer

12 Examples (cont.) Quiz: R(A, B) is a Relation Schema Defined over A and B Let domain(A) = {a1, a2} and domain(B) = {0, 1, 2} Which of the Following are Relations of R? {(a1, 1), (a1, 2), (a2, 0)} {(a1, 0), (a1, 1), (a1, 2)} {(a1, 1), (a2, 2}, (a0, 0)} {(a1, 1), (a2, a2}, (a0, a0)} {(a1, 1, c1), (a2, 2)} What if Attribute A is a Key? Yes, No, No.

13 Characteristics of Attributes
Attribute Name An Attribute Name Refers to a Position in a Tuple by Name Rather than Position An Attribute Name Indicates the Role of a Domain in a Relation Attribute Names must be Unique Within Relations By Using Attribute Names we can Disregard the Ordering of Field Values in Tuples Attribute Value - Must have a Value Must Be an Atomic Value Can Be a Null Value Meaning “Not Known”, “Not Applicable” ... Not Possible to have Undefined Value

14 Characteristics of Relations
No Duplicate Tuples It is a Set! The Primary Key Always Exists No Explicit or Implicit Ordering of Tuples No Ordering of Attributes (If They Are Referred to by Their Names) All Attribute Values Are Atomic A Special Null Value is Used to Represent Values that are Unknown or Inapplicable to Certain Tuples Thus - If “No” Value is Desired, “Null” is Used Ordering of tuples in a relation r(R): The tuples are not considered to be ordered, even though they appear to be in the tabular form. Ordering of attributes in a relation schema R: The order of the attributes in a relation schema R(A1, A2, ..., An) is not important. Values in a tuple: All values are considered atomic. A special null value is used to represent values that are unknown or inapplicable to certain tuples.

15 Other Examples Which of Following are Relations in a Relational Model?
A B C D A B C D R1 R2 a2 {b1, b2} c1 d5 a b c6 d1 a2 b c9 d5 a b c9 d5 a2 b c22 d1 a b c9 d5 …... …... Employee E# Ename AGE ADDRESS E2 Diamond Buford Hyw. E1 Smith Peachtree Rd., Atlanta, GA E3 Evan Baker Ct. Atlanta

16 Data Structure: Summary
Relational Schema R( A1:D1, A2 :D2 , ..., An :Dn) Relation R(r) With Tuples of n Columns Denoted as Ti = {vi1,vi2, ...,vin} Attributes Aj (I=1,…,m) and R[aj] = {v1j,v2j, ...,vmj}, Domain(aj.) is a Subset of D1, and Several Attributes may be Defined on the Same Domain Degree N: Number of Attributes Cardinality M: Number of Tuples

17 Quiz R(A, B) is a Relation Schema Defined Over A and B Let Domain(A) = {a1, a2} and Domain(B) = {0, 1, 2} Is R(A, B) Equivalent to R(B, A)? How May Possible Tuples? Is the Set {(a1, 1), (a2, 2}, (a0, 0)} a Relation of Schema R? What is the Degree of a Relation of Schema R? What is the Cardinality of the Following Relation {(a1, 1), (a1, 2), (a2, 0)} of Schema R? Yes 2 × 3 = 6 No 2 Yes, No, No. 3

18 Summary of Model: Common Terms
Informal Table Column Row (Instance) Table Definition Populated Table Formal Relation Attribute Tuple Schema of Relation Extension

19 Summary of Model: Theoretical Foundation
Notion of Relation and Tuple is Modeled as in Set Theory Changes From Set Theory Existence of Null Value in the Tuples Most Implementation Allow Duplicate Tuples in Result Sets (such as Projection) Interpretation of Relations: Interpretation Linguistic Logical Logical Schema declaration assertion predicate instance of assertion values of satisfying predicate Tuple fact

20 Summary of Model: Features
Simple and Mathematically Elegant Simple, Uniform Data Structure Solid Theoretical Foundation Advantage of the Relational Model: Simplicity Separation Between Data and Data Access Easier to Define Data and Data Structure Easier to Write Queries (Specify What Not How) Relational DBMS can do More for You PC-Based Systems have Brought DB to Masses MS Access - Easy to Use Integration with Office Tools (Word, Excel)

21 Relational Integrity Constraints
Integrity Constraints (ICs): Conditions that Must Hold on All Valid Relation Instances at Any Given DB State Why are Integrity Constraints Needed? Multiple Flights, Customers, and Cust/Flight/Date What Happens when we try to Delete a Flight? FLT-SCHEDULE CUSTOMER FLT# CUST# CUST-NAME DepT Dest ArrT Set Constraints: rules governing set of tuples. Key constraint: uniqueness of keys Entity integrity: no primary key value is null Domain Constraints: limit on values of particular attributes. Transition Constraints: how value changes due to database update. Referential Integrity Constraints: between two relations, cross references must point to existing tuples. Semantic Constraints: those need to be explicitly specified in the schema definition. RESERVATION FLT# DATE CUST#

22 Relational Integrity Constraints Classification
There are Three Main Types of Constraints: Key Constraints Entity Integrity Constraints Referential Integrity Constraints Other Types of Semantic Constraints: Domain Constraints Transition Constraints Set Constraints DBMSs Handle Some But Not All Constraints Think About Programming Related Constraints Throughout Our Upcoming Discussion Set Constraints: rules governing set of tuples. Key constraint: uniqueness of keys Entity integrity: no primary key value is null Domain Constraints: limit on values of particular attributes. Transition Constraints: how value changes due to database update. Referential Integrity Constraints: between two relations, cross references must point to existing tuples. Semantic Constraints: those need to be explicitly specified in the schema definition.

23 Key Constraints Superkey (SK):
Any Subset of Attributes Whose Values are Guaranteed to Distinguish Among Tuples Could be All Attributes of Entire Relation Candidate Key (CK): A Superkey with a Minimal Set of Attributes (No Attribute Can Be Removed Without Destroying the Uniqueness -- Minimal Identity) A Value of an Attribute or a Set of Attributes in a Relation That Uniquely Identifies a Tuple There may be Multiple Candidate Keys Examples are given in class: Student (S#, SSN, SN, DN, Birthdate) Candidate Key: S#, SSN Superkey: Any combinations of attrributes containing S# or SSN Primary Key: SSN Foreign Key: none COURSE(C#, Sec#, Instructtor, Classroom) S-C(S#, C#, Grade) Candidate Key= Primary key = (S#, C#) Superkeys: (S#, C#), (S#, C#, Grade) Foreign Key: S# with respect to S# in Student.relation C# w.r.t. C# in Course relation

24 Key Constraints Primary Key (PK): Foreign Key (FK):
Choose One From Candidate Keys The Primary Key Attributed are Underlined Foreign Key (FK): An Attribute or a Combination of Attributes (Say A) of Relation R1 Which Occurs as the Primary Key of another Relation R2 (Defined on the Same Domain) Allows Linkages Between Relations that are Tracked and Establish Dependencies What are Foreign Keys in: Examples are given in class: Student (S#, SSN, SN, DN, Birthdate) Candidate Key: S#, SSN Superkey: Any combinations of attrributes containing S# or SSN Primary Key: SSN Foreign Key: none COURSE(C#, Sec#, Instructtor, Classroom) S-C(S#, C#, Grade) Candidate Key= Primary key = (S#, C#) Superkeys: (S#, C#), (S#, C#, Grade) Foreign Key: S# with respect to S# in Student.relation C# w.r.t. C# in Course relation

25 Superkeys and Candidate Keys: Examples
The CAR relation schema: CAR(State, Reg#, SerialNo, Make, Model, Year) Its primary key is {State, Reg#} It has two candidate keys Key1 = {State, Reg#} Key2 = {SerialNo} which are also superkeys {SerialNo, Make} is a Superkey but not a Key Why? If Remove SerialNo, Make is not a Primary Key

26 Another Schema with Key
What are Typically Used as Keys for Cars?

27 A Complete Schema with Keys ...
Keys Allow us to Establish Links Between Relations

28 …and Corresponding DB Tables
Which Represent Tuples/Instances of Each Relation A S C null W B 1 4 5 Houston

29 …with Remaining DB Tables

30 Another View: What Do Arrows Represent?

31 Entity Integrity Constraint
Relational Database Schema: A Set S of Relation Schemas (R1, R2, ..., Rn) That Belong to the Same Database S is the Name of the Database S = {R1, R2, ..., Rn} Entity Integrity: For Any Ri in S, Pki is the Primary Key of R Attributes in Pki Cannot Have Null Values in any Tuple of R(ri) T[pki] < > Null for Any Tuple T in R(r) Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R). This is because primary key values are used to identify the individual tuples. t[PK] <> null for any tuple t in r(R) Note: Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key.

32 Referential Integrity Constraints
A Constraint Involving Two Relations Used to Specify a Relationship Among Tuples in Referencing Relation and Referenced Relation Definition: R1and R2 have a Referential Integrity Constraint If Tuples in the Referencing Relation R1 have a Set of Foreign Key (FK) Attributes That Reference the Primary Key PK of the Referenced Relation R2 A Tuple T1 in R1( A1, A2 , ..., An) is Said to Reference a Tuple T2 in R2 if $ FK {A1, A2 , ..., An} such that T1[fk] = T2[pk]

33 Examples Can we Add this Tuple to WORKS? E9 P3 Engineer 30 WORKS ENO
ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP ENO PNO RESP DUR E1 P1 Manager 12 E2 Analyst 24 P2 6 E3 P3 Consultant 10 P4 Engineer 48 E4 Programmer 18 E5 E6 E7 36 E8 40 P5 23 Can we Add this Tuple to WORKS? PROJ PNO PNAME BUDGET P1 Instrumentation 150000 P3 CAD/CAM 250000 P2 Database Develop. 135000 P4 Maintenance 310000 P5 500000 E9 P Engineer

34 Referential Integrity Constraints
A Referential Integrity Constraint Can Be Displayed in a Relational Database Schema as a Directed Arc From R1.FK to R2.PK EMP PROJ ENO ENAME TITLE PNO PNAME BUDGET ENO PNO RESP DUR WORK WORK[ENO] is a subset of EMP[ENO] WORK[PNO] is a subset of PROJ[PNO]

35 Integrity Constraints Summary
Relational Database: Set of Relations Satisfying the Integrity Constraints Integrity Constraints (ICs): Conditions that Must Hold on All Valid Relation Instances Key Constraints - Uniqueness of Keys Entity ICs - No Primary Key Value is Null Referential ICs Between Two Relations, Cross References Must Point to Existing Tuples Domain ICs are Limits on the Value of Particular Attribute Transition ICs Indicate the Way Values Changes Due to Database Update

36 What is Relational Algebra?
Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra) Basic Relational Operations: Unary Operations SELECT s PROJECT  or P. Binary Operations Set operations: UNION  INTERSECTION  DIFFERENCE – CARTESIAN PRODUCT  JOIN operations

37 Relational Algebra A1, A2, ..., An (R) projection F (R) selection
R S union R S intersection R - S set difference R S Cartesian product A1, A2, ..., An (R) projection F (R) selection R S natural join R S theta-join RS division [A1 B1,.., An Bn] rename

38 Selection Selects the Tuples (Rows) From a Relation, Which Satisfy a Selection Condition Selection Produces a Horizontal Subset of the Operand Relation General Form F (R) R is a Relation F is a Boolean Expression on the Attributes of R Resulting Relation Has the Same Schema as R Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)

39 Selection Example TITLE='Elect. Eng.'(EMP)
ENO ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP ENO ENAME TITLE E1 J. Doe Elect. Eng E6 L. Chu Elect. Eng. TITLE='Elect. Eng.'(EMP) TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP)

40 Another Selection Example
null W B

41 Selection Condition A SELECT Condition is a Boolean Expression
Form F1 Y F2 Y ..., Y Fq (Q>=1), Where Fi (I=1,…,q) are Atomic Boolean Expressions of the Form aqc or aqb, a, b are Attributes of R and c is a Constant. The Operator Q is one of the Arithmetic Comparison Operators: <, >, =, <>, >=, <= The Operator Y is one of the Logical Operators: , , ¬ Nesting: ( ) Conditions are Essentially “Conditional Expressions” in PLs

42 Projection Extract Only Certain Columns (Attributes) Specified in an Attribute List X From a Relation R Produces a New Relation, which is a Vertical Subset of the Operand Relation R The Schema (Columns) of the Resulting Relation is X General Form X(R) R is a Relation X is a Subset of the Attributes of R Over Which the Projection is Performed Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples)

43 Projection Example  PNO,BUDGET(PROJ) PROJ PNO BUDGET P2 135000 P3
250000 P4 310000 P5 500000 PNAME P1 150000 Instrumentation Database Develop. CAD/CAM Maintenance  PNO,BUDGET(PROJ) PNO BUDGET P1 150000 P2 135000 P3 250000 P4 310000 P5 500000

44 Other Projection Examples

45 Characteristics of Projection
The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation Why? Projection Must Maintain a Mathematical Set (No Duplicate Elements) ENO ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones EMP  TITLE(PROJ) Elect.Eng Syst.Anal Mec.Eng Programmer TITLE

46 Relational Algebra Expression
Several Operations can be Combined to form a Relational Algebra Expression (query) Example: Retrieve all Customers over age 60? Method 1:  CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) ) Method 2: Senior-CUST(C#, Addr, Age) =  CNAME, ADDRESS, AGE (s AGE>60(CUSTOMER) ) Method 3:  CNAME, ADDRESS, AGE (C) where C = s AGE>60(CUSTOMER)

47 Selection with Projection Example

48 Union General Form R  S where R, S are Relations
Result contains Tuples from both R and S Duplications are Removed The two Operands R, S should be union-compatible (type-compatible w.r.t Columns/Attributes) Example: “find students registered for course C1 or C3” s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C))

49 Union Compatibility Two Relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said Union-compatible If and Only If They Have The Same Number of Attributes The Domains of Corresponding Attributes are Compatible, i.e., Dom(Ai)=dom(Bi) for I=1, 2, ..., N Names Do Not Have to be Same! For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible The Resulting Relation for Relational Set Operations Has the Same Attribute Names as the First Operand Relation R1 (by Convention)

50 Set Difference s#(CNO=‘C1’ (S-C)) –s#(CNO=‘C3’ (S-C))
General Form R – S where R and S are Relations Result Contains all tuples that are in R, but not in S. R – S <> S – R Again, there Must be Compatibility Example “Find the students who registered course C1 but not C3”  s#(CNO=‘C1’ (S-C)) –s#(CNO=‘C3’ (S-C))

51 Set Intersection s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C))
General Form R S where R and S are Relations Result Contains all Tuples that are in R and S. R S = R – (R – S) Again, there Must be Compatibility Example “find the students who registered for both C1 and C3” s#(CNO=‘C1’ (S-C)) s#(CNO=‘C3’ (S-C))

52 Union, Difference, Intersection Examples
What are these Other Three Result Tables? S  I S - I I - S

53 Cartesian Product Given Relations R of Degree k1 and Cardinality card1
S of Degree k2 and Cardinality card2 Cartesian Product R  S is a Relation of Degree (k1+ k2) and Consists of Tuples of Degree (k1+ k2) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S Cardinality of the Result of the Cartesian Product R  S is card1 * card2 What is One Problem with Cartesian Product w.r.t. the Result Set?

54 Cartesian Product: Example
B C E F a1 a2 a3 b1 b1b4 c3 c5 c7 e1 e2 f1 f5 A B C E F R S a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 f1 f5

55 Cartesian Product Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R  S is a relation of schema R’(A1, …, An, B1, …, Bm). Example “Get a list containing (S#, C#) for all students who live in Storrs but are not registered for the database course” (S#(city=‘Storrs’(STUDENT))   C# (CNAME=‘Database’(COURSE))) – S#, C#(S-C)

56 Cartesian Product Example
Generates Lots of “Data” that Doesn’t Make Sense ENO ENAME TITLE E1 J. Doe Elect. Eng E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP ENO ENAME EMP.TITLE SAL.TITLE SAL E1 J. Doe Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000 E2 M. Smith E3 A. Lee E8 J. Jones EMP SAL TITLE SAL Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000

57 Homework 2 Spring 15 Problems
Problem 6.18, parts a, b, c, and d – Using Cartesian Product

58 The Library Schema (Figure 6.14)

59 Part a BaB= (BOOKCOPIES × (Title=‘The Lost Tribe’ (BOOK))) )
How many copies of the book titled The Lost Tribe are owned by the library branch whose name is ‘Sharpstown’? (BaB – books at Branches) BaB= (BOOKCOPIES × (Title=‘The Lost Tribe’ (BOOK))) ) Ans = No_Of_Copies( (BranchName=‘Sharpstown’ (LIBRARY-BRANCH)) × BaB)

60 Part b b. How many copies of the book titled The Lost Tribe are owned by each library branch? (CaB- Copies at Branches) CaB = BOOKCOPIES × LIBRARY_BRANCH) Ans = BranchName, No_Of_Copies( (Title=‘The Lost Tribe’ (BOOK)) × CaB)

61 Reminder – Discuss Homework 1

62 Theta Join (-Join) General Form R  S where R, S are Relations,
F is a Boolean Expression, called a Join Condition. A Derivative of Cartesian Product R  S =  (R  S) R(A1, A2, ..., Am, B1, B2, ..., Bn) is the Resulting Schema of a -Join over R1 and R2: R1(A1, A2, ..., Am)  R2 (B1, B2, ..., Bn) -Join is derived from a Cartesian Product followed by a SELECT.

63 -Join Condition A -Join Condition is a Boolean Expression of the form F1 y1 F2 y2 ..., yn-1 Fq (q>=1), where Fi (i=1,…,q) are Atomic Boolean Expressions of the form Ai  Bj, Ai, Bj are Attributes of R1 and R2 Respectively q is one of the Algorithmic Comparison Operators =, <>, >, <. >=, <= The Operator yi (i=1,…,n-1) is Either a Logical AND operator  or a logical OR operator  Again – a Conditional Expression

64 -Join Example Notice “Absence” of Nonsensical Data from Cart Product.
ENAME TITLE E1 J. Doe Elect. Eng E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP EMP E.TITLE=SAL.TITLE SAL SAL 40000 34000 27000 24000 ENO ENAME TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. SAL.TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. E1 J. Doe E2 M. Smith E3 A. Lee TITLE SAL Elect. Eng. 40000 Syst. Anal. 34000 Mech. Eng. 27000 Programmer 24000 E4 J. Miller E5 B. Casey E6 L. Chu E7 R. Davis E8 J. Jones

65 Other Types of Join Equi-join (EQUIJOIN)
The  Expression only Contains one or more Equality Comparisons Involving Attributes from R1 and R2 User Specifies Equality Conditions Natural Join Denoted as R S Special Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and S By Common, it means that each Join Attribute in A has not only Compatible Domains but also the Same Name in both Relations R and S System Automatically Picks the Equality Conditions Equi-join (EQUIJOIN) The formula F only contains one or more equality comparisons involving attributes from R1 and R2. Natural join Equi-join of two relations R and S over a set of attributes (say A) common to both R and S denoted as R A S. Difference In an EQUIJOIN, the join attribute of S appear redundantly in the result relation. In a NATURAL JOIN, the redundant join attributes of S are eliminated from the result relation. The equality condition is implied.

66 Examples R S A B C B E a1 a2 a3 b1 b1b4 c3 c5 c7 b1 b5 e1 e2 EQUIJOIN
Natural Join R S R R.B=S.B S A R.B C E A R.B S.B C E a1 a2 b1 c3 c5 e1 a1 a2 b1 b1 c3 c5 e1

67 Homework 2 Spring 15 Problems
Problem 6.18, parts a, b, c, and d – with Join

68 The Library Schema (Figure 6.14)

69 Part c Ans = Name(BORROWER * NO_CHECKOUT_B)
c. Retrieve the names of all borrowers who do not have any books checked out. NO_CHECKOUT_B =CardNo(BORROWER) - CardNo(BOOK_LOANS) Ans = Name(BORROWER * NO_CHECKOUT_B)

70 Part d d. For each book that is loaned out from the Sharpstown branch and whose Due_date is today, retrieve the book title, the borrower’s name, and the borrower’s address. S =BranchID( BranchName=‘Sharpstwon’ (LIBRARY-BRANCH)) B_FROM_S = BookID, CardNo ( ( DueDate=‘Today’ (BOOKLOANS)) * S ) BranchId Ans = Title, Name, Address ( BOOK * BORROWER * B_FROM_S ) BookId CardNo

71 Natural Join Natural Join Combines Relations on Attributes with the Same Names STUDENT(S#, SN, CITY, ) S-C(S#, C#, G) Example Query 1: “list of students with complete course grade info” STUDENT S-C All Natural Joins can be Expressed by a Combination of Primitive Operators Example Query 2: “print all students info (courses taken and grades)” S#, SN, CITY, , C#, G (STUDENT.S# = S-C.S# (STUDENT S-C))

72 Natural Join Example EMP EMP SAL ENO ENAME TITLE E1 J. Doe Elect. Eng
M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey E6 L. Chu Elect. Eng. E7 R. Davis E8 J. Jones EMP EMP SAL ENO ENAME E.TITLE SAL E1 J. Doe Elect. Eng. 70000 E2 M. Smith Syst. Anal. 80000 E3 A. Lee Mech. Eng. 56000 E4 J. Miller Programmer 60000 E5 B.Casey Syst.Anal 80000 TITLE SAL Elect. Eng. 70000 Syst. Anal. 80000 Mech. Eng. 56000 Programmer 60000 E6 L. Chu Elect.Eng 70000 E7 R.Davis Mech.Eng 56000 E8 J. Jones Syst. Anal. 80000

73 Another Natural Join Example

74 Yet Another Natural Join Example
1 4 5 Houston

75 Quotient (Division) Given Relations R(T,U) of degree r
S(U) of degree s The Division of R by S, R ÷ S Results is a Relation of Degree (rs) Consists of all (rs)-tuples t such that for all s-tuples u in S, the tuple tu is in R.

76 Division Example ENO PNO PNAME E1 P1 Instrumentation 150000 BUDGET E2 P2 Database Develop. 135000 E3 P4 Maintenance E4 E5 E6 E7 P3 CAD/CAM E8 310000 250000 R Find the employees who work for both project P1 and project P4? S PNO PNAME BUDGET P1 Instrumentation 150000 P4 Maintenance 310000 R ÷ S ENO E3

77 Relational Algebra Selection Projection Union Difference
Cartesian Product Intersection Join, Equi-join, Natural Join Derivable from the fundamental operators Fundamental Operators

78 All Relational Algebra Operations
A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in Set Any Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set Important Concepts: The Set of Algebra Operations {, , , –, } is a Complete Set of Relational Algebra Operations Any Query Language Equivalent to These Five Operations is Called Relationally Complete All the operations discussed so far (Join, Natural Join) can be described as a sequence of only the operations SELECT, PROJECT , UNION, SET DIFFERENCE, and CARTESIAN PRODUCT. Hence, the set {s ,P , U, - , X } is called a complete set of relational algebra operations. Any query language equivalent to these operations is called relationally complete.

79 Relational Algebra: Summary
Fundamental Operators Selection Projection Union Set Difference Cartesian Product Additional Operators Join Intersection Quotient (Division) Union Compatibility Same Degree Corresponding Attributes Defined Over the Same Domain Form: <Operator><Operand(s)> Result>  Relation (s) Relation

80 Semi-Join General form: R F S
R is Target Relation, S is the Source Relation Semi-join A Form of Join where the Result Contains ONLY those Tuples of the Target Relation, which Participate in the Join with the Source Relation Benefit: Decreases the Number of Tuples that need to be Handled in the Join R F S = (R FS)  = (R) F(S) = R F(S) where A is a set of Attributes of R, B is a set of attributes of S

81 Semi-Join Example = R  BSAL R  (P B S) R a1 a2 a3 a4 b1 b3 b4 b2 b3
C D A B b2 b3 b4 b5 b6 b7 b8 b9 b10 c1 c2 c3 c4 d9 d8 d7 d5 d4 a3 a4 b3 b4

82 Aggregate Functions Commonly Used Aggregate Functions:
SUM, COUNT, AVERAGE, MIN, MAX They Often Applied to sets of Values or sets of Tuples in Database Applications [<grouping attributes> ] f <function list> (R) The Grouping Attributes are Optional

83 Examples of Aggregate Functions
Retrieve the average salary of all employees (no grouping): R(AVG_SAL) = s AVG(SALARY) (EMPLOYEE) Example 2: For each department, retrieve the department number, the number of employees, and the average salary (in the department): R(D#,Num_E,AVG_SAL) = GROUPDNO( s DNO, COUNT(ENO), AVG(SALARY)(EMPLOYEE)) DNO is called the grouping attribute in this example

84 Operations on Relations
A DBMS Operates via User Queries to Read and Change Data in a Database Changes Can be Inserting, Deleting, or Updating (Equivalent to a Delete followed by Insert) One Critical Issue in DB Operations is Integrity Constraints Maintenance in the Presence of INSERTING a Tuple DELETING a Tuple UPDATING/MODIFYING a Tuple. We’ll discuss Each case in Turn What is Constraint Maintenance Similar to in PL?

85 Problem Statements Integrity Constraints (ICs) Should Not Be Violated by Update Operations To Maintain ICs, Updates may Need to be Propagated and Cause Other Updates Automatically Common Method: Group Several Update Operations Together As a Single Transaction If Integrity Violation, Several Actions Can Be Taken: Cancel Operation that Caused Violation (REJECT) Perform the Operation but Inform User of Violation Trigger Additional Updates So the Violation is Corrected (CASCADE Option, SET NULL Option) Execute a User-specified Error-Correction Routine (Similar to What in a PL Like Java?)

86 Insertion Operations on Relations
Insert a Duplicate Key Violates Key Integrity: Check If Duplicates Occur Insert a Null Key Violates Entity Integrity: Check If Null is in Any Key Insert a Tuple Whose Foreign Key Attribute Pointing to an Non-existent Tuple Violates Referential Integrity: Check the Existence of Referred Tuple Correction Actions: Reject the Update Correct the Violation - Change Null, Duplicate, Etc. Cascade the Access - Insert a New Tuple That Did Not Exist/Delete Tuples that are being Referenced

87 Examples ? ? ? ? ? EMP WORKS ENO ENAME TITLE ENO PNO RESP DUR E1
J. Doe Elect. Eng. E1 P1 Manager 12 E2 M. Smith Syst. Anal. E2 P1 Analyst 24 E3 A. Lee Mech. Eng. E2 P2 Analyst 6 E4 J. Miller Programmer E3 P3 Consultant 10 E5 B. Casey Syst. Anal. E3 P4 Engineer 48 E4 P2 Programmer 18 E5 P2 Manager 24 E6 L. Chu ? E3 R. Davis Mech. Eng. ? E1 Engineer 36 ? PROJ PNO PNAME BUDGET P1 Instrumentation 150000 P3 CAD/CAM 250000 P2 Database Develop. 135000 P4 Maintenance 310000 P5 500000 E1 P5 Engineer ? E8 P3 Manager 40 ?

88 Deletion Operations on Relations
Deleting a Tuple Referred to by Other Tuples in Database (via FKs) would Violate Referential Integrity Action: Check for Incoming Pointers of the Deleted Tuple. Group the Deletion and the Post-processing of the Referencing Pointers in a Single Transaction Three Options If Deletion Causes a Violation Reject the Deletion Attempt to Cascade (Propagate) the Deletion by Deleting the Tuples which Reference the Tuple being or to be Deleted Modify the Referencing Attribute Values that Cause the Violation; Each Values is Set to Null or Changed to Reference to Another Valid Tuple

89 Example 1. Cascading Deleting this tuple? 2. reference revision? ENO
ENAME TITLE E1 J. Doe Elect. Eng. E2 M. Smith Syst. Anal. E3 A. Lee Mech. Eng. E4 J. Miller Programmer E5 B. Casey EMP PNO RESP P1 Manager 12 DUR Analyst 24 P2 6 P3 Consultant 10 P5 Engineer 48 18 P4 WORKS PROJ PNAME BUDGET Instrumentation 150000 CAD/CAM 250000 Database Develop. 135000 Maintenance 310000 500000 E6 L. Chu Deleting this tuple? 1. Cascading 2. reference revision?

90 Modify Operations on Relations
Modify Operation Changes Values of One or More Attributes in a Tuple (or Tuples) of a Given Relation R Maintaining ICs Requires to Check If the Modifying Attributes Are Primary Key or Foreign Keys. Integrity Check Actions: Case 1: If the Attributes to be Modified are Neither a Primary Key nor a Foreign Key, Modify Causes No Problems Must Check and Confirm that the New Value is of Correct Data Type and Domain Case 2: Modifying a Primary Key Value Similar to Deleting One Tuple and Insert Another in its Place

91 Constraints and Update Operations
Three Types of Update Operations: INSERT, DELETE, MODIFY Constraint Maintenance During Updates Some DB Do Maintain Domain Constraints via Enumeration and Value-Range Data Types The Types of Constraints That Most DBMSs Maintain are Key Constraints Entity Constraints Referential Integrity Constraints Other Semantic Constraints Need to Be Maintained by Application Developers/programmers Transition Constraints Domain Constraints Etc.

92 Concluding Remarks What have we Seen in Chapters 3 & 6?
Basic Concepts of Relational Model Including Relation/Table, Tuple/Row, Attribute/Column, Domain/Attribute Value Concept of SK, CK, PK, and FK for Identification and Referential Integrity Integrity Constraints as they Relate to Referential Dependencies Check for Modification Operations Overall, Relational Theory is Basis for SQL, Normal Forms, ER-Relational Translation, etc.


Download ppt "Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2"

Similar presentations


Ads by Google