Lecture 3.1 and 3.2 Relational Algebra

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 36

Department of Computer Science and Engineering (CSE)

Database
Management
System

Course Outcome Will be covered in


CO Title Level this lecture
Number

CO1 To perceive the significance and Remember


implementation of a commercial
 
relational database system (Oracle)
by writing SQL using the system.
CO2 To understand the relational database Understand
theory, and be able to write
 
relational algebra expressions for
queries
CO3 To identify the basic issues of Analysis and
transaction processing and application
concurrency control and find out its
solutions.
2
University Institute of Engineering (UIE)
Department
Department
of Computer
of Computer
Science
Scienceand
andEngineering
Engineering (CSE)
(CSE)
Contents of the Syllabus
UNIT-I [10h]
Overview of Databases: Database concepts, DBMS, Data Base System Architecture (Three
Level ANSI-SPARC Architecture), Advantages and Disadvantages of DBMS, Data Independence,
DBA and Responsibilities of DBA, Relational Data Structure, Keys, Relations, Attributes, Schema and
Instances, Referential integrity, Entity integrity.
Data Models: Relational Model, Network Model, Hierarchical Model, ER Model: Design,
issues, Mapping constraints, ER diagram, Comparison of Models.

Relational Algebra & Relational Calculus: Introduction, Syntax, Semantics, Additional


operators, Grouping and Ungrouping, Relational comparisons, Tuple Calculus, Domain Calculus,
Calculus Vs Algebra, Computational capabilities.

UNIT-II [10h]
Functional dependencies and Normalization: Functional dependencies, Decomposition, Full
Functional Dependency (FFD), Transitive Dependency (TD), Join Dependency (JD), Multi-valued
Dependency (MVD), Normal Forms (1NF, 2NF, 3NF, BCNF), De-normalization.
Database Security: Introduction, Threats, Counter Measures.
Control Structures: Introduction to conditional control, Iterative control and sequential control
statements, Cursors, Views.

3
University Institute of Engineering (UIE)
Department
Department
of Computer
of Computer
Science
Scienceand
andEngineering
Engineering (CSE)
(CSE)
Contents of the Syllabus

UNIT-III
[10h]
Package, Procedures and Triggers: Parts of procedures, Parameter modes, Advantages of
procedures, Syntax for creating triggers, Types of triggers, package specification and package body,
developing a package, Bodiless package, Advantages of packages.
Transaction Management and Concurrency Control: Introduction to Transaction Processing,
Properties of Transactions, Serializability and Recoverability, Need for Concurrency Control, Locking
Techniques, Time Stamping Methods, Optimistic Techniques and Granularity of Data items.

Database Recovery of database: Introduction, Need for Recovery, Types of errors,


Recovery Techniques.

4
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Chapter 1.3

(Relational Algebra & Relational Calculus)

Relational Algebra & Relational Calculus: Introduction, Syntax, Semantics, Additional


operators, Grouping and Ungrouping, Relational comparisons, Tuple Calculus, Domain
Calculus, Calculus vs Algebra, Computational capabilities.

University Institute of Engineering (UIE) 5


Department of Computer Science and Engineering (CSE)

Learning Objective
• To study relational algebra concepts,
selection ,projection ,relational calculus which helps in
understanding queries
• Fundamentals operations on Relational Algebra
• Tuple and Domain Relational Calculus

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Learning Outcome
• Apply relational database theory, and be able to write
relational algebra expressions for queries
• Utilize the knowledge of basics of SQL and construct
queries using SQL
• Understanding relational algebra operators including
extended operators

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Introduction
• Relational algebra
– Basic set of operations for the relational model
– More operational (procedural), very useful for representing
execution plans.

• Relational calculus
– Higher-level declarative language for specifying relational queries
– Lets users describe what they want, rather than how to compute
it: Non-operational, declarative.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Relational Algebra
• Relational algebra operations work on one or more relations to define
another relation without changing the original relations.

• Both operands and results are relations, so output from one


operation can become input to another operation.

• Allows expressions to be nested, just as in arithmetic. This property is


called closure.

• Five basic operations in relational algebra: Selection, Projection,


Cartesian product, Union and Set Difference.

• These perform most of the data retrieval operations needed.

• Also have Join, Intersection, and Division operations, which can be


expressed in terms of 5 basic operations.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Relational Algebra

• Basic operations:
– Selection (  ) Selects a subset of rows from relation.
– Projection ( 
) Deletes unwanted columns from relation.
– Cross-product ( 
) Allows us to combine two relations.
– Set-difference (
– Union (

) Tuples in reln. 1, but not in reln. 2.
 ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!) useful.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Relational Algebra Operations

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Relational Algebra Operations

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Selection (or Restriction)


• predicate (R)
– Works on a single relation R and defines a relation that contains only
those tuples (rows) of R that satisfy the specified condition (predicate).
– Selects rows that satisfy selection condition.
– No duplicates in result!
– Schema of result identical to schema of input relation.
– Selection is distributive over binary operators
– Selection is commutative

13
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Example - Selection Restriction)


• List all staff with a salary greater than £10,000.

salary > 10000 (Staff)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Projection
• col1, . . . , coln(R)
– Works on a single relation R and defines a relation that contains a
vertical subset of R, extracting the values of specified attributes and
eliminating duplicates.
– Deletes attributes that are not in projection list.
– Schema of result contains exactly the fields in the projection list,
with the same names that they had in the input relation.
– Projection operator has to eliminate duplicates!

Note: real systems typically don’t do duplicate elimination unless the


user explicitly asks for it (by DISTINCT). Why not?

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Projection
• Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.

staffNo, fName, lName, salary(Staff)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Union
• RS
– Union of two relations R and S defines a relation that contains all the
tuples of R, or S, or both R and S,
duplicate tuples being eliminated.
– R and S must be union-compatible.

• If R and S have I and J tuples, respectively, union is obtained by


concatenating them into one relation with a maximum of (I + J) tuples.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - UNION
• List all cities where there is either a branch office or a property for rent.

city(Branch)  city(PropertyForRent)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Set Difference
• R–S
– Defines a relation consisting of the tuples that are in relation R,
but not in S.
– R and S must be union-compatible.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Set Difference


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

city(Branch) – city(PropertyForRent)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Intersection
• RS
– Defines a relation consisting of the set of all tuples that are in both R
and S.
– R and S must be union-compatible.

• Expressed using basic operations:


R  S = R – (R – S)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Intersection
• List all cities where there is both a branch office and at least one
property for rent.

city(Branch)  city(PropertyForRent)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Cartesian product
• RXS
– Defines a relation that is the concatenation of every tuple of
relation “R with every tuple of relation S”

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Cartesian product


• List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment (Viewing))

24
Pearson Education © 2009
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Example - Cartesian product and


Selection
• Use selection operation to extract those tuples where Client.clientNo =
Viewing.clientNo.
sClient.clientNo = Viewing.clientNo((ÕclientNo, fName, lName(Client))  (ÕclientNo, propertyNo,
comment(Viewing)))

 Cartesian product and Selection can be reduced to a single operation called a Join.

25
Pearson Education © 2009
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Join Operations
• JOINs can be used to combine tables

• Join is a derivative of Cartesian product.

• Equivalent to performing a Selection, using join predicate as selection


formula, over Cartesian product of the two operand relations.

• One of the most difficult operations to implement efficiently in an


RDBMS and one reason why RDBMSs have intrinsic performance
problems.

26
Pearson Education © 2009
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Join Operations
• Various forms of join operation
– JOIN (Inner Join) : Return rows when there is at least one match
in both tables
– LEFT JOIN(Left Outer Join): Return all rows from the left
table, even if there are no matches in the right table
– RIGHT JOIN(Right Outer Join): Return all rows from the right
table, even if there are no matches in the left table
– FULL JOIN: Return rows when there is a match in one of the
tables

27
Pearson Education © 2009
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

SQL INNER JOIN Syntax


SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON
table_name1.column_name=table_name2.column_name

INNER JOIN is the same as JOIN


The word "INNER" is optional

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Inner Join


*

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Left Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example - Left Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Right Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example – Right Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Full Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example – Full Join

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

FAQ
1. How does Tuple-oriented relational calculus differ from domain-
oriented relational calculus?
2. Define Relational Algebra and Relational Calculus
3. What is degree of a Relation?
4. What is a Relation Schema and a Relation?
5. List various Operators used in Relational Algebra and Calculus
6. Solve the given by relational algebra. The Relations are :
• Emp(empno,ename,sal,job,comm,deptno)
• Dept (deptno,dname,dloc)
•  Create a query to list unique jobs that are in deptno 30 and include the
location of department 30 in output

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

References
OTHER REFRENCES
•     https://cs.uwaterloo.ca/~tozsu/courses/CS348/notes/4b-calculus-handout-notes.pdf
• http://www.ccs.neu.edu/home/kathleen/classes/cs3200/4-RAAndRC.pdf
• https://www2.cs.sfu.ca/CourseCentral/354/louie/Chap3_practice.pdf
• http://www.nyu.edu/classes/jcf/CSCI-GA.2433-001_fa11/slides/session5/RelationalAlgebra-
RelationalCalculus-SQL.pdf
• https://blog.inf.ed.ac.uk/da15/files/2015/01/inf1-da-15-t3.pdf
• DatabaseSystemConceptsbySudarshan,Korth(McGraw-HillEducation)

• FundamentalsofDatabaseSystemByElmasari&Navathe-PearsonEducation
• http://ecomputernotes.com/database-system/rdbms
• https://www.tutorialspoint.com/sql/sql-rdbms-concepts.htm

• https://www.studytonight.com/dbms/rdbms-concept
SUGGESTED BOOK REFERENCES
• Ramez Elmasri and Shamkant B. Navathe,“Fundamentals of Database System”, The
Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database SystemConcepts”, McGraw Hall.
• Pratt,”DBMS”, Cengage Learning.

University Institute of Engineering (UIE)

You might also like