• Tidak ada hasil yang ditemukan

Database Management System

N/A
N/A
Protected

Academic year: 2025

Membagikan "Database Management System"

Copied!
11
0
0

Teks penuh

(1)

Database Management System

CBCS BHCS10 Parul Chachra

(2)

Chapter 8

The Rela=onal Algebra and Rela=onal Calculus

Reference: Book prescribed as per the syllabus

(3)

DIVISION Opera=on

•  The division opera=on is applied to two rela=ons

•  R(Z) ÷ S(X), where X subset Z

•  Let Y = Z - X (and hence Z = X Y), that is, let Y be the set of aUributes of R that are not

aUributes of S

•  The result of DIVISION is a rela=on T(Y) that

includes a tuple t if tuples tR appear in R with tR [Y] = t, and with

tR [X] = ts for every tuple tx in S.

•  For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combina=on with every tuple in S.

(4)

DIVISION Opera=on

•  Example of Division opera=on:

•  (a) shows Dividing SSN_PNOS by SMITH_PNOS

•  (b) shows T R ÷ S.

(5)

Aggregate Func=ons and Grouping

•  A type of request that cannot be expressed in the basic rela=onal algebra is to specify

mathema=cal aggregate func=ons on collec=ons of values from the database.

•  Examples of such func=ons include retrieving the average or total salary of all employees or the total number of employee tuples.

•  The COUNT func=on is used for coun=ng tuples or values.

•  Common func=ons applied to collec=ons of numeric values include SUM, AVERAGE,

MAXIMUM, and MINIMUM

(6)

Aggregate Func=ons and Grouping

•  Use of the Aggregate Func=onal opera=on

•  MAX Salary (EMPLOYEE) retrieves the maximum salary value from the EMPLOYEE rela=on

•  MIN Salary (EMPLOYEE) retrieves the minimum Salary value from the EMPLOYEE rela=on

•  COUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count (number) of employees and their average salary

•  Note: count just counts the number of rows, without removing duplicates

(7)

Aggregate Func=ons and Grouping

•  Grouping can be combined with Aggregate Func=ons

•  Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY

•  DNO COUNT SSN, AVERAGE Salary (EMPLOYEE)

•  Above opera=on groups employees by DNO (department number) and computes the

count of employees and average salary per department

(8)

Aggregate Func=ons and Grouping

•  Example of Aggregate Func=ons and grouping:

(9)

Recursive Closure Opera=ons

•  Another type of opera=on that, in general,

cannot be specified in the basic original rela=onal algebra is recursive closure.

•  This opera=on is applied to a recursive

rela?onship between tuples of the same type, such as the rela=onship between an employee and a supervisor.

•  An example of a recursive opera=on is to retrieve all SUPERVISEES of an EMPLOYEE e at all levels — that is, all EMPLOYEE e’ directly supervised by e;

all employees e’’ directly supervised by each employee e’; all employees e’’’ directly

supervised by each employee e’’; and so on.

(10)

Recursive Closure Opera=ons

•  For example, to specify the Ssns of all employees eʹ directly supervised—at level one—by the employee e whose name is

‘James Borg’, we can apply the following opera=on:

•  To retrieve all employees supervised by Borg at level 2—that is, all employees eʹʹ supervised by some employee eʹ who is directly supervised by Borg—we can apply another JOIN to the result of the first query, as follows:

•  To get both sets of employees supervised at levels 1 and 2 by

‘James Borg’, we can apply the UNION opera=on to the two results, as follows:

(11)

Rela=onal Algebra Queries

Referensi

Dokumen terkait