Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 Database Design Chapter -6- 3 Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does.

Similar presentations


Presentation on theme: "2 Database Design Chapter -6- 3 Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does."— Presentation transcript:

1

2 2 Database Design Chapter -6-

3 3 Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does not change the current state of the database.

4 Operations of the Relational Model not be violated Integrity constraints should not be violated by the update operations. Several update operations may have to be grouped together. Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints. March 31,2008Fatimah Alakeel4

5 5 The Insert Operation This operation can violate all constraints of the relation. How? Domain constraint: attribute value does not apear in the corresponding domain. Key constraint: if the key value already exist in another tuple. Entity integrity: if the PK of the new tuple is NULL Referential integrity: if any value of the FK refers to a tuple that does not exist in the other relation.

6 6 The Delete Operation This operation can violate only the referential integrity constraint. How? E.g: you can not delete a course that many students study in already.

7 7 The Update Operation The new value must be of the correct data type and domain.

8 Chapter 5-8 Update Operations on Relations In case of integrity violation, several actions can be taken: Cancel Cancel the operation that causes the violation (REJECT option) inform the user Perform the operation but inform the user of the violation Trigger additional updates Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option) Execute Execute a user-specified error-correction routine

9 9 Relational Algebra is a procedural query language

10 10 Relational Algebra Operations to manipulate relations. Used to specify retrieval requests (queries). Query results in the form of a relation -> this relation may be used

11 11 Theoretical expressions where both operands are relations The output from one operation can become the input to another operation (nested relational algebra) Eight fundamental operations in relational algebra: Unary operations work on one relation Selection Projection Binary operations work on pairs of relations Cartesian productJoin UnionIntersection Set differenceDivision Relational Algebra

12 12 Algebra Operators abcabc xyxy aabbccaabbcc xyxyxyxyxyxy Cartesian Product a1 a2 a3 b1 b2 b3 b1 b2 b3 c1 c2 c3 a1 a2 a3 b1 b2 b3 c1 c2 c3 Join xyxy a aaabcaaabc xyzxyxyzxy Divide SelectionProjection UnionIntersection Difference

13 13 Selection Operation Return the tuples of R that satisfy a selection condition Denoted by  (R) Selection condition = can be one condition or more using AND / OR. Example: Select all staff with a salary greater than 10,000  salary > 10000 (STAFF)

14 14 SL21 SG37 SG14 SG5 StaffNo John Ann David Susan FName White Beech Ford Brand LNameSex M F M F DOB 1-Oct-45 10-Nov-60 24-May-58 3-Jun-40 Salary 30000 12000 18000 24000 Selection Operation BrnNo D005 D003  salary > 10000 (STAFF)

15 15 Projection Operation Return the attributes of R Denoted by  (R) Example: Produce a list of salaries for all staff, showing only the staff number, first name, last name, and salary.  sno,fname,lname,salary (STAFF)

16 16 SL21 SG37 SG14 SNo John Ann David FName 30000 Salary 12000 18000 Projection Operation LName White Ford Beech SA9 SG5 SL41 Mary Susan Julie Howe Lee Brand 9000 24000 9000  sno,fname,lname,salary (STAFF)

17 17 Question Retrieve the names and salaries of employees who work in department 4.  FNAME,LNAME,SALARY (  DNO=4 (EMPLOYEE)) R1=  DNO=4 (EMPLOYEE) R2=  FNAME,LNAME,SALARY ( R1) You can rename the attributes of the resulted relation R1=  DNO=4 (EMPLOYEE) R2 (FIRSTNAME, LASTNAME,SALARY) =  FNAME,LNAME,SALARY ( R1) OR

18 18 Set Operations Binary operations Union R  S Intersection R  S Difference R-S Cartesian Product R x S Note: (union compatible): Dom ( Ai ε R )= Dom (Bi ε S) R,S must have the same number of attributes and same domain. The resulting relation has the same names as the first operand (R)

19 19 Union Operation Return a relation that includes all tuples that are either in R or in S, or in both R and S. Eliminate duplicate tuples. R & S must be union-compatible. Denoted by R  S R S R  S abcabc adfadf abcdfabcdf

20 20 Union Operation Example: List all cities where there is either a branch office or a property for rent.  city (BRANCH)   city (PROPERTY)

21 21 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Union Operation City London Glasgow Aberdeen City London Glasgow Aberdeen Bristol  city (BRANCH)   city (PROPERTY) BrnNo B001 B003

22 22 Intersection Operation Return a relation that includes all tuples that are in both R and S. R & S must be union-compatible. Denoted by R  S R S R  S abcabc adfadf a

23 23 Intersection Operation Example: List all cities where there is a branch office and at least one property for rent.  city (BRANCH)   city (PROPERTY)

24 24 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Intersection Operation City London Glasgow Aberdeen City  city (BRANCH)   city (PROPERTY) London Glasgow Aberdeen BrnNo B001 B003

25 25 Difference Operation Return a relation that includes all tuples that are in R but NOT in S. R & S must be union-compatible. Denoted by R - S R S R - S abcabc adfadf bcbc

26 26 Difference Operation Example: List all cities where there is a property for rent but no branch office.  city (PROPERTY) -  city (BRANCH)

27 27 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY B001 B002 B003 BrnNo Deer Rd Argyll St Main St Street SW1 4EH CO46 CO87 CO40 CO93 CO87 PostCode AB2 3SU G11 9QX BRANCH Difference Operation City London Glasgow Aberdeen City Bristol  city (PROPERTY) -  city (BRANCH) BrnNo B001 B003

28 28 Cartesian Product Operation Return a relation that is the concatenation of tuples from two relations R & S Denoted by R X S {a,b} X {1,2,3} = {(a,1), (a,2), (a,3), (b,1), (b,2), (b,3)} Cartesian Product is meaningless on its own, it can combine related tuples from two relations if followed by the appropriate SELECT operation.

29 29 Cartesian Product Operation Example: List the names and comments of all clients who viewed a property. Step 1: Cartesian product  ClientNo, Fname, Lname (CLIENT) X  ClientNo, PrprtyNo, Comment (VIEW)  Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT X VIEW) Step 2: Selection  Client.ClientNo=View.ClientNo (  ClientNo, Fname, Lname (CLIENT) X  ClientNo, PrprtyNo, Comment (VIEW) )

30 30 CR76 CO56 CR74 CR62 ClientNo John Aline Mike Mary FName Kay Stewart Ritchie Tregear LNameTelNo 207-774-5632 141-848-1825 1475-392178 1224-196720 CLIENT CR56 CR76 CR56 ClientNo PA14 PG4 PrprtyNo Too small Comment Too remote No dining room VIEW Cartesian Product Operation ViewDate 24-May-01 26-May-01 20-Apr-01 CR62 CR56 PA14 PG3628-Apr-01 14-May-01 Cardinality = 4 Tuples Degree = 3 Attributes (projected) Cardinality = 5 Tuples Degree = 3 Attributes (projected)

31 March 31,2008Fatimah Alakeel31 CR76 CR56 CR74 CR62 Client. ClientNo John Aline Mike Mary FName Kay Stewart Ritchie Tregear LName View. ClientNo PrprtyNo Comment CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR76JohnKay CR76JohnKay CR76JohnKay CR76JohnKay CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 AlineStewart CR56 AlineStewart CR56 AlineStewart CR74 MikeRitchie CR74 MikeRitchie CR74 Mike Ritchie CR74Mike Ritchie CR74Mike Ritchie CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear  ClientNo, Fname, Lname (CLIENT) X  clientNo, PropertyNo, comment (VIEW)) Cardinality = 5 * 4 = 20 Tuples Degree = 3 + 3 = 6 Attributes

32 32 Join Operation Return a relation that is the concatenation of tuples from two relations R & S that satisfy a certain condition Form of join operation: - Theta join - Equijoin - Natural join - Outer join

33 33 * Similar to CP followed by Select. The condition is called join condition (>,<, = ….) The join condition contains one or more equality conditions. (Could be redundant) Contains one or more equality conditions With no redundancy If there are no matching tuples in S that matches R then the NULL is shown

34 34

35 35 Theta join Operation Return a relation that includes all tuples that satisfy the logical condition F from the Cartesian product of R & S. Logical operators (  ) Denoted by R F S =  F (R X S)

36 36 Theta join Operation CLIENT(ClientNo, FName, Lname, TelNo) VIEW(ClientNo, PropertyNo, ViewDate, Comment) Example: List the names and comments of all renters who have viewed a property.  Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT Client.ClientNo=View.ClientNo VIEW) Alternative:  Client.ClientNo=View.ClientNo (  ClientNo, Fname, Lname (CLIENT) X  ClientNo, PrprtyNo, Comment (VIEW))

37 37 Equijoin Operation A Theta join where the logical condition is equality (=). Example: List the names and comments of all renters who have viewed a property.  Client.ClientNo, Fname, Lname, View.ClientNo, PrprtyNo, Comment (CLIENT Client.ClientNo=View.ClientNo VIEW)

38 38 CR76 CR56 CR74 CR62 Client. ClientNo John Aline Mike Mary FName Kay Stewart Ritchie Tregear LName View. ClientNo PrprtyNo Comment CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR76JohnKay CR76JohnKay CR76JohnKay CR76JohnKay CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR56 AlineStewart CR56 AlineStewart CR56 AlineStewart CR74 MikeRitchie CR74 MikeRitchie CR74 Mike Ritchie CR74Mike Ritchie CR74Mike Ritchie CR56 CR76 CR56 PA14 PG4 Too small Too remote No dining room CR62 CR56 PA14 PG36 CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear CR62 Mary Tregear  ClientNo, Fname, Lname (CLIENT) X  clientNo, PropertyNo, comment (VIEW))

39 39 CR76 CR56 Client. ClientNo John Aline FName Kay Stewart LName View. ClientNo PrprtyNo Comment CR76 CR56 PG4 PA14 Too remote Too small PG4 PA14CR62 No dining roomMary Tregear CR62  Client.ClientNo=View.ClientNo (  ClientNo, Fname, Lname (CLIENT) X  ClientNo, PrprtyNo, Comment (VIEW)) CR56 AlineStewart Equijoin Operation

40 40 Natural join Operation Equijoin of two relation R & S over all common attributes and values. One occurrence of each common attribute is eliminated from the result Denoted by R * (A ε R ),(B ε S ) S Example: Retrieve each EMPLOYEE’s name and the name of department he/she belongs to: T EMPLOYEE * (DNO),(DUNMBER) DEPARTMENT RESULT π FNAME, LNAME, DNAME (T)

41 41 Natural join Operation equality test Invariably the JOIN involves an equality test, and thus is often described as an equi-join. Such joins result in two attributes in the resulting relation having exactly the same value. A `natural join' will remove the duplicate attribute(s). If you do use natural joins make sure that the relations do not have two attributes with the same name by accident.

42 42 Outer join Operation A natural join in which tuples from R that do not have matching values in the common attributes of S are included in the result relation. Missing values in S are set to null. Denoted by R S Example: Produce a status report on property viewings. (  PropertyNo, Street, City, CLientNo,ViewDate, Comment (PROPERTY) VIEW)

43 43 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City PostCode AB7 5SU NW2 G11 9QX G32 4QX G12 Type House Flat House OwnerNo PROPERTY CO46 CO87 CO40 CO93 CO87 VIEW Outer Join Operation CR56 CR76 CR56 ClientNo PA14 PG4 PrprtyNo Too small Comment Too remote No dining room ViewDate 24-May-01 26-May-01 20-Apr-01 CR62PA14 14-May-01

44 44 PA14 PL94 PG4 PG36 PG21 PropertyNo Holheld Argyll St Lawrence Monar Novar Street Aberdeen London Glasgow Bristol City ClientNo CR56 null CR76 ViewDate 24-May-01 null 20-Apr-01 Comment Too small null Too remote Outer Join Operation PA14 HolheldAberdeenCR6214-May-01 No dining room null PG4 Lawrence Glasgow CR5626-May-01 null  PropertyNo, Street, City, CLientNo,ViewDate, Comment (PROPERTY) VIEW

45 45 More examples

46 46 UNION Example

47 47 INTERSECTION Example

48 48 DIFFERENCE Example

49 49 CARTESIAN PRODUCT example

50 50 JOIN Example

51 51 OUTER JOIN example 1

52 52 OUTER JOIN example 2

53 53 Additional Relational Functions

54 54 Aggregate Functions 1. SUM : returns the sum of the values in a specified attribute. 2. COUNT: returns the number of tuples in a specified attribute. 3. AVERAGE: returns the average of the values in a specified attribute. 4. MIN: returns the smallest value in a specified attribute. 5. MAX: returns the largest value in a specified attribute.

55 55 Examples NRNameSalaryDepartment 1John100A 5Sara300C 7Tom100A 12AnnienullC EMPLOYEE

56 56 1- SUM Find the total of the employees salary R  SUM(SALARY) (EMPLOYEE) The result is sum= 500 select sum(salary) from EMPLOYEE Note: Duplicates are not eliminated. Null values are ignored.

57 57 2- COUNT Count all the empolyees who get a salary R  count(salary) (EMPLOYEE) The result is count=3 select count(salary) from EMPLOYEE

58 58 3- AVERAGE Retrieve the average of the employees’ salaries. R  AVG( SALARY) (EMPLOYEE) Select avg(salary) from EMPLOYEE

59 59 4- MIN Retrieve the minimum salary among all employees. R  MIN (SALARY) (EMPLOYEE) The result is min= 100

60 60 5- MAX Retrieve the maximum salary among all employees. R  MAX( SALARY) (EMPLOYEE) The result is max= 300

61 61 Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name. Example:  X (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then  (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the attributes renamed to A1, A2, …., An.

62 62 Rename Operation  (DNO,NOEMPS,AVGSAL)  DNO COUNT (SSN),AVG( SALARY) (EMPLOYEE) DNO is the grouping attribute

63 63 Summary of Relational Algebra Operations Operation Notation Selection Projection Union Difference Intersection Cartesian Product Theta join Equijoin Natural join Outer join Rename Aggregate Function  (R)  (R) R  S R - S R  S R X S R F S R * S R S


Download ppt "2 Database Design Chapter -6- 3 Operations of the Relational Model Updates: changes the database’s state. Insert Delete Update/modify Retrievals: does."

Similar presentations


Ads by Google