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.
91
Embed
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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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
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
Underlined Attributes are Relation Keys which Uniquely Distinguish Among Tuples (Rows)
Tabular Form
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.
Which of Following are Relations in a Relational Model?
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
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
Formal Relation Attribute Tuple Schema of Relation Extension
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:
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)
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?
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
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
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:
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
Keys Allow us to Establish Links Between Relations
CSE 4701
Chapter 3-28
…and Corresponding DB Tables
Which Represent Tuples/Instances of Each Relation
1455
ASCnullWBnullnull
CSE 4701
Chapter 3-29
…with Remaining DB Tables
CSE 4701
Chapter 3-30
Another View: What Do Arrows Represent?
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.
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
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-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)
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
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)
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
CSE 4701
Chapter 3-40
Another Selection Example
ASCnullWBnullnull
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
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
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
CSE 4701
Chapter 3-44
Other Projection Examples
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)
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)
CSE 4701
Chapter 3-47
Selection with Projection Example
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
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)
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
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
CSE 4701
Chapter 3-52
Union, Difference, Intersection Examples
What are these OtherThree Result Tables?
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
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
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)
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
CSE 4701
Chapter 3-57
Homework 2 Spring 15 Problems
Problem 6.18, parts a, b, c, and d – Using Cartesian Product
CSE 4701
Chapter 3-58
The Library Schema (Figure 6.14)
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)
CSE 4701
Chapter 3-60
Reminder – Discuss Homework 1
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)
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
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.
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
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
CSE 4701
Chapter 3-66
Homework 2 Spring 15 Problems
Problem 6.18, parts a, b, c, and d – with Join
CSE 4701
Chapter 3-67
The Library Schema (Figure 6.14)
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
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.
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
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
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
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
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?
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?)
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
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.