91
CSE 4701 Chapter 3-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 06269-3155 [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-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

Embed Size (px)

Citation preview

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

CSE 4701

Chapter 3-1

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

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.

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

CSE 4701

Chapter 3-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

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

CSE 4701

Chapter 3-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

Page 4: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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

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

CSE 4701

Chapter 3-5

Two Versions of a Student Relation

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

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

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

CSE 4701

Chapter 3-7

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

Page 8: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-8

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

Basic Concepts

Page 9: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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. }

Page 10: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-10

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

Page 11: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-11

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

Page 12: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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?

Page 13: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

Page 14: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

Page 15: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-15

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

Other Examples

Which of Following are Relations in a Relational Model?

Page 16: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-16

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

Page 17: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

No

2

3

2 × 3 = 6

Page 18: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-18

Summary of Model: Common Terms

Informal Table Column Row (Instance) Table Definition Populated Table

Formal Relation Attribute Tuple Schema of Relation Extension

Page 19: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-19

Interpretation Linguistic Logical Logical

Schema

Tuple

declaration assertion predicate

fact instance of assertion

values of satisfying predicate

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:

Page 20: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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)

Page 21: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-21

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 DB State

Why are Integrity Constraints Needed? Multiple Flights, Customers, and Cust/Flight/Date

What Happens when we try to Delete a Flight?

Page 22: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

Page 23: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

Page 24: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-24

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

What are Foreign Keys in:

Page 25: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-25

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

Page 26: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-26

Another Schema with Key

CAR(License#, EngineSerialNumber, Make, Model, Year)

What are Typically Used as Keys for Cars?

Page 27: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-27

A Complete Schema with Keys ...

Keys Allow us to Establish Links Between Relations

Page 28: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-28

…and Corresponding DB Tables

Which Represent Tuples/Instances of Each Relation

1455

ASCnullWBnullnull

Page 29: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-29

…with Remaining DB Tables

Page 30: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-30

Another View: What Do Arrows Represent?

Page 31: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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

Page 32: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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

Page 33: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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

Can we Add this Tuple to WORKS?

Page 34: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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

Page 35: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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

Page 36: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-36

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)

Page 37: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-37

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

Page 38: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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)

Page 39: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-39

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

Page 40: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-40

Another Selection Example

ASCnullWBnullnull

Page 41: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-41

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

Selection Condition

Page 42: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-42

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)

Projection

Page 43: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-43

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

Page 44: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-44

Other Projection Examples

Page 45: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-45

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)

Page 46: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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)

Page 47: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-47

Selection with Projection Example

Page 48: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-48

General FormR 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))

Union

Page 49: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-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)

Page 50: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-50

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

Set Difference

Page 51: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-51

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

Set Intersection

Page 52: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-52

Union, Difference, Intersection Examples

What are these OtherThree Result Tables?

Page 53: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-53

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?

Cartesian Product

Page 54: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-54

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

Page 55: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-55

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”

Cartesian Product

(S#(

city=‘Storrs’(STUDENT))

C#

(CNAME=‘Database’

(COURSE))) – S#,

C#(S-C)

Page 56: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-56

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

Generates Lots of “Data” that Doesn’t Make Sense

Page 57: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-57

Homework 2 Spring 15 Problems

Problem 6.18, parts a, b, c, and d – Using Cartesian Product

Page 58: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-58

The Library Schema (Figure 6.14)

Page 59: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-59

a. How many copies of the book titled The Lost Tribe are owned by the library branch whose name is ‘Sharpstown’? (BaB – books at Branches)

b. How many copies of the book titled The Lost Tribe are owned by each library branch? (CaB- Copies at Branches)

Parts a and b

Ans = No_Of_Copies( (BranchName=‘Sharpstown’ (LIBRARY-BRANCH)) × BaB)

BaB= (BOOKCOPIES × (Title=‘The Lost Tribe’

(BOOK))) )

Ans = BranchName, No_Of_Copies( (Title=‘The Lost Tribe’ (BOOK)) × CaB)

CaB = BOOKCOPIES × LIBRARY_BRANCH)

Page 60: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-60

Reminder – Discuss Homework 1

Page 61: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-61

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)

Theta Join (-Join)

Page 62: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-62

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

-Join Condition

Page 63: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

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.

Notice “Absence” of Nonsensical Data from Cart Product.

Page 64: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-64

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

Page 65: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-65

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

Page 66: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-66

Homework 2 Spring 15 Problems

Problem 6.18, parts a, b, c, and d – with Join

Page 67: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-67

The Library Schema (Figure 6.14)

Page 68: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-68

a. How many copies of the book titled The Lost Tribe are owned by the library branch whose name is ‘Sharpstown’? (BaB – books at Branches)

b. How many copies of the book titled The Lost Tribe are owned by each library branch? (CaB- Copies at Branches)

Parts a and b

Ans = No_Of_Copies( (BranchName=‘Sharpstown’ (LIBRARY-BRANCH)) * BaB)

BranchID

BaB= (BOOKCOPIES *(Title=‘The Lost Tribe’

(BOOK))) )

BookId

Ans = BranchName, No_Of_Copies( (Title=‘The Lost Tribe’ (BOOK)) * CaB)

BookId

CaB = BOOKCOPIES * LIBRARY_BRANCH)BranchId

Page 69: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-69

c. Retrieve the names of all borrowers who do not have any books checked out.

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.

Parts c and d

NO_CHECKOUT_B =CardNo(BORROWER) - CardNo(BOOK_LOANS)

Ans = Name(BORROWER * NO_CHECKOUT_B)

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

Page 70: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-70

Natural Join Natural Join Combines Relations on Attributes with the

Same Names STUDENT(S#, SN, CITY, Email) 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, Email, C#, G (

STUDENT.S# = S-C.S# (STUDENT S-C))

Page 71: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-71

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

Page 72: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-72

Another Natural Join Example

Page 73: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-73

Yet Another Natural Join Example

1455

Page 74: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-74

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 (rs) Consists of all (rs)-tuples t such that for all s-tuples u in S, the tuple tu is in R.

Quotient (Division)

Page 75: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-75

Division Example

ENO

E3

R ÷ S

ENO PNO PNAME

E1 P1 Instrumentation 150000

BUDGET

E2 P1 Instrumentation 150000E2 P2 Database Develop. 135000E3 P1 InstrumentationE3 P4 MaintenanceE4 P2 InstrumentationE5 P2 InstrumentationE6 P4E7 P3 CAD/CAME8 P3 CAD/CAM

310000150000150000310000250000250000

R

Maintenance

150000

S

PNO PNAME BUDGET

P1 Instrumentation 150000P4 Maintenance 310000

Find the employees who work for both project P1 and project P4?

Page 76: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-76

Relational Algebra

Fundamental OperatorsDerivable from the fundamental operators

Selection Projection Union Difference Cartesian Product

Intersection Join, Equi-join, Natural Join

Page 77: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-77

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:

1. The Set of Algebra Operations {, , , –, } is a

Complete Set of Relational Algebra Operations

2. Any Query Language Equivalent to These Five Operations is Called Relationally Complete

All Relational Algebra Operations

Page 78: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-78

Fundamental Operators Selection Projection Union Set Difference Cartesian Product

Relational Algebra: Summary

Form:

<Operator><Operand(s)> Result>

Relation (s) Relation

Additional Operators Join Intersection Quotient (Division)

Union Compatibility Same Degree Corresponding

Attributes Defined Over the Same Domain

Page 79: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-79

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

Page 80: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-80

Semi-Join Example

B C D

b2b2b3b4b5b6b7b8b9b10

S

A B

R

b1b1b3b4

a1a2a3a4 R

BSALR ( P B S)=

c1c1c1c2c2c2c3c3c4c4

d9d8d7d9d8d7d5d4d4d9

A B

b3b4

a3a4

Page 81: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-81

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

Aggregate Functions

Page 82: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-82

Example 1: 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

Examples of Aggregate Functions

Page 83: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-83

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?

Page 84: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-84

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

Page 85: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-85

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

Page 86: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-86

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 ?

Page 87: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-87

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

Page 88: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-88

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

Page 89: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-89

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

Page 90: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-90

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

Page 91: CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University

CSE 4701

Chapter 3-91

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.