Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | russell-lambert |
View: | 226 times |
Download: | 0 times |
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 Connecticut191 Auditorium Road, Box U-155
Storrs, CT [email protected]
http://www.engr.uconn.edu/~steve(860) 486 - 4818
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.
CSE 4701
Chapter 3-2
Data Model: The Basics Data Model
Guidelines for Logical Organization of Data Three Parts: Structure, Operations and Constraints
Structure - Ways in Which Data Logically Organized Operations - Actions on Structures
e.g., List of Objects, FIFO (Queue) or LIFO (Stack) Update Operations and Query Operations
Constraints - Logical Restrictions on Data Inherent - Implied by Logical Structure Explicit - Defined in Assertions/Integrity
Constraints Implicit - Logically Follows From the Inherent &
Explicit Constraints
CSE 4701
Chapter 3-3
Essentials of Relational Approach
The Relational Model of Data is Based on the Concept of Relations A Relation is a Mathematical Concept Based on the
Concept of Sets The 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
CSE 4701
Chapter 3-4
R A1 A2 An......
.
.
.
Tuples
Attributesrelation name
t1t2
tm
v11 v12 v1nv21
vm1
v2n
vmn
v22
vm2
t1[An]
Relational Data Model: Data Structure
Relational Data Model 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
CSE 4701
Chapter 3-5
Two Versions of a Student Relation
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
A Schema of a Relation Denoted as R(A
1:D
1, A
2:D
2 , ..., A
n:D
n)
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 Di is Domain Over Which A
i
is Defined Domain
The Set of Values From which the Values of an Attribute Aj are Drawn, Denoted by Domain(Aj)
Example
CSE 4701
Chapter 3-7
A Relation (Relation Instance) An Occurrence of a Relation Scheme
R( A1:D
1, A
2 :D
2 , ..., A
n :D
n);
Defined as a Subset of the Cartesian Product of the Domains that Define its Schema, Denoted by R(r) = {T1, T2, ..., Tm}
Ti (i=1,…,m) is a Member of the Cartesian Product Domain(A1) Domain(A2) … Domain(An). R is also Called the Intension of a Relation r is also Called the Extension of a Relation
Relation Instances
CSE 4701
Chapter 3-8
A Relation (Relation Instance) D
1, D
2 , D
3 , ..., D
n are Sets of Atomic Values
R( A1:D
1, A
2:D
2 , ..., A
n:D
n) is Relation Schema
A Relation of Schema R, Defined Over D1, D
2 ,
D3 , ..., D
n, is a Subset of the Set of Ordered N-tuples
{<T1, T
2, T
3, ..., T
n >| T
i D
i, i=1, ...,n};
D1, D
2 , D
3 , ..., D
n Are Called Domains
N is the Degree of the Relation (Unary, Binary, Ternary, N-ary)
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
CSE 4701
Chapter 3-9
Relation Scheme - Definition of a Relation Set of Attributes that Describe a Relation
e.g., R( A1, A
2 , ..., A
n)
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
Basic Concepts
CSE 4701
Chapter 3-10
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. }
CSE 4701
Chapter 3-11
Tuple A Row in a Relational Table e.g., 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
Basic Concepts
CSE 4701
Chapter 3-12
EMP
PROJ
WORKS
ENO ENAME TITLE SAL
PNO PNAME BUDGET
RESPPNOENO DUR
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
CSE 4701
Chapter 3-13
Relation Instances
ENO ENAME TITLE
E1 J. Doe Elect. Eng.E2 M. Smith Syst. Anal.E3 A. Lee Mech. Eng.E4 J. Miller ProgrammerE5 B. Casey Syst. Anal.E6 L. Chu Elect. Eng.E7 R. Davis Mech. Eng.E8 J. Jones Syst. Anal.
EMP
ENO PNO RESP
E1 P1 Manager 12
DUR
E2 P1 Analyst 24E2 P2 Analyst 6E3 P3 Consultant 10E3 P4 Engineer 48E4 P2 Programmer 18E5 P2 Manager 24E6 P4 Manager 48E7 P3 Engineer 36
E8 P3 Manager 40
WORKS
E7 P5 Engineer 23
PROJ
PNO PNAME BUDGET
P1 Instrumentation 150000
P3 CAD/CAM 250000P2 Database Develop. 135000
P4 Maintenance 310000P5 CAD/CAM 500000
PROJ[PNO] P1P2P3P4P5
EMP[TITLE]
Elect.EngSyst. AnalMech. Eng
Programmer
CSE 4701
Chapter 3-14
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?
CSE 4701
Chapter 3-15
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” ...
CSE 4701
Chapter 3-16
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
CSE 4701
Chapter 3-17
Employee
A B C Da2 {b1, b2} c1 d5a2 b7 c9 d5a2 b23 c22 d1…...
A B C Da2 b2 c6 d1a2 b7 c9 d5a2 b7 c9 d5…...
R1 R2
E# Ename AGE ADDRESS
E1 Smith 30 3302 Peachtree Rd., Atlanta, GAE2 Diamond 45 1888 Buford Hyw.
E3 Evan Baker Ct. Atlanta
Examples
Are the Following Relations in a Relational Model? Why or Why Not?
CSE 4701
Chapter 3-18
Relational Schema R( A1:D
1, A
2 :D
2 , ..., A
n :D
n)
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 D
1, and Several Attributes
may be Defined on the Same Domain Degree N: Number of Attributes Cardinality M: Number of Tuples
Data Structure: Summary
CSE 4701
Chapter 3-19
RESERVATION
FLT# DATE CUST#
FLT-SCHEDULE
FLT#
CUSTOMER
CUST# CUST-NAMEDepT Dest ArrT
Relational Integrity Constraints
Integrity Constraints (ICs) Conditions that Must Hold on All Valid Relation
Instances at Any Given Database State Why are Integrity Constraints Needed?
What Happens when we try to Delete a Flight?
CSE 4701
Chapter 3-20
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
CSE 4701
Chapter 3-21
Key Constraints
Superkey (SK): Any Subset of Attributes Whose Values are
Guaranteed to Distinguish Among Tuples 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
CSE 4701
Chapter 3-22
Key Constraints
Primary Key (PK): 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
Useful to Capture ER Relationships
CSE 4701
Chapter 3-23
Superkeys and Candidate Keys: Examples
Example: 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 KeyWhy?
If Remove SerialNo, Make is not a Primary Key
CSE 4701
Chapter 3-24
Another Schema with Key
CAR(License#, EngineSerialNumber, Make, Model, Year)
What are Typically Used as Keys for Cars?
CSE 4701
Chapter 3-25
A Complete Schema with Keys ...
Keys Allow us to Establish Links Between Relations
What is ThisSimilar to in ER?
CSE 4701
Chapter 3-26
…and Corresponding DB Tables
Which Represent Tuples/Instances of Each Relation
1455
ASCnullWBnullnull
CSE 4701
Chapter 3-27
…with Remaining DB Tables
CSE 4701
Chapter 3-28
Superkeys vs. Candidate Keys
Superkey of R: 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
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 T1 and T2 in R(r), T1[sk] < > T2[sk]
For Cars, Valid Superkeys Must Contain:SerialNo or State, Reg#or Both
For Students [SSN, Name, Age] Superkey but not key since removing Name, Age or Both – still SK
CSE 4701
Chapter 3-29
Superkeys vs. Candidate Keys
Candidate Key of R: One of Many Possible Keys 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
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 T1 and T2 in R(r) Such That T1[k-a] = T2[k-a]
In Previous (State, Reg#, Make, Model) is SKIs it a CK? NO!Why or Why Not?
CSE 4701
Chapter 3-30
r1(PROJ)
PNO PNAME BUDGET
P11 Instrumentation 450000
P13 CAD/CAM 150000P12 Database Develop. 145000
P14 Maintenance 450000P15 Wireless Web 350000
r2(PROJ)
PNO PNAME BUDGET
P1 Instrumentation 150000
P3 CAD/CAM 250000P2 Database Develop. 135000
P4 Maintenance 310000P5 CAD/CAM 500000
Examples
Relational Schema PROJ(PNO, PNAME, BUDGET), we Assume that PNO is the Primary Key
The Two Tables Below are Relations of PROJ Is (PNO,PNAME) a Superkey in Either? Both? Do Two Distinct Tuples have Same Value for SK? Is PNAME a Candidate Key? Explain Your Answer. Is (PNAME,BUDGET) a Superkey in Either? Both?
CSE 4701
Chapter 3-31
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 Constraint
CSE 4701
Chapter 3-32
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, A
2 , ..., A
n) is Said to
Reference a Tuple T2 in R2 if $ FK {A1, A
2 , ...,
An} such that T1[fk] = T2[pk]
Referential Integrity Constraints
CSE 4701
Chapter 3-33
Examples
ENO ENAME TITLE
E1 J. Doe Elect. Eng.E2 M. Smith Syst. Anal.E3 A. Lee Mech. Eng.E4 J. Miller ProgrammerE5 B. Casey Syst. Anal.E6 L. Chu Elect. Eng.E7 R. Davis Mech. Eng.E8 J. Jones Syst. Anal.
EMPWORKS
PROJ
PNO PNAME BUDGET
P1 Instrumentation 150000
P3 CAD/CAM 250000P2 Database Develop. 135000
P4 Maintenance 310000P5 CAD/CAM 500000
ENO PNO RESP DUR
E1 P1 Manager 12E2 P1 Analyst 24E2 P2 Analyst 6E3 P3 Consultant 10E3 P4 Engineer 48E4 P2 Programmer 18E5 P2 Manager 24E6 P4 Manager 48E7 P3 Engineer 36
E8 P3 Manager 40E7 P5 Engineer 23
E9 P3 Engineer 30
CSE 4701
Chapter 3-34
ENO ENAME TITLE
ENO PNO RESP DUR
PNO PNAME BUDGET
WORK
EMP PROJ
WORK[ENO] is a subset of EMP[ENO]
WORK[PNO] is a subset of PROJ[PNO]
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
CSE 4701
Chapter 3-35
Another Example: Referential Integrity
What Do theseArrows Represent
in ER Diagram?
CSE 4701
Chapter 3-36
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
CSE 4701
Chapter 3-37
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?
CSE 4701
Chapter 3-38
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?)
CSE 4701
Chapter 3-39
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
CSE 4701
Chapter 3-40
Examples
ENO ENAME TITLE
E1 J. Doe Elect. Eng.E2 M. Smith Syst. Anal.E3 A. Lee Mech. Eng.E4 J. Miller ProgrammerE5 B. Casey Syst. Anal.
EMP
ENO PNO RESP
E1 P1 Manager 12
DUR
E2 P1 Analyst 24E2 P2 Analyst 6E3 P3 Consultant 10E3 P4 Engineer 48E4 P2 Programmer 18E5 P2 Manager 24
WORKS
PROJ
PNO PNAME BUDGET
P1 Instrumentation 150000
P3 CAD/CAM 250000P2 Database Develop. 135000
P4 Maintenance 310000P5 CAD/CAM 500000
E3 R. Davis Mech. Eng.?E1 Engineer 36 ?
E6 L. Chu?
E1 P5 Engineer ?E8 P3 Manager 40 ?
CSE 4701
Chapter 3-41
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
CSE 4701
Chapter 3-42
Example
ENO ENAME TITLE
E1 J. Doe Elect. Eng.E2 M. Smith Syst. Anal.E3 A. Lee Mech. Eng.E4 J. Miller ProgrammerE5 B. Casey Syst. Anal.
EMP
ENO PNO RESP
E1 P1 Manager 12
DUR
E2 P1 Analyst 24E2 P2 Analyst 6E3 P3 Consultant 10E3 P5 Engineer 48E4 P2 Programmer 18E5 P2 Manager 24
P4 Manager 48
WORKS
PROJ
PNO PNAME BUDGET
P1 Instrumentation 150000
P3 CAD/CAM 250000P2 Database Develop. 135000
P4 Maintenance 310000P5 CAD/CAM 500000
E6 L. Chu Elect. Eng.
Deleting this tuple?
1. Cascading
E6
2. reference revision?E5
CSE 4701
Chapter 3-43
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
CSE 4701
Chapter 3-44
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.
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
Constraints and Update Operations
CSE 4701
Chapter 3-45
Summary of Model: Common Terms
Informal Table Column Row (Instance) Table Definition Populated Table
Formal Relation Attribute Tuple Schema of Relation Extension
CSE 4701
Chapter 3-46
Interpretation Linguistic Logical Logical
Schema
Tuple
declaration assertion predicate
fact instance of assertion
values of satisfying predicate
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:
CSE 4701
Chapter 3-47
Features of Relational Model
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)
CSE 4701
Chapter 3-48
Relational DBMS Products Popular Commercial Products:
ORACLE SYBASE INFORMIX INGRES SQL Server MySQL
Popular Java-based RDB Products InstanceDB and Simple Text
Popular Personal DBMS product MSFT/Access
Mobile Apps MySQL Lite
CSE 4701
Chapter 3-49
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 Many Possible Relations That the Schema R
May Have? 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 23 =2 6 = 64
No
2
3
CSE 4701
Chapter 3-50
Relational Languages
A Relational Language Defines Operations to Manipulate Relations Used to Specify Retrieval Requests (Queries) Query Result is Expressed in the Form of a Relation
Classification Relational Algebra (A Quick First Look) Relational Calculus (Jump to Chapter XX) Basic Structured Query Language (SQL) Back to Relational Algebra (Back to Chapter YY) Advanced Structured Query Language (SQL)
CSE 4701
Chapter 3-51
Basic Relational Operations: Unary Operations
SELECT s PROJECT or P.
Binary Operations Set operations:
UNION INTERSECTION DIFFERENCE –
CARTESIAN PRODUCT JOIN operations
What is Relational Algebra?
Relational Algebra is a Procedural ParadigmYou Need to Tell What/How to Construct the Result
Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)
CSE 4701
Chapter 3-52
Relational Algebra
R S unionR S intersectionR \ S set differenceR S Cartesian
product
A1, A2, ..., An (R) projection
F (R) selection
R S natural join
R S theta-joinRS division[A1 B1,.., An Bn] rename
CSE 4701
Chapter 3-53
ENO ENAME TITLE
E1 J. Doe Elect. EngE6 L. Chu Elect. Eng.
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 Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP)
Selection Example
CSE 4701
Chapter 3-54
Another Selection Example
ASCnullWBnullnull
CSE 4701
Chapter 3-55
Projection Example
PNO,BUDGET(PROJ)
PNO BUDGET
P1 150000
P2 135000
P3 250000
P4 310000
P5 500000
PROJ
PNO BUDGET
P2 135000
P3 250000
P4 310000P5 500000
PNAME
P1 150000Instrumentation
Database Develop.
CAD/CAM
MaintenanceCAD/CAM
CSE 4701
Chapter 3-56
Other Projection Examples
CSE 4701
Chapter 3-57
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)
CSE 4701
Chapter 3-58
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 Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP TITLE(PROJ)
Elect.EngSyst.AnalMec.EngProgrammer
TITLE
Characteristics of Projection The PROJECT Operation Eliminates Duplicate Tuples
in the Resulting Relation Why? Projection Must Maintain a Mathematical Set (No
Duplicate Elements)
CSE 4701
Chapter 3-59
Selection with Projection Example
CSE 4701
Chapter 3-60
Union, Difference, Intersection Examples
What are these OtherThree Result Tables?
CSE 4701
Chapter 3-61
Cartesian Product: Example
A B C
a1a2a3
b1b1b4
c3c5c7
FE
f1f5
e1e2
A B C E
a1a1a2a2a3a3
b1b1b1b1b4b4
c3c3c5c5c7c7
e1e2e1e2e1e2
R S
R S F
f1f5f1f5f1f5
CSE 4701
Chapter 3-62
Cartesian Product Example
ENO ENAME EMP.TITLE SAL.TITLE SAL
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
EMP SAL
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 Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
CSE 4701
Chapter 3-63
-Join Example
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 Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
ENO ENAME
E1 J. Doe
M. SmithE2
E3 A. Lee
E4 J. Miller
E5 B. Casey
E6 L. Chu
E7 R. DavisE8 J. Jones
TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
SAL
40000
34000
27000
24000
34000
40000
2700034000
EMP E.TITLE=SAL.TITLE SAL
SAL.TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
CSE 4701
Chapter 3-64
Examples
A B C
a1a2a3
b1b1b4
c3c5c7
B E
b1b5
e1e2
R S
R R.B=S.B S
A R.B
a1a2
b1b1
C E
c3c5
e1e1
S.B
b1b1
EQUIJOIN
A R.B C E
a1a2
b1b1
c3c5
e1e1
R SNatural Join
CSE 4701
Chapter 3-65
Natural Join Example
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 Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
EMP
TITLE SAL
SAL
Elect. Eng. 70000
Syst. Anal. 80000
Mech. Eng. 56000
Programmer 60000
ENO ENAME E.TITLE SAL
E1 J. Doe Elect. Eng. 70000
E2 M. Smith Syst. Anal. 80000
56000
80000
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
EMP SAL
60000E4 J. Miller Programmer
80000E5 B.Casey Syst.Anal
70000E6 L. Chu Elect.Eng
56000E7 R.Davis Mech.Eng
CSE 4701
Chapter 3-66
Another Natural Join Example
CSE 4701
Chapter 3-67
Yet Another Natural Join Example
1455
CSE 4701
Chapter 3-68
Concluding Remarks
What have we Seen in Chapter 3? 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.