chapter 2 relational data model-part 2

41
RELATIONAL DATA MODEL PART 2 (Additional Notes) DFC2033 Database System

Upload: eddyzulham-mahluzydde

Post on 15-Jan-2017

698 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Chapter 2 Relational Data Model-part 2

RELATIONAL DATA MODEL PART 2 (Additional Notes)  

DFC2033 Database System

Page 2: Chapter 2 Relational Data Model-part 2

Course Learning Outcome

¨  CLO1 : Explain the fundamentals concepts of database management and relational data model to create a database based on an organization’s requirements.

¨  CLO3 : Solve an organization’s requirements by selecting the correct database query formulation using an appropriate commercial Database Management System (DBMS).

FP 304 – Database System

Page 3: Chapter 2 Relational Data Model-part 2

Learning Outcome

¨  Use operators of relational algebra.

¨  Identify the fundamental operators used to retrieve information from a relational database.

¨  Describe the purpose and input of each of the operators.

¨  Use the operators to write the expressions based on given relational tables.

¨  Show the output of the fundamental operators based on a given database.

¨  Define the traditional set of operators for relational tables.

¨  Draw the Venn diagrams for traditional set operators.

¨  Define union compatibility.

¨  Apply the union, intersect, and difference set operators to two given relational tables.

FP 304 – Database System

Page 4: Chapter 2 Relational Data Model-part 2

Relational Algebra

Relational Algebra is : ¨  The formal description of how a relational database operates

¨  An interface to the data stored in the database itself

¨  The mathematics which underpin SQL operations

¨  Operators in relational algebra are not necessarily the same as SQL operators, even if they have the same name. For example, the SELECT statement exists in SQL, and also exists in relational algebra. These two uses of SELECT are not the same.

¨  The DBMS must take whatever SQL statements the user types in and translate them into relational algebra operations before applying them to the database.

FP 304 – Database System

Page 5: Chapter 2 Relational Data Model-part 2

Restrict (select)

¨  SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition.

¨  It is a filter that keeps only those tuples that satisfy a qualifying condition – those satisfying the condition are selected while others are discarded.

¨  Symbol : ¤  σ (sigma) used to denote the SELECT operator

¤  (R) denote relation name.

¨  Syntax :

¤ σ<selection condition > ( R )

¤  Normally combine with {=,<,>, <=, >=, =}

FP 304 – Database System

Page 6: Chapter 2 Relational Data Model-part 2

Restrict (select)

¨  Symbol : ¤  σ (sigma) used to denote the SELECT operator ¤  (R) denote relation name.

¨  Syntax :

¤ σ<selection condition>( R ) ¤  Normally combine with {=,<,>, <=, >=}

¨  May combine with Boolean Operator: ¤  AND

¤  OR ¤  NOT

FP 304 – Database System

Page 7: Chapter 2 Relational Data Model-part 2

Restrict (select)

¨  Example: ¤ To select the EMPLOYEE tuples whose department

number is four ¤ Select salary is greater than $30,000 the following

notation is used:

FP 304 – Database System

σDNO = 4 (EMPLOYEE)

σSALARY > 30,000 (EMPLOYEE)

Page 8: Chapter 2 Relational Data Model-part 2

¨  Select the tuples for all employees who either work in department 4 and make over RM2500 per year, or work in department 1 and make over RM3000.

FP 304 – Database System

Restrict (select)

σ(Dno=4 AND Salary>2500) OR (Dno=1 AND Salary>3000)(EMPLOYEE)

Page 9: Chapter 2 Relational Data Model-part 2

Example

Table : EMP_RESULT

FP 304 – Database System

EMP_RESULT ß σ(Dno=4 AND Salary>2500) OR (Dno=1 AND Salary>3000)(EMPLOYEE)

Page 10: Chapter 2 Relational Data Model-part 2

Project

¨  Project operation selects certain columns from the table and discards the other columns.

¨  The PROJECT creates a vertical partitioning – one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns.

¨  Symbol -: Л (pi) ¨  Syntax : Л <attribute list> (R)  

FP 304 – Database System

Page 11: Chapter 2 Relational Data Model-part 2

Example

¨  List each employee’s staff number first, last name and salary.

STAFF_NEW ß  Л  staffNo,fname,lname,salary(STAFF)

FP 304 – Database System

Page 12: Chapter 2 Relational Data Model-part 2

Example FP 304 – Database System

Table : STAFF_NEW

Page 13: Chapter 2 Relational Data Model-part 2

Project and select operators

¨  We can combine project with select operators when we want to select certain column that specify specific conditions.

¨  Example: ¤ Select student name and address for people who live in

Seremban. ¤ First : Select from temporary table who live in

Seremban ¤ Select : List name and address à temporary table

FP 304 – Database System

Page 14: Chapter 2 Relational Data Model-part 2

Example

Retrieve the first name, last name, salary and branch number of all employees who work in branch B003. The sequence is :

TEM_STAFF ßσbranchNo=B003(STAFF) RESULT ß ЛFname,Lname,Salary,branchNo(TEMP_STAFF)

FP 304 – Database System

Page 15: Chapter 2 Relational Data Model-part 2

Example

TEM_STAFF ßσbranchNo=B003(STAFF) RESULT ß ЛFname,Lname,Salary,branchNo(TEMP_STAFF)

You can also combine the operation as below: ЛFname,Lname,Salary,branchNo(σbranchNo=B003(STAFF))

FP 304 – Database System

Page 16: Chapter 2 Relational Data Model-part 2

Exercise

¨  Using relational algebra ¤  Select Employee whose test score is greater than 91. ¤  List all the employee number, test code and score for test record. ¤  List all the test number, test code for employee number 110.

FP 304 – Database System

Table : Employee

Page 17: Chapter 2 Relational Data Model-part 2

Cross product

¨  This operation is used to combine tuples from two relations in a combinatorial fashion.

¨  Concatenation of every tuple of relation R with every tuple of relation S.

¨  Denoted by X ¨  R X S ¨  Syntax: (Л  <attributelist>(R))X (Л  <attributelist>(S))

FP 304 – Database System

Page 18: Chapter 2 Relational Data Model-part 2

Example

ClientNo fName lname

CR76 John Kay

CR56 Aline Stewart

CR74 Mike Ritchie

CR62 Mary Tregear

ClientNo propertyNo comment

CR56 PA14 Too small

CR76 PG4 Too remote

CR56 PG4

CR62 PA14 No dining room

CR56 PG36

TABLE: CLIENT TABLE: VIEWING

TABLE: RESULT

FP 304 – Database System

Page 19: Chapter 2 Relational Data Model-part 2

TABLE: RESULT

FP 304 – Database System

RESULT ß(Л  clientNo, fName, lName(Client)) X ((Л clientNo, propertyNo, comment (Viewing))

RESULT

Page 20: Chapter 2 Relational Data Model-part 2

Join

¨  Two relations R and S over all common attributes. ¨  One occurrence of each common attribute is eliminated from

the result. ¨  Symbol : ¨  Syntax:(Л  <attributelist>(R)) (Л  <attributlist> (R)) ¨  Types of Join:

¤  Inner Join ¤  Outer Join ¤  Theta Join ¤  Natural Join ¤  Equi Join ¤  Semi Join

FP 304 – Database System

Please Find the definition

Page 21: Chapter 2 Relational Data Model-part 2

Inner join

¨  An INNER JOIN produces a result table containing composite rows created by combining rows from two tables where some join condition evaluates to true.

¨  Rows that do not satisfy the join condition will not appear in the result table of an inner join.

¨  The inner join is the default join type, therefore the keyword INNER is optional and may be omitted.

FP 304 – Database System

Page 22: Chapter 2 Relational Data Model-part 2

ClientNo fName lname

CR76 John Kay

CR56 Aline Stewart

CR74 Mike Ritchie

CR62 Mary Tregear

ClientNo propertyNo comment

CR56 PA14 Too small

CR76 PG4 Too remote

CR56 PG4

CR62 PA14 No dining room

CR56 PG36

TABLE: client TABLE: VIEWING

FP 304 – Database System

Example

Page 23: Chapter 2 Relational Data Model-part 2

Example

Client.ClientNo fname lname comment.ClientNo propertyNo comment

CR76 John Kay CR76 PG4 Too remote

CR56 Aline Stewart CR56 PA14 Too small

CR56 Aline Stewart CR56 PG4

CR56 Aline Stewart CR56 PG36

CR62 Mary Tregear CR62 PA14 No dining room

Result ß (Л  clientNo, fName, lName(Client)) (Л  clientNo, propertyNo, comment (Viewing))

TABLE: Result

FP 304 – Database System

Viewing.

Page 24: Chapter 2 Relational Data Model-part 2

Outer join

¨  There are two types of outer join: ¤ Left Outer Join ¤ Right Outer Join

FP 304 – Database System

Page 25: Chapter 2 Relational Data Model-part 2

Left outer join

¡  The left outer join operation keeps every tuple in the first or left relation R in R and S;

¡  If no matching tuple is found in S, then the attributes of S in the join result are filled with null values.

¡  Symbol :

FP 304 – Database System

Page 26: Chapter 2 Relational Data Model-part 2

Left outer join

Result ß (Л  P.PROJNO, PPROJNAME, DEPNO(Project)) Department

FP 304 – Database System

Null Null

Page 27: Chapter 2 Relational Data Model-part 2

Right outer join

¡  A similar operation with left outer join, right outer join, keeps every tuple in the second or right relation S in the result of R and S.

¨  Symbol :

FP 304 – Database System

Page 28: Chapter 2 Relational Data Model-part 2

Right outer join

Result ß (Л  P.PROJNO, PPROJNAME, DEPNO(Project)) Department)

FP 304 – Database System

Null Null Null

Page 29: Chapter 2 Relational Data Model-part 2

Traditional set operator from set theory

¨  There are 3 traditional set operator : ¤ Union ¤  Intersection ¤ Difference

Page 30: Chapter 2 Relational Data Model-part 2

Union

¨  Relation that includes all tuples that are either in R or in S or in both R and S.

¨  Table must have the same number of attribute. ¨  Duplicate tuples are eliminated. ¨  Symbol : ∪ ¨  Syntax :  Л  <attribute list>(R) ∪ Л  <attribute list>(R)

FP 304 – Database System

Page 31: Chapter 2 Relational Data Model-part 2

Union FP 304 – Database System

Page 32: Chapter 2 Relational Data Model-part 2

Union List all cities where there is in branch office or in property for rent.

Result1 ß Л  city(Branch) Result2 ß Л  city(PropertyForRent) Result_Union ß Result1 ∪ Result2

FP 304 – Database System

Page 33: Chapter 2 Relational Data Model-part 2

FP 304 – Database System

Result_Union ß  Л  city(Branch) ∪ Л  city(PropertyForRent)

Result

Page 34: Chapter 2 Relational Data Model-part 2

Union compatibility

¡  The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n.

¡  In other words, two relations are union compatibles if they

have the same degree and the ¡  same number attribute ¡  and domains

FP 304 – Database System

Page 35: Chapter 2 Relational Data Model-part 2

Union compatibility

¨  The relations below is said to be union compatibility relation

FP 304 – Database System

Page 36: Chapter 2 Relational Data Model-part 2

Intersection

¨  Only those tuples that appear in both of the named relation are given as an output result.

¨  The two operand must be type compatible. ¨  Symbol : ∩ ¨  R ∩ S ¨  Syntax: Л  <attributelist>(R) ∩ Л  <attributelist>(R)

FP 304 – Database System

Page 37: Chapter 2 Relational Data Model-part 2

Intersection

TION Example

FP 304 – Database System

Page 38: Chapter 2 Relational Data Model-part 2

Intersection

¨  List all cities where there is both a branch office and at least one property for rent.

¨  Л  city(Branch) ∩ Л  city(PropertyForRent)

FP 304 – Database System

Page 39: Chapter 2 Relational Data Model-part 2

Difference

¨  Subtracts from the first named relation those tuples that appear in the second named relation and create a new relation.

¨  The two operand must be type compatible. ¨  Symbol : - (minus) ¨  Syntax: Л  <attributelist>(R) – Л<attributelist>(R)

FP 304 – Database System

Page 40: Chapter 2 Relational Data Model-part 2

Difference FP 304 – Database System

Page 41: Chapter 2 Relational Data Model-part 2

Difference

¨  List all cities where there is a branch office but no properties for rent.

 Л  city(Branch) – Лcity(PropertyForRent)

FP 304 – Database System