Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 4701 Chapter 3-1 Chapter 3 6e & 7 5e : Relational Model – Part 1 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University.

Similar presentations


Presentation on theme: "CSE 4701 Chapter 3-1 Chapter 3 6e & 7 5e : Relational Model – Part 1 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University."— Presentation transcript:

1 CSE 4701 Chapter 3-1 Chapter 3 6e & 7 5e : Relational Model – Part 1 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 n A large portion of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech. n The remainder of these slides have been adapted from the AWL web site for the textbook.

2 CSE 4701 Chapter 3-2 Data Model: The Basics n Data Model l Guidelines for Logical Organization of Data l Three Parts: Structure, Operations and Constraints n Structure - Ways in Which Data Logically Organized n Operations - Actions on Structures e.g., List of Objects, FIFO (Queue) or LIFO (Stack) l Update Operations and Query Operations n Constraints - Logical Restrictions on Data l Inherent - Implied by Logical Structure l Explicit - Defined in Assertions/Integrity Constraints l Implicit - Logically Follows From the Inherent & Explicit Constraints

3 CSE 4701 Chapter 3-3 Essentials of Relational Approach n The Relational Model of Data is Based on the Concept of Relations l A Relation is a Mathematical Concept Based on the Concept of Sets n The Theory of Relations Provides a Formal Foundation for the Relational Data Model n 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 CSE 4701 Chapter 3-4 R A1A2An............ Tuples Attributes relation name t1t1 t2t2 tmtm v 11 v 12 v 1n v 21 v m1 v 2n v mn v 22 v m2 t 1 [A n ] Relational Data Model: Data Structure n Relational Data Model l Structures a Database as a Set of Relations. n A Relation l Set of Tuples and Typically Shown as a Table With Columns and Rows. l Column (Field) Represents an Attribute l Row (Tuple) Represents an Entity or a Relationship

5 CSE 4701 Chapter 3-5 Two Versions of a Student Relation

6 CSE 4701 Chapter 3-6 STUDENT (s#, sname, email, dept) Domain(s#): Number(9) Domain(sname): Char(30) Domain(email): Char(20) Domain(dept): Char(15) Basic Concepts - Relation Schema n A Schema of a Relation l Denoted as R(A 1 :D 1, A 2 :D 2,..., A n :D n ) l Set of Attributes That Describe a Relation Denoted by {A 1 :D 1, A 2 :D 2,..., A n :D n }, where A i (i=1, …, n) is Attribute Name and D i is Domain Over Which A i is Defined n Domain l The Set of Values From which the Values of an Attribute A j are Drawn, Denoted by Domain(A j ) n Example

7 CSE 4701 Chapter 3-7 n A Relation (Relation Instance) l An Occurrence of a Relation Scheme R( A 1 :D 1, A 2 :D 2,..., A n :D n ); l Defined as a Subset of the Cartesian Product of the Domains that Define its Schema, Denoted by R(r) = {T 1, T 2,..., T m } þ T i (i=1,…,m) is a Member of the Cartesian Product Domain(A 1 )  Domain(A 2 )  …  Domain(A n ). þ R is also Called the Intension of a Relation þ r is also Called the Extension of a Relation Relation Instances

8 CSE 4701 Chapter 3-8 n A Relation (Relation Instance) l D 1, D 2, D 3,..., D n are Sets of Atomic Values l R( A 1 :D 1, A 2 :D 2,..., A n :D n ) is Relation Schema A Relation of Schema R, Defined Over D 1, D 2, D 3,..., D n, is a Subset of the Set of Ordered N-tuples { | T i  D i, i=1,...,n}; D 1, D 2, D 3,..., D n Are Called Domains l N is the Degree of the Relation (Unary, Binary, Ternary, N-ary) l Number of Tuples in R, |R|, is Cardinality of R If D 1, D 2, D 3,..., D n are Finite then there Are 2 | D1 |  | D2 | ...  | Dn | Possible Relation States Relation: Formal Definition

9 CSE 4701 Chapter 3-9 n Relation Scheme - Definition of a Relation l Set of Attributes that Describe a Relation e.g., R( A 1, A 2,..., A n ) n Domain - Set of Values from which the Values of an Attribute Are Drawn l Denoted by Domain(a j ) n Relation (Relation Instance) l Subset of the Cartesian Product of Domains that Defines its Schema l Occurrence of a Relation Scheme l R(r) = {T 1, T 2,..., T m }. n Cardinality is the Number of Tuples Basic Concepts

10 CSE 4701 Chapter 3-10 What is an Example? n R(A, B) is a Relation Schema Defined over A and B n Let domain(A) = {a1, a2} and domain(B) = {0, 1, 2} n Tuples are: l,, etc. l How Many Possible Tuples are there? l Entire Relation is a set: à {,, etc. }

11 CSE 4701 Chapter 3-11 n Tuple l A Row in a Relational Table l e.g., t i = {v i1,v i2,...,v in } n Attribute l A Column in a Relational Table l Projection of an Attribute A j is {v 1j,v 2j,...,v mj }, a Subset of Domain(A j. ) l Several Attributes may be Defined on the same Domain (e.g., date of purchase, date of order, etc.) n Null Value l Special Value Meaning “not known” or “not applicable” … l Must be a Value - Even if it is Null n Degree - the Number of Attributes Basic Concepts

12 CSE 4701 Chapter 3-12 EMP PROJ WORKS ENOENAMETITLESAL PNOPNAMEBUDGET RESPPNOENODUR Relation Schemes n Example l EMP(ENO, ENAME, TITLE, SAL) l PROJ (PNO, PNAME, BUDGET) l WORKS(ENO, PNO, RESP, DUR) n Underlined Attributes are Relation Keys which Uniquely Distinguish Among Tuples (Rows) n Tabular Form

13 CSE 4701 Chapter 3-13 Relation Instances ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP ENOPNORESP E1P1Manager12 DUR E2P1Analyst24 E2P2Analyst 6 E3P3Consultant10 E3P4Engineer48 E4P2Programmer18 E5P2Manager24 E6P4Manager48 E7P3Engineer36 E8P3Manager40 WORKS E7P5Engineer23 PROJ PNOPNAMEBUDGET P1Instrumentation150000 P3CAD/CAM250000 P2Database Develop.135000 P4Maintenance310000 P5CAD/CAM500000 PROJ[PNO] P1 P2 P3 P4 P5 EMP[TITLE] Elect.Eng Syst. Anal Mech. Eng Programmer

14 CSE 4701 Chapter 3-14 Examples (cont.) n Quiz: l R(A, B) is a Relation Schema Defined over A and B l Let domain(A) = {a1, a2} and domain(B) = {0, 1, 2} l 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)} l What if Attribute A is a Key?

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

16 CSE 4701 Chapter 3-16 Characteristics of Relations n No Duplicate Tuples l It is a Set! l The Primary Key Always Exists n No Explicit or Implicit Ordering of Tuples n No Ordering of Attributes (If They Are Referred to by Their Names) n All Attribute Values Are Atomic l A Special Null Value is Used to Represent Values that are Unknown or Inapplicable to Certain Tuples l Thus - If “No” Value is Desired, “Null” is Used

17 CSE 4701 Chapter 3-17 Employee A B C D a2 {b1, b2} c1 d5 a2 b7 c9 d5 a2 b23 c22 d1 …... A B C D a2 b2 c6 d1 a2 b7 c9 d5 …... R1R2 E# Ename AGE ADDRESS E1 Smith 30 3302 Peachtree Rd., Atlanta, GA E2 Diamond 45 1888 Buford Hyw. E3 Evan Baker Ct. Atlanta Examples n Are the Following Relations in a Relational Model? n Why or Why Not?

18 CSE 4701 Chapter 3-18 n Relational Schema R( A 1 :D 1, A 2 :D 2,..., A n :D n ) n Relation R(r) With l Tuples of n Columns Denoted as T i = {v i1,v i2,...,v in } l Attributes A j (I=1,…,m) and R[a j ] = {v 1j,v 2j,...,v mj }, l Domain(a j. ) is a Subset of D 1, and Several Attributes may be Defined on the Same Domain l Degree N: Number of Attributes l Cardinality M: Number of Tuples Data Structure: Summary

19 CSE 4701 Chapter 3-19 RESERVATION FLT#DATECUST# FLT-SCHEDULE FLT# CUSTOMER CUST# CUST-NAME DepTDestArrT Relational Integrity Constraints n Integrity Constraints (ICs) l Conditions that Must Hold on All Valid Relation Instances at Any Given Database State n Why are Integrity Constraints Needed? What Happens when we try to Delete a Flight?

20 CSE 4701 Chapter 3-20 Relational Integrity Constraints Classification n There are Three Main Types of Constraints: l Key Constraints l Entity Integrity Constraints l Referential Integrity Constraints n Other Types of Semantic Constraints: l Domain Constraints l Transition Constraints l Set Constraints n DBMSs Handle Some But Not All Constraints n Think About Programming Related Constraints Throughout Our Upcoming Discussion

21 CSE 4701 Chapter 3-21 Key Constraints n Superkey (SK): l Any Subset of Attributes Whose Values are Guaranteed to Distinguish Among Tuples n Candidate Key (CK): l A Superkey with a Minimal Set of Attributes (No Attribute Can Be Removed Without Destroying the Uniqueness -- Minimal Identity) l A Value of an Attribute or a Set of Attributes in a Relation That Uniquely Identifies a Tuple l There may be Multiple Candidate Keys

22 CSE 4701 Chapter 3-22 Key Constraints n Primary Key (PK): l Choose One From Candidate Keys l The Primary Key Attributed are Underlined n Foreign Key (FK): l 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) l Allows Linkages Between Relations that are Tracked and Establish Dependencies l Useful to Capture ER Relationships

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

24 CSE 4701 Chapter 3-24 Another Schema with Key What are Typically Used as Keys for Cars?

25 CSE 4701 Chapter 3-25 A Complete Schema with Keys... Keys Allow us to Establish Links Between Relations What is This Similar to in ER?

26 CSE 4701 Chapter 3-26 …and Corresponding DB Tables Which Represent Tuples/Instances of Each Relation 14551455 A S C null W B null

27 CSE 4701 Chapter 3-27 …with Remaining DB Tables

28 CSE 4701 Chapter 3-28 Superkeys vs. Candidate Keys n Superkey of R: l A Superkey SK is a Set of Attributes of R Such that No Two Tuples in Any Valid Relation Instance R(r) will Have the Same Value for SK l Given R(U), U is the Set of Attributes of R and a Relation Instance of R, Denoted As R(r), For Any Distinct Tuples T 1 and T 2 in R(r), T 1 [sk] T 2 [sk] l For Cars, Valid Superkeys Must Contain: à SerialNo or State, Reg# à or Both l For Students [SSN, Name, Age] Superkey but not key since removing Name, Age or Both – still SK

29 CSE 4701 Chapter 3-29 Superkeys vs. Candidate Keys n Candidate Key of R: One of Many Possible Keys l A "Minimal" Superkey: a Candidate Key K is a Superkey s.t. Removal of any Attribute From K Results in a Set of Attributes that is Not a Superkey l Given R(U), U is the Set of Attributes of R and a Relation Instance of R, Denoted As R(r) K is a Candidate Key If and Only If for Any A in K, There Exist Two Distinct Tuples T 1 and T 2 in R(r) Such That T 1 [k-a] = T 2 [k-a] l In Previous (State, Reg#, Make, Model) is SK à Is it a CK? NO! à Why or Why Not?

30 CSE 4701 Chapter 3-30 r1(PROJ) PNOPNAMEBUDGET P11Instrumentation450000 P13CAD/CAM150000 P12Database Develop.145000 P14 Maintenance 450000 P15Wireless Web 350000 r2(PROJ) PNOPNAMEBUDGET P1Instrumentation150000 P3CAD/CAM250000 P2Database Develop.135000 P4 Maintenance 310000 P5CAD/CAM 500000 Examples n Relational Schema PROJ(PNO, PNAME, BUDGET), we Assume that PNO is the Primary Key n The Two Tables Below are Relations of PROJ l Is (PNO,PNAME) a Superkey in Either? Both? l Do Two Distinct Tuples have Same Value for SK? l Is PNAME a Candidate Key? Explain Your Answer. l Is (PNAME,BUDGET) a Superkey in Either? Both?

31 CSE 4701 Chapter 3-31 n Relational Database Schema: l A Set S of Relation Schemas (R 1, R 2,..., R n ) That Belong to the Same Database l S is the Name of the Database l S = {R 1, R 2,..., R n } n Entity Integrity: l For Any R i in S, Pk i is the Primary Key of R l Attributes in Pk i Cannot Have Null Values in any Tuple of R(r i ) l T[pk i ] Null for Any Tuple T in R(r) Entity Integrity Constraint

32 CSE 4701 Chapter 3-32 n A Constraint Involving Two Relations Used to Specify a Relationship Among Tuples in l Referencing Relation and Referenced Relation n Definition: R 1 and R 2 have a Referential Integrity Constraint If l Tuples in the Referencing Relation R 1 have a Set of Foreign Key (FK) Attributes That Reference the Primary Key PK of the Referenced Relation R 2 A Tuple T 1 in R 1 ( A 1, A 2,..., A n ) is Said to Reference a Tuple T 2 in R 2 if  FK  {A 1, A 2,..., A n } such that T 1 [fk] = T 2 [pk] Referential Integrity Constraints

33 CSE 4701 Chapter 3-33 Examples ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5 B. CaseySyst. Anal. E6 L. ChuElect. Eng. E7 R. Davis Mech. Eng. E8 J. Jones Syst. Anal. EMP WORKS PROJ PNOPNAMEBUDGET P1Instrumentation150000 P3CAD/CAM250000 P2Database Develop.135000 P4Maintenance310000 P5CAD/CAM500000 ENOPNORESP DUR E1P1Manager12 E2P1Analyst24 E2P2Analyst 6 E3P3Consultant10 E3P4Engineer48 E4P2Programmer18 E5P2Manager24 E6P4Manager48 E7P3Engineer36 E8P3Manager40 E7P5Engineer23 E9 P3 Engineer 30

34 CSE 4701 Chapter 3-34 ENO ENAME TITLE ENO PNO RESP DUR PNO PNAME BUDGET WORK EMPPROJ WORK[ENO] is a subset of EMP[ENO] WORK[PNO] is a subset of PROJ[PNO] Referential Integrity Constraints n A Referential Integrity Constraint Can Be Displayed in a Relational Database Schema as a Directed Arc From R 1.FK to R 2.PK

35 CSE 4701 Chapter 3-35 Another Example: Referential Integrity What Do these Arrows Represent in ER Diagram?

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

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

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

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

40 CSE 4701 Chapter 3-40 Examples ENO ENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5 B. CaseySyst. Anal. EMP ENOPNORESP E1P1Manager12 DUR E2P1Analyst24 E2P2Analyst 6 E3P3Consultant10 E3P4Engineer48 E4P2Programmer18 E5P2Manager24 WORKS PROJ PNOPNAMEBUDGET P1Instrumentation150000 P3CAD/CAM250000 P2Database Develop.135000 P4Maintenance310000 P5CAD/CAM500000 E3R. DavisMech. Eng. ? E1Engineer36 ? E6L. Chu ? E1P5Engineer ? E8P3Manager40 ?

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

42 CSE 4701 Chapter 3-42 Example ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5 B. CaseySyst. Anal. EMP ENOPNORESP E1P1Manager12 DUR E2P1Analyst24 E2P2Analyst 6 E3P3Consultant10 E3P5Engineer48 E4P2Programmer18 E5P2Manager24 P4Manager48 WORKS PROJ PNOPNAMEBUDGET P1Instrumentation150000 P3CAD/CAM250000 P2Database Develop.135000 P4Maintenance310000 P5CAD/CAM500000 E6L. Chu Elect. Eng. Deleting this tuple? 1. Cascading E6 2. reference revision? E5

43 CSE 4701 Chapter 3-43 Modify Operations on Relations n Modify Operation Changes Values of One or More Attributes in a Tuple (or Tuples) of a Given Relation R n Maintaining ICs Requires to Check If the Modifying Attributes Are Primary Key or Foreign Keys. n Integrity Check Actions: l 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 l Case 2: à Modifying a Primary Key Value Similar to Deleting One Tuple and Insert Another in its Place

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

45 CSE 4701 Chapter 3-45 Summary of Model: Common Terms n Informal l Table l Column l Row (Instance) l Table Definition l Populated Table n Formal l Relation l Attribute l Tuple l Schema of Relation l Extension

46 CSE 4701 Chapter 3-46 Interpretation Linguistic Logical Logical Schema Tuple declaration assertion predicate fact instance of assertion values of satisfying predicate Theoretical Foundation n Notion of Relation and Tuple is Modeled as in Set Theory n Changes From Set Theory l Existence of Null Value in the Tuples l Most Implementation Allow Duplicate Tuples in Result Sets (such as Projection) n Interpretation of Relations:

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

48 CSE 4701 Chapter 3-48 Relational DBMS Products n Popular Commercial Products: l ORACLE l SYBASE l INFORMIX l INGRES l SQL Server l MySQL n Popular Java-based RDB Products l InstanceDB and Simple Text n Popular Personal DBMS product l MSFT/Access n Mobile Apps l MySQL Lite

49 CSE 4701 Chapter 3-49 Quiz n R(A, B) is a Relation Schema Defined Over A and B Let Domain(A) = {a1, a2} and Domain(B) = {0, 1, 2} l Is R(A, B) Equivalent to R(B, A)? l How Many Possible Relations That the Schema R May Have? l Is the Set {(a1, 1), (a2, 2}, (a0, 0)} a Relation of Schema R? l What is the Degree of a Relation of Schema R? l What is the Cardinality of the Following Relation {(a1, 1), (a1, 2), (a2, 0)} of Schema R? Yes 2 2  3 =2 6 = 64 No 2 3

50 CSE 4701 Chapter 3-50 Relational Languages n A Relational Language l Defines Operations to Manipulate Relations l Used to Specify Retrieval Requests (Queries) l Query Result is Expressed in the Form of a Relation n Classification l Relational Algebra (A Quick First Look) l Relational Calculus (Jump to Chapter XX) l Basic Structured Query Language (SQL) l Back to Relational Algebra (Back to Chapter YY) l Advanced Structured Query Language (SQL)

51 CSE 4701 Chapter 3-51 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT  or . l Binary Operations à Set operations:  UNION   INTERSECTION  F DIFFERENCE –  CARTESIAN PRODUCT  à JOIN operations  What is Relational Algebra? n Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result n Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)

52 CSE 4701 Chapter 3-52 Relational Algebra R  Sunion R  Sintersection R \ Sset difference R  SCartesian product  A1, A2,..., An (R)projection  F (R)selection R Snatural join R   Stheta-join R  Sdivision  [ A 1 B 1,.., An Bn ]rename

53 CSE 4701 Chapter 3-53 ENOENAMETITLE E1J. DoeElect. Eng E6L. ChuElect. Eng.  TITLE='Elect. Eng.' (EMP) ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’ (EMP) Selection Example

54 CSE 4701 Chapter 3-54 Another Selection Example A S C null W B null

55 CSE 4701 Chapter 3-55 Projection Example  PNO,BUDGET (PROJ) PNOBUDGET P1150000 P2135000 P3250000 P4310000 P5500000 PROJ PNOBUDGET P2135000 P3250000 P4310000 P5500000 PNAME P1150000Instrumentation Database Develop. CAD/CAM Maintenance CAD/CAM

56 CSE 4701 Chapter 3-56 Other Projection Examples

57 CSE 4701 Chapter 3-57 Relational Algebra Expression n Several Operations can be Combined to form a Relational Algebra Expression (query) n Example: Retrieve all Customers over age 60? Method 1:  CNAME, ADDRESS, AGE (  AGE>60 (CUSTOMER) ) l Method 2: Senior-CUST(C#, Addr, Age) =  CNAME, ADDRESS, AGE (  AGE>60 (CUSTOMER) ) Method 3:  CNAME, ADDRESS, AGE (C) where  C  AGE>60 (CUSTOMER)

58 CSE 4701 Chapter 3-58 ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE (PROJ) Elect.Eng Syst.Anal Mec.Eng Programmer TITLE Characteristics of Projection n The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation l Why? l Projection Must Maintain a Mathematical Set (No Duplicate Elements)

59 CSE 4701 Chapter 3-59 Selection with Projection Example

60 CSE 4701 Chapter 3-60 Union, Difference, Intersection Examples What are these Other Three Result Tables?

61 CSE 4701 Chapter 3-61 Cartesian Product: Example ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 F E f1 f5 e1 e2 ABCE a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 e1 e2 e1 e2 RS R S F f1 f5 f1 f5 f1 f5 

62 CSE 4701 Chapter 3-62 Cartesian Product Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000

63 CSE 4701 Chapter 3-63  -Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng. 40000 Syst. Anal.34000 Mech. Eng.27000 Programmer24000 ENOENAME E1J. Doe M. SmithE2 E3A. Lee E4J. Miller E5B. Casey E6L. Chu E7R. Davis E8J. Jones TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal. SAL 40000 34000 27000 24000 34000 40000 27000 34000 EMP E.TITLE=SAL.TITLE SAL SAL.TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal.

64 CSE 4701 Chapter 3-64 Examples ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 BE b1 b5 e1 e2 RS R R.B=S.B S AR.B a1 a2 b1 CE c3 c5 e1 S.B b1 EQUIJOIN AR.BCE a1 a2 b1 c3 c5 e1 R S Natural Join

65 CSE 4701 Chapter 3-65 Natural Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng.70000 Syst. Anal.80000 Mech. Eng.56000 Programmer60000 ENOENAMEE.TITLE SAL E1J. DoeElect. Eng. 70000 E2M. SmithSyst. Anal. 80000 56000 80000 E3A. LeeMech. Eng. E8J. JonesSyst. Anal. EMP SAL 60000 E4J. MillerProgrammer 80000 E5B.CaseySyst.Anal 70000 E6L. ChuElect.Eng 56000 E7R.DavisMech.Eng

66 CSE 4701 Chapter 3-66 Another Natural Join Example

67 CSE 4701 Chapter 3-67 Yet Another Natural Join Example 14551455

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


Download ppt "CSE 4701 Chapter 3-1 Chapter 3 6e & 7 5e : Relational Model – Part 1 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University."

Similar presentations


Ads by Google