Docsity

Understanding Relational Algebra: Selection, Projection, Union, Intersection, Difference, Exams of Principles of Database Management

Toggle sidebar
bg1
Week # 15
Relational Algebra
1) Background
* For data manipulation, Dr. Codd suggested two generic languages, both based on mathematical
principles
* The two languages provide well-defined methods to work on rows and columns of tables
* These two languages are called relational algebra and relational calculus
* Both the algebra and calculus are formal, non-user-friendly languages
* No program code is directly written to contain relational algebra and relational calculus commands or
operations. However, popular languages included in commercial database management systems implement
elements of these generic languages
* Structured Query Language (SQL) is one such implementation based on the generic languages, mostly
based on relational calculus
* To understand how data manipulation is made possible in a relational data model, you need to gain a good
understanding of the two generic languages
2) Relational Algebra
* The relational algebra is a theoretical language with operations that work on one or more relations to
define another relation without changing the original relation(s) (or)
* Relational algebra consists of mathematical operations on rows and columns of one or more relations
* The result of an operation is a new relation, which may have been formed from one or more input
relations
* There are several variations of syntax for relational algebra commands
* The five fundamental operations in relational algebra are selection, projection, cartesian product,
union, and set difference
- Perform most of the data retrieval operations
* In addition, there are also the join, intersection, assignment and division operations
* The selection and projection are unary operations, as they operate on single relation
* The other operations work on pairs of relations and are therefore called binary operations
2.a) Selection Operation (σ)
* Operates on a single relation
* Selects specific rows from a relation on the basis of given criteria
* The result is a relation containing rows and columns
- Rows: only the selected rows from the original relation
- Columns: same columns as in the original relation
Dr. Kifayat Ullah Introduction to Database Systems CECOS University Peshawar
pf3
pf4
pf5

Partial preview of the text

Download Understanding Relational Algebra: Selection, Projection, Union, Intersection, Difference and more Exams Principles of Database Management in PDF only on Docsity!

**Week # 15 Relational Algebra

  1. Background**
  • For data manipulation , Dr. Codd suggested two generic languages , both based on mathematical principles
  • The two languages provide well-defined methods to work on rows and columns of tables
  • These two languages are called relational algebra and relational calculus
  • Both the algebra and calculus are formal, non-user-friendly languages
  • No program code is directly written to contain relational algebra and relational calculus commands or operations. However, popular languages included in commercial database management systems implement elements of these generic languages
  • Structured Query Language ( SQL) is one such implementation based on the generic languages, mostly based on relational calculus
  • To understand how data manipulation is made possible in a relational data model, you **need to gain a good understanding of the two generic languages
  1. Relational Algebra**
  • The relational algebra is a theoretical language with operations that work on one or more relations to define another relation without changing the original relation (s) (or)
  • Relational algebra consists of mathematical operations on rows and columns of one or more relations
  • The result of an operation is a new relation, which may have been formed from one or more input relations
  • There are several variations of syntax for relational algebra commands
  • The five fundamental operations in relational algebra are selection, projection, cartesian product, union, and set difference
  • Perform most of the data retrieval operations
  • In addition, there are also the join, intersection, assignment and division operations
  • The selection and projection are unary operations, as they operate on single relation
  • The other operations work on pairs of relations and are therefore called binary operations 2.a) Selection Operation ( σ)
  • Operates on a single relation
  • Selects specific rows from a relation on the basis of given criteria
  • The result is a relation containing rows and columns
  • Rows: only the selected rows from the original relation
  • Columns: same columns as in the original relation

Example

  • Consider the following PATIENT table
  • Find all the patients who have stayed in the medical center for more than 15 days SELECT (PATIENT : (DateDischarged – DateAdmit) > 15) 2.b) Projection Operation ( π)
  • Operates on a single relation
  • Selects specific columns from a relation on the basis of given criteria
  • The result is a relation containing rows and columns
  • Rows—same rows as in the original relation
  • Columns—only the selected columns from the original relation Example
  • Consider the above PATIENT table again
  • List only the names of patients and their discharge dates
  • PATIENT [PatientName, DateDischarged]
  • The selected columns are indicated within the square brackets 2.c) Assignment Operation (:=)
  • Not really an operation in the sense of other operations
  • Assigns the result of some set of operations to the resulting relation Example
  • Consider the above PATIENT table again
  • Find all the patients who have stayed in the medical center for more than 15 days
  • Assign the result of the operation to a relation called PATIENT_ PATIENT_15:= SELECT (PATIENT : (DateDischarged – DateAdmit) > 15)

2.e) Intersection Operation (∩)

  • Operates on two relations
  • Identifies common rows in two relations
  • Both relations must be union-compatible

- They must have the same columns

  • The result is a single relation containing rows and columns
  • Rows: only those rows that are common to both relations
  • Columns: same columns as in either of the original relations Example
  • Consider the above relations again
  • List all the attributes of only those doctors who have doctors reporting to them and who themselves report to other doctors
  • These are the doctors who are both subordinates as well as having supervisory responsibilities
  • DOCTOR := SUPVRDOCTOR ∩ SUBDOCTOR
  • DOCTOR is name of the result relation
  • The symbol ∩ indicating the intersection operation
  • The intersection operation finds only those entities that are common to both relations 2.f) Difference Operation (–)
  • Operates on **two relations
  • Identifies the rows in one relation that are not present in the other relation**
  • Both relations must be union-compatible
  • The result is a single relation containing rows and columns
  • Rows: o nly those rows that are in the first relation but not in the second
  • Columns: same columns as in either of the original relations Example
  • Consider the above relations again
  • List all the attributes of all doctors who report to some other doctors but have no one reporting to them
  • These are the doctors who are sub- ordinates without any supervisory responsibilities
  • SUBORDDOCTOR := SUBDOCTOR – SUPVRDOCTOR
  • SUBORDDOCTOR is name of the result relation
  • The symbol – indicating the difference operation
  • The difference operation finds only those entities in the SUBDOCTOR relation but not present in the SUPVRDOCTOR relation
  • Note that the result will not be same if you reverse the order of the two relations in the difference operation
  • (A–B) is not the same as (B–A) 2.g) Product Operation (×)
  • Operates on two relations
  • Produces the scalar product between the two relations
  • The result is a single relation containing rows and columns
  • Rows: formed by concatenation each of row of one relation to each row of the other relation
  • Columns: all the columns of both relations put together Example *Consider the following tables
  • Find the product relation C between two relations A and B
  • C := A * B
  • The **symbol *** indicating the product
  • If relation A has m columns and p rows, and relation B has n columns and q rows, then the result relation C will have (m+n) columns and (pq) rows 2.h) Divide Operation (* ÷ , /)
  • Operates on two relations
  • The divide operation is the reverse of the product operation
  • The result is a single relation containing rows and columns
  • Theta join
  • Equi join
  • Natural join
  • Outer join
  • Semijoin Try it Yourself