9
ATTEMPT THIS ONLY WHEN YOU FINISH THE PRACTICAL EXCERCISES READ THE NOTES BELOW AND ANSWER THE QUESTIONS PLEASE BRING THIS NOTES TO NEXT CLASS Chapter 3 : RELATIONAL ALGEBRA Relational algebra operators operate on Relations (or tables) to produce new relations. In this method data retrieval functions are specified by a procedural language. It is based on algebraic concepts that consist of a collection of operators that are defined upon relations and produce as result other relations. We shall see some basic operators like: Selection, Projection, Union, Intersection, Difference, Cartesian product and Join. 1.Selection: When applied on a relation, this operator results in a horizontal (selected row-wise) selection of a portion of that relation. Consider the following relation: EMPLOYEE EMPLOYEE NAME SEX AGE SALARY Mustafa Male 25 2000 Salima Female 40 3000 Ahad Male 36 4500 Muneera Female 28 3800 The selection operation of Age < 30 or Salary > 4000 gives the following relation: EMPLOYEE NAME AGE SALARY 28

Chapter 3 Relational Algebra

  • Upload
    ayisha

  • View
    114

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Chapter 3 Relational Algebra

ATTEMPT THIS ONLY WHEN YOU FINISH THE PRACTICAL EXCERCISES

READ THE NOTES BELOW AND ANSWER THE QUESTIONS PLEASE BRING THIS NOTES TO NEXT CLASS

Chapter 3 : RELATIONAL ALGEBRA

Relational algebra operators operate on Relations (or tables) to produce new relations. In this method data retrieval functions are specified by a procedural language. It is based on algebraic concepts that consist of a collection of operators that are defined upon relations and produce as result other relations.

We shall see some basic operators like: Selection, Projection, Union, Intersection, Difference, Cartesian product and Join.

1. Selection:When applied on a relation, this operator results in a horizontal (selected row-wise) selection of a portion of that relation.Consider the following relation: EMPLOYEE EMPLOYEE

NAME SEX AGE SALARYMustafa Male 25 2000Salima Female 40 3000Ahad Male 36 4500Muneera Female 28 3800

The selection operation of Age < 30 or Salary > 4000 gives the following relation: EMPLOYEE

NAME AGE SALARY

EMPLOYEENAME AGE SALARY

EMPLOYEENAME AGE SALARY

Mustafa 25 2000

28

Page 2: Chapter 3 Relational Algebra

Ahad 36 4500Muneera 28 3800

Write the selection of all females above 35. EMPLOYEE

NAME SEX AGE SALARY

2. ProjectionProjection gives a vertical (selected columns only) selection of a portion of a relation as the result. EMPLOYEE

NAME DEPARTMENT MANAGERMaryam Sales AliLatifa Sales AliMustafa Marketing SaifMohammed Accounts Mansoor

The projection attributes name and head from the above EMPLOYEE relation would be as follows: EMPLOYEE

NAME MANAGERMaryam AliLatifa AliMustafa SaifMohammed Mansoor

Write the projection of Department and Manager:

3. UnionThis operator when applied on two relations NATIONALS and MANAGERS results in a combination of a new relation. Remember to remove duplicates if any.Consider the following relations: NATIONALS and MANAGERS

NATIONALS ID NAME AGE145 Mohammed 25238 Ali 40

29

Page 3: Chapter 3 Relational Algebra

650 Anisa 36

Then NATIONALS Union MANAGERS gives the following relation.

NATIONALS MANAGERS ID NAME AGE145 Mohammed 25238 Ali 40650 Anisa 3699 Ali 52

4. IntersectionThis operator when applied on two relations GRADUATES and MANAGERS results in a perfectly matching combination of a new relation.Consider the following relation: GRADUATES and MANAGERS

Consider the following relation,

Arabic Speakers English SpeakersID NAME AGE ID NAME AGE145 Mohammed 25 99 Ali 52238 Ali 40 238 Ali 39650 Anisa 36 650 Anisa 26

The GRADUATES intersection MANAGERS gives the following relations:

Arabic speakers English SpeakersID NAME AGE145 Mohammed 25238 Ali 40650 Anisa 36

What is the meaning of the new relation after Intersection?

5. DifferenceThis operator when applied on two relations ARABS and MANAGERS results in a subtraction of rows to result in a new relation.

Consider the following relations: ARABS, MANAGERS

ARABSID NAME AGE145 Mohammed 25238 Ali 40650 Anisa 36

MANAGERS ID NAME AGE99 Ali 52232388 Ali 39650650 Anisa 26

30

Page 4: Chapter 3 Relational Algebra

Then ARABS – MANAGERS gives the following relation. ARABS - MANAGERS

ID NAME AGE145 Mohammed 25

It is like a Subtraction – all records in the first relation subtract all records in the second relation.

What is MANAGERS – ARABS relation?

6. Cartesian ProductIt is also called the Product operation. It takes two relations and returns a relation where every row of the first relation is concatenated with every Tuple in the second relation. Consider the following two relations:

EMPLOYEENAME DEPT GRADEMaryam Sales 4Latifa Sales 4

Mustafa Marketing 5Mohammed Accounts 3

DEPARTMENTDEPT_NAME MANAGER BUDGET

Sales Ali 1,200,000Marketing Saif 50,000Accounts Mansoor 50,000,000

Now the first relation has 3 columns and the second relation has 3 columns. The resulting relation has 3+3=6 columns. Each row of relation EMPLOYEE appears with each relation of DEPARTMENT and so there are 4x3=12 records shown below:EMPLOYEE product DEPARTMENTNAME DEPT GRADE DEPT_NAME MANAGER BUDGETMaryam Sales 4 Sales Ali 1,200,000Maryam Sales 4 Marketing Saif 50,000Maryam Sales 4 Accounts Mansoor 50,000,000Latifa Sales 4 Sales Ali 1,200,000Latifa Sales 4 Marketing Saif 50,000Latifa Sales 4 Accounts Mansoor 50,000,000Mustafa Marketing 5 Sales Ali 1,200,000Mustafa Marketing 5 Marketing Saif 50,000Mustafa Marketing 5 Accounts Mansoor 50,000,000Mohammad Accounts 3 Sales Ali 1,200,000Mohammad Accounts 3 Marketing Saif 50,000Mohammad Accounts 3 Accounts Mansoor 50,000,000

MANAGERSID NAME AGE99 Ali 52238 Ali 39650 Anisa 26

31

Page 5: Chapter 3 Relational Algebra

Write the DEPARTMENT product EMPLOYEE DEPARTMENT product EMPLOYEE DEPT_NAME MANAGER BUDGET NAME DEPT GRADE

Summary of Relational Operations

Summary :

Relational algebra is a set of operations on relational databases that allow retrieval of data.

We know that in mathematics, a set can have any number of subsets. A set is the subject of another if all its members are also members of other set. For example, consider the following two sets:

We can state here that S is a subset of S because all the elements of S are also the elements of S.

As in the case of set theory in mathematics, we can treat a table as a set and derive some rows from that table as its subset. In effect, we will have another table with the same header, but with a different name. For example, from a student table, we can derive another table that contains records for only those students who have passed. Thus we restrict the number of rows that we want.

The idea in a restrict operation is to select only the desired rows and eliminate the unwanted rows.

32

Page 6: Chapter 3 Relational Algebra

The operation of eliminating columns in a table is called a project operation.

Projection operation on a table leads to the formation of another table by copying specified columns of the original table. During the process, if any duplicate rows are found because of this elimination, they are ignored too.

Product we know of Cartesian product in mathematics. Cartesian product of two sets yields a third set. This set contains pairs of elements. Here, the first element in each pair belongs to the first set, and the second element in each pair belongs to the second set.

Union In mathematical set theory, the union of two sets is a set that contains elements present in either or both of the sets. In the process, any resulting duplicates are eliminated.

By using union, multiple queries can be put together and their output merged.

Intersection In mathematics, the intersection of two sets is the set of elements that are common to both the sets.

The result of intersection is the rows common to the rows produced by the individual queries.

In mathematics, the difference between two sets is a set that contains members of the first set that are not in the second set.

Join joins two or more tables by using operators such as equal to, not equal to, greater than etc.

Joining two (or more) tables has some prerequisites, if it has to be of some practical value:

The tables should be joined based on a common column.The common column should be compatible in terms of domain.

TUTORIAL 4

1. What does relational algebra work with?

2. Explain the following operators:

a. Selection

b. Projection

33

Page 7: Chapter 3 Relational Algebra

c. Union

d. Difference

4. What does a Join operator do?

5. Perform a join operation of the following relations where PROJ_NAME = PROJ_NAME?

PROJECTPROJ_NAME LEADER LOCATIONSultan Centre Ali Azaiba

Markat Al Bahja Saif SeebVilla Mansoor Qurum

PROJECTSTAFF_NAME PROJ_NAME TIME

Ahmed Sultan Centre 2 monthsBader Villa 1 month

Khalfan Villa 3 months

PROJECT join STAFFPROJ_NAME LEADER LOCATION STAFF_NAME PROJ_NAME TIME

34