0% found this document useful (0 votes)
58 views96 pages

DBMS - Module2 - Till Cross Product

Database management system CMS and e-commerce

Uploaded by

darshane.s44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views96 pages

DBMS - Module2 - Till Cross Product

Database management system CMS and e-commerce

Uploaded by

darshane.s44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

Database Management System

Module: 2

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 1
Relational Model Concepts

• The relational Model of Data is based on the concept of a Relation


• The strength of the relational approach to data management comes from the formal
foundation provided by the theory of relations
• A Relation is a mathematical concept based on the ideas of sets
• The model was first proposed by Dr. E.F. Codd of IBM Research in 1970 in the following
paper:
• "A Relational Model for Large Shared Data Banks," Communications of the ACM,
June 1970
• The above paper caused a major revolution in the field of database management and
earned Dr. Codd the coveted ACM Turing Award

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 2
Basic Idea of Relational Model

• Informally, a relation looks like a table of values.

• A relation typically contains a set of rows.

• The data elements in each row represent certain facts that correspond to a real-world
entity or relationship
• In the formal model, rows are called tuples

• Each column has a column header that gives an indication of the meaning of the data
items in that column
• In the formal model, the column header is called an attribute name (or just attribute)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 3
Relational Model Concepts
• The table is called relation (or) simply, a table is a collections of related attributes and there is a
relation among the attributes.

• The relational model is today the primary data model for commercial data processing
applications.

• A relational database consists of a collection of tables, each of which is assigned a unique name.

• A row in a table represents a relationship among a set of values. Since a table is a collection of
such relationships, there is a close correspondence between the concept of table and the
mathematical concept of relation, from which the relational data model takes its name.

• Thus, in the relational model the term relation is used to refer to a table, while the term tuple is
used to refer to a row. Similarly, the term attribute refers to a column of a table.

4
Example of a Relation

attributes
(or columns)

tuples
(or rows)

5
Basic structure
• Formally, given sets D1, D2, …. Dn, a relation r is a subset of D1 x D2 x … x. Dn
Thus a relation is a set of n-tuples (a1, a2, …, an) where each ai ∈ Di

Example: if
customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}

Then r = { (Jones, Main, Harrison),


(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield)}

is a relation over customer-name x customer-street x customer-city

6
Example

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 7
Schema Definition

• The Schema (or description) of a Relation:


• Denoted by R(A1, A2, .....An)
• R is the name of the relation
• The attributes of the relation are A1, A2, ..., An
• Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
• CUSTOMER is the relation name
• Defined over the four attributes: Cust-id, Cust-name, Address, Phone#
• Each attribute has a domain or a set of valid values.
• For example, the domain of Cust-id is 6 digit numbers.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 8
Domain Definition

• A domain has a logical definition:


• Example: “USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S.
• A domain also has a data-type or a format defined for it.
• The USA_phone_numbers may have a format: (ddd)ddd-dddd where each d is a decimal
digit.
• Dates have various formats such as year, month, date formatted as yyyy-mm-dd, or as dd
mm,yyyy etc.

• The attribute name designates the role played by a domain in a relation:


• Used to interpret the meaning of the data elements corresponding to that attribute
• Example: The domain Date may be used to define two attributes named “Invoice-date”
and “Payment-date” with different meanings

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 9
Tuple definition

• A tuple is an ordered set of values (enclosed in angled brackets ‘< … >’)


• Each value is derived from an appropriate domain.
• A row in the CUSTOMER relation is a 4-tuple and would consist of four values, for
example:
• <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000">
• This is called a 4-tuple as it has 4 values
• A tuple (row) in the CUSTOMER relation.
• A relation is a set of such tuples (rows)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 10
Relation State

• The relation state is a subset of the Cartesian product of the domains of its attributes
• each domain contains the set of all possible values the attribute can take.
• Example: attribute Cust-name is defined over the domain of character strings of
maximum length 25
• dom(Cust-name) is varchar(25)
• The role these strings play in the CUSTOMER relation is that of the name of a customer.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 11
Relation- Attribute , Domain and Degree

• A relation schema R, denoted by R(A1, A2, … , An), is made up of a relation name R


and a list of attributes, A1, A2, … , An.

• Each attribute Ai is the name of a role played by some domain D in the relation schema
R.

• D is called the domain of Ai and is denoted by dom(Ai).

• A relation schema is used to describe a relation; R is called the name of this relation.

• The degree (or arity) of a relation is the number of attributes n of its relation schema.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 12
Relation Model-Notations

• A relation schema R of degree n is denoted by R(A1, A2, … , An).


• The uppercase letters Q, R, S denote relation names.
• The lowercase letters q, r, s denote relation states.
• The letters t, u, v denote tuples.
• An attribute A can be qualified with the relation name R to which it belongs by using the
dot notation R.A—for example, STUDENT.Name or STUDENT.Age. This is because the
same name may be used for two attributes in different relations. However, all attribute
names in a particular relation must be distinct.
• An n-tuple t in a relation r(R) is denoted by t = <v1, v2, … , vn>, where vi is the value
corresponding to attribute Ai. The following notation refers to component values of
tuples:
• Both t[Ai] and t.Ai (and sometimes t[i]) refer to the value vi in t for attribute Ai.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 13
Terms in RDBMS

Informal Terms Formal Terms


Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple

Table Definition Schema of a Relation


Populated Table State of the Relation

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 14
Characteristics Of Relations

1. Ordering of tuples in a relation r(R):


• The tuples are not considered to be ordered, even though they appear to be in the
tabular form.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 15
Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 16
Characteristics of Relations

2. Ordering of attributes in a relation schema R (and of values within each tuple) does not
require this ordering
• But for ease of handling, we will consider the attributes in R(A1, A2, ..., An) and the
values in t=<v1, v2, ..., vn> to be ordered .

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 17
Characteristics Of Relations

3. Values in a tuple:
• All values are considered atomic (indivisible).
• Each value in a tuple must be from the domain of the attribute for that column
• If tuple t = <v1, v2, …, vn> is a tuple (row) in the relation state r of R(A1, A2,
…, An)
• Then each vi must be a value from dom(Ai)

• A special null value is used to represent values that are unknown or inapplicable to
certain tuples

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 18
MCQs

1) What is an Instance of a Database?


a) The logical design of the database system
b) The entire set of attributes of the Database put together in a single relation
c) The state of the database system at any given point of time
d) The initial values inserted into the Database immediately after its creation
Answer option c
2) The value of an attribute in a table may be NULL because,
a) the value is not known b) the value does not exist
c) the value is not applicable d) all of the above
Answer option D

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 19
3. In RDBMS terminology a table is called a …………..
a)Range b)domain c)relation d)tuple

Answer option c)relation

4. The term attribute refers to a ……………… of a table.


a) Record b) Tuple c)column d) Key

Answer option c)column

5. A domain is atomic if the elements are considered to be …………….units.


b) Different b) indivisible c) constant d)divisible

Answer option b

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 20
Relational Model Constraints

• In a relational database, there will typically be many relations, and the tuples in those
relations are usually related in various ways.
• The state of the whole database will correspond to the states of all its relations at a
particular point in time.
• There are generally many restrictions or constraints on the actual values in a database
state

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 21
Categories of constraints on databases
1. Inherent model-based constraints or implicit constraints: Constraints that are
inherent in the data model
2. Schema-based constraints or explicit constraints : Constraints that can be directly
expressed in the schemas of the data model by specifying them in the DDL
3. Application-based or semantic constraints or business rules : Constraints that
cannot be directly expressed in the schemas of the data model, and hence must be
expressed and enforced by the application programs

• Another important category of constraints is data dependencies, which include functional


dependencies and multivalued dependencies.
• They are used mainly for testing the “goodness” of the design of a relational database and
are utilized in a process called normalization

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 22
Schema based constraints

• Domain constraints
• Key constraints
• Constraints on NULLs
• Entity integrity constraints
• Referential integrity constraints.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 23
Domain Constraints

• Domain constraints specify that within each tuple, the value of each attribute A must be an
atomic value from the domain dom(A).
• The data types associated with domains typically include standard numeric data types for
integers (such as short integer, integer, and long integer) and real numbers (float and
double-precision float).
• Characters, Booleans, fixed-length strings, and variable-length strings are also available,
as are date, time, timestamp, and other special data types.
• Domains can be even enumerated data types.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 24
Key Constraints

• Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK]  t2[SK]
• This condition must hold in any valid state r(R)
• A superkey SK specifies a uniqueness constraint that no two distinct tuples in any state r
of R can have the same value for SK.
• Every relation has at least one default superkey— the set of all its attributes.
• Eg. {Ssn, Name, Age},{SSN,Name,Address},{SSN,Name}

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 25
Key

• A "minimal" superkey
• That is, a key is a superkey K such that removal of any attribute from K results in a
set of attributes that is not a superkey (does not possess the superkey uniqueness
property)
• {Ssn, Name, Age} {Ssn}
• In general, any super key formed from a single attribute is also a key.
• A key with multiple attributes must require all its attributes together to have the
uniqueness property.

• Any key is a superkey (but not vice versa)


• Any set of attributes that includes a key is a superkey

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 26
Candidate Key
• A relation schema may have more than one key. In this case, each of the keys is called a
candidate key.
• Eg.

• One of the candidate keys is designated as the primary key of the relation

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 27
Constraints on NULLs

• Certain attributes can be defined NOT NULL in schema definition.


• Hence it won’t allow null value for that attribute in any valid tuple.

Entity Integrity Constraint


• It specifies that a primary key can never contain null value.
• If null value allowed it can’t uniquely identify a tuple.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 28
Sample Dataset

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 29
Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 30
Referential Integrity

• A constraint involving two relations


• The previous constraints involve a single relation.
• Used to specify a relationship among tuples in two relations:
• The referencing relation and the referenced relation.
• The referential integrity constraint states that a tuple in one relation that refers to another
relation must refer to an existing tuple in that relation.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 31
Foreign Key – Formal Definition

• A set of attributes FK in relation schema R1 is a foreign key of R1 that references


relation R2 if it satisfies the following rules:
• 1. The attributes in FK have the same domain(s) as the primary key attributes PK of
R2; the attributes FK are said to reference or refer to the relation R2.
• 2. A value of FK in a tuple t1 of the current state r1(R1) either occurs as a value of
PK for some tuple t2 in the current state r2(R2) or is NULL.
• Relation R1 have attributes foreign key attributes is called referencing relation.
• Relation R2 that holds same attribute as primary key is called referenced relation.
• A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].
• A referential integrity constraint can be displayed in a relational database schema as a
directed arc from R1.FK to R2.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 32
Schema Diagram with Foreign Key

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 33
Referential Integrity Constraints in Company
Database

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 34
Other Constraints

• Semantic Integrity Constraints:


• based on application semantics and cannot be expressed by DDL
• Example: “ Salary of an employee should not be higher than his supervisor salary”
• A constraint specification language may have to be used to express these
• SQL allows triggers and ASSERTIONS to express for some of these

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 35
State Constraints and Transition Constraints

• All constraints discussed so far are state constraints since it is applicable on any valid
state,
• Transition constraints can be defined to deal with state changes in the database.
• An example of a transition constraint is: “the salary of an employee can only increase.”
• Such constraints are typically enforced by the application programs or specified using
active rules and triggers

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 36
Foreign key representation : Another Example

37
Update Operations, Transactions,
and Dealing with Constraint Violations
• There are mainly 3 update operations basically possible on dataset in ant database
• Insert
• Delete
• Update

• Having specified the constraints, it can’t be violated by any of these operations.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 38
In case of integrity violation, several actions can be taken

• Cancel the operation that causes the violation (RESTRICT or REJECT option)
• Perform the operation but inform the user of the violation
• Trigger additional updates so the violation is corrected (CASCADE option, SET
NULL option)
• Execute a user-specified error-correction routine

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 39
Sample Dataset

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 40
Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 41
Insertion Operation
• INSERT may violate any of the constraints:
• Domain constraint:
• if one of the attribute values provided for the new tuple is not of the specified
attribute domain
• Key constraint:
• if the value of a key attribute in the new tuple already exists in another tuple in
the relation
• Referential integrity:
• if a foreign key value in the new tuple references a primary key value that does
not exist in the referenced relation
• Entity integrity:
• if the primary key value is null in the new tuple
• Insertion operation will be just rejected if any of the attribute violates defined constraints

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 42
Operation:
Delete the WORKS_ON tuple with
Essn = ‘999887777’ and Pno = 10.

Result: This deletion is acceptable and


deletes exactly one tuple.
• Operation:
• Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
• Result: This deletion is not acceptable, because there are tuples in WORKS_ON that
refer to this tuple.
• Hence, if the tuple in EMPLOYEE is deleted, referential integrity violations will result.
• Operation:
• Delete the EMPLOYEE tuple with Ssn = ‘333445555’.
• Result: This deletion will result in even worse referential integrity violations, because the
tuple involved is referenced by tuples from the EMPLOYEE,DEPARTMENT,
WORKS_ON, and DEPENDENT relations.
Delete Operation

• DELETE may violate only referential integrity:


• If the primary key value of the tuple being deleted is referenced from other tuples in
the database
• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL
• RESTRICT option: reject the deletion
• CASCADE option: delete those referencing tuples in the child relations.
• SET NULL option: set the foreign keys of the referencing tuples to NULL
• One of the above options must be specified during database design for each foreign
key constraint

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 46
• Operation:
• Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
• Result: This deletion is not acceptable, because there are tuples in WORKS_ON that
refer to this tuple.
• Hence, if the tuple in EMPLOYEE is deleted, referential integrity violations will result.
• Operation:
• Delete the EMPLOYEE tuple with Ssn = ‘333445555’.
• Result: This deletion will result in even worse referential integrity violations, because the
tuple involved is referenced by tuples from the EMPLOYEE,DEPARTMENT,
WORKS_ON, and DEPENDENT relations.
Delete Operation

• DELETE may violate only referential integrity:


• If the primary key value of the tuple being deleted is referenced from other tuples in
the database
• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL
• RESTRICT option: reject the deletion
• CASCADE option: delete those referencing tuples in the child relations.
• SET NULL option: set the foreign keys of the referencing tuples to NULL
• One of the above options must be specified during database design for each foreign
key constraint

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 49
When to use the option?

ON DELETE CASCADE
• A foreign key with cascade delete means that if a record in the parent table is deleted, then
the corresponding records in the child table will automatically be deleted.
• This option is specified when this FK is also a part of PK in child relation
ON DELETE SET NULL
• If set null option is specified on the foreign key, if the primary key in parent is deleted
corresponding referring tuples values will be set to null.
• This option can be specified if the FK is not part the PK of the table

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 50
Update Operation

• UPDATE may violate domain constraint and NOT NULL constraint on an attribute being
modified
• Any of the other constraints may also be violated, depending on the attribute being
updated:
• Updating the primary key (PK):
• Similar to a DELETE followed by an INSERT
• Need to specify similar options to DELETE
• Updating a foreign key (FK):
• May violate referential integrity
• Updating an ordinary attribute (neither PK nor FK):
• Can only violate domain constraints

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 51
Transaction

• This concept is relevant in the context where multiple users and/or application programs
are accessing and updating the database concurrently.
• A transaction is a logical unit of work that may involve several accesses and/or updates to
the database (such as what might be required to reserve several seats on an airplane
flight).
• The point is that, even though several transactions might be processed concurrently, the
end result must be as though the transactions were carried out sequentially.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 52
Exercise
• Consider the following relations for a database that keeps track of student enrollment in
courses and the books adopted for each course:
• STUDENT(SSN, Name, Major, Bdate)
• COURSE(Course#, Cname, Dept)
• ENROLL(SSN, Course#, Quarter, Grade)
• BOOK_ADOPTION(Course#, Quarter, Book_ISBN)
• TEXT(Book_ISBN, Book_Title, Publisher, Author)
• Draw a relational schema diagram specifying the foreign keys for this schema.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 53
SOLUTION

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 54
RELATIONAL ALGEBRA

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 55
RELATIONAL ALGEBRA

• Relational algebra is the basic set of operations for the relational model
• These operations enable a user to specify basic retrieval requests (or queries)
• The result of an operation is a new relation, which may have been formed from one or
more input relations

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 56
What is an Algebra?
A language based on operators and a domain of values.
Operators map values taken from the domain into other domain values.
Hence, an expression involving operators and arguments produces a value in the domain.
When the domain is a set of all relations (and the operators are as described later), we get
the relational algebra
We refer to the expression as a query and the value produced as the query result.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 57
• The algebra operations thus produce new relations
• These can be further manipulated using operations of the same algebra
• A sequence of relational algebra operations forms a relational algebra expression
• The result of a relational algebra expression is also a relation that represents the result
of a database query (or retrieval request)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 58
Relational Algebra consists of several groups of operations

• Unary Relational Operations


• SELECT (symbol:  (sigma))
• PROJECT (symbol:  (pi))
• RENAME (symbol:  (rho))
• Relational Algebra Operations From Set Theory
• UNION (  ), INTERSECTION (  ), DIFFERENCE (or MINUS, – )
• CARTESIAN PRODUCT ( x )
• Binary Relational Operations
• JOIN (several variations of JOIN exist)
• DIVISION
• Additional Relational Operations
• OUTER JOINS, OUTER UNION
• AGGREGATE FUNCTIONS (These compute summary of information: for
example, SUM, COUNT, AVG, MIN, MAX)
Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 59
UNARY OPERATORS-SELECTION AND PROJECTION

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 60
Unary Relational Operations: SELECT

• The SELECT operation (denoted by  (sigma)) is used to select a subset of the tuples from a
relation based on a selection condition.
• The selection condition acts as a filter
• Keeps only those tuples that satisfy the qualifying condition
• Tuples satisfying the condition are selected whereas the other tuples are discarded
(filtered out)
• Examples:
• Select the EMPLOYEE tuples whose department number is 4:

 DNO = 4 (EMPLOYEE)
• Select the employee tuples whose salary is greater than $30,000:

 SALARY > 30,000 (EMPLOYEE)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 61
• In general, the select operation is denoted by  <selection condition>(R)
where
• the symbol  (sigma) is used to denote the select operator
• the selection condition is a Boolean (conditional) expression specified on the attributes
of relation R
• tuples that make the condition true are selected
• appear in the result of the operation
• tuples that make the condition false are filtered out
• discarded from the result of the operation

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 62
SELECT Operation Properties

• The SELECT operation  <selection condition>(R) produces a relation S that has the same schema
(same attributes) as R
• SELECT  is commutative:
•  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))
• Because of commutativity property, a cascade (sequence) of SELECT operations may be
applied in any order:
• <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))
• A cascade of SELECT operations may be replaced by a single selection with a conjunction
of all the conditions:
• <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
• The number of tuples in the result of a SELECT is less than (or equal to) the number
of tuples in the input relation R

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 63
EXAMPLE

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 64
ANOTHER EXAMPLE

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 65
VARIOUS OPERATORS IN SELECTION LOGIC

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 66
FIND EMPLOYEES AGED MORE THAN 30 AND
SALARY>40000

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 67
FIND THE CITIZENS WHOSE RESIDENCE AND PLACE OF
BIRTH ARE SAME

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 68
Unary Relational Operations: PROJECT

• PROJECT Operation is denoted by  (pi)


• This operation keeps certain columns (attributes) from a relation and
discards the other columns.
• PROJECT creates a vertical partitioning
• The list of specified columns (attributes) is kept in each tuple
• The other attributes in each tuple are discarded
• Example: To list each employee’s first and last name and salary, the
following is used:
LNAME, FNAME,SALARY(EMPLOYEE)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 69
PROJECT

• The general form of the project operation is:


<attribute list>(R)
•  (pi) is the symbol used to represent the project operation
• <attribute list> is the desired list of attributes from relation R.
• The project operation removes any duplicate tuples
• This is because the result of the project operation must be a set of tuples
• Mathematical sets do not allow duplicate elements

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 70
PROJECT Operation Properties

• The number of tuples in the result of projection <list>(R) is always less or equal
to the number of tuples in R
• If the list of attributes includes a key of R, then the number of tuples in the result of
PROJECT is equal to the number of tuples in R
• PROJECT is not commutative
•  <list1> ( <list2> (R) ) =  <list1> (R) as long as <list2> contains the attributes in <list1>

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 71
EXAMPLE

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 72
RELATIONAL ALGREBRA EXPRESSIONS

• We may want to apply several relational algebra operations one after the other
• Either we can write the operations as a single relational algebra expression by nesting
the operations, or
• We can apply one operation at a time and create intermediate result relations.
• In the latter case, we must give names to the relations that hold the intermediate results.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 73
Single expression versus sequence of relational operations
(Example)
• To retrieve the first name, last name, and salary of all employees who work in
department number 5, we must apply a select and a project operation
• We can write a single relational algebra expression as follows:
• FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
• OR We can explicitly show the sequence of operations, giving a name to each
intermediate relation:
• DEP5_EMPS   DNO=5(EMPLOYEE)
• RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 74
Select id,name from person where Hobby=‘stamps’ OR
Hobby=‘coins’;

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 75
Unary Relational Operations: RENAME

• The RENAME operator is denoted by  (rho)


• In some cases, we may want to rename the attributes of a relation or the relation name or
both
• Useful when a query requires multiple operations
• Necessary in some cases like JOIN operation.

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 76
RENAME Operation

• The general RENAME operation  can be expressed by any of the following forms:
• S (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
• the column (attribute) names to B1, B1, …..Bn
• S(R) changes:
• the relation name only to S
• (B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1, …..Bn

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 77
• For convenience, we also use a shorthand for renaming attributes in
an intermediate relation:
• If we write:
• RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)
• RESULT will have the same attribute names as DEP5_EMPS (same
attributes as EMPLOYEE)
• If we write:
• RESULT (F, M, L)  FNAME, LNAME, SALARY (DEP5_EMPS)
• The 3 attributes of DEP5_EMPS are renamed to F, M, L respectively

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 78
Example Query
• Need to get firstname, last name and salary of employees working for department 5 and
rename attributes as first_name, last_name and salary
• RFNAME, LNAME, SALARY(DNO=5(EMPLOYEE))
• (first_name,last_name,salary) (R)
• As a shorthand
• RESULT(first_name,last_name,salary) (FNAME, LNAME, SALARY(DNO=5(EMPLOYEE)))

SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary


FROM EMPLOYEE AS E WHERE E.Dno=5

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 79
Example of applying multiple operations and RENAME

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 80
Relational Algebra Operations from Set Theory: UNION

• UNION Operation
• Binary operation, denoted by 
• The result of R  S, is a relation that includes all tuples that are either in R or
in S or in both R and S
• Duplicate tuples are eliminated
• The two operand relations R and S must be “type compatible” (or UNION
compatible)
• R and S must have same number of attributes
• Each pair of corresponding attributes must be type compatible (have same or
compatible domains)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 81
Example

• To retrieve the social security numbers of all employees who either work in
department 5 (RESULT1 below) or directly supervise an employee who works in
department 5 (RESULT2 below)
• We can use the UNION operation as follows:
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1   SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
• The union operation produces the tuples that are in either RESULT1 or RESULT2 or
both

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 82
Union Example

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 83
Relational Algebra Operations from
Set Theory
• Type Compatibility of operands is required for the binary set operation UNION ,
(also for INTERSECTION , and SET DIFFERENCE –, see next slides)
• R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type compatible if:
• they have the same number of attributes, and
• the domains of corresponding attributes are type compatible (i.e. dom(Ai)=dom(Bi)
for i=1, 2, ..., n).
• The resulting relation for R1R2 (also for R1R2, or R1–R2, see next slides) has
the same attribute names as the first operand relation R1 (by convention)

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 84
Relational Algebra Operations from Set Theory:
INTERSECTION
• INTERSECTION is denoted by 
• The result of the operation R  S, is a relation that includes all tuples that are in
both R and S
• The attribute names in the result will be the same as the attribute names in R
• The two operand relations R and S must be “type compatible”

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 85
Relational Algebra Operations from Set Theory: SET
DIFFERENCE (cont.)
• SET DIFFERENCE (also called MINUS or EXCEPT) is denoted by –
• The result of R – S, is a relation that includes all tuples that are in R but not in S
• The attribute names in the result will be the same as the attribute names in R
• The two operand relations R and S must be “type compatible”

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 86
Example to illustrate the result of UNION, INTERSECT,
and DIFFERENCE

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 87
Some properties of UNION, INTERSECT, and
DIFFERENCE
• Notice that both union and intersection are commutative operations; that is
• R  S = S  R, and R  S = S  R
• Both union and intersection can be treated as n-ary operations applicable to any
number of relations as both are associative operations; that is
• R  (S  T) = (R  S)  T
• (R  S)  T = R  (S  T)
• The minus operation is not commutative; that is, in general
• R–S≠S–R

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 88
Mcq
1. Relational Algebra is a __________ query language that takes two relations as input
and produces another relation as an output of the query.
a) Relational b) Structural c) Procedural d) Fundamental
2. Which of the following is used to denote the selection operation in relational algebra?
a) Pi (Greek) b) Sigma (Greek) c) Lambda (Greek) d) Omega (Greek)
3. For select operation the ________ appear in the subscript and the ___________
argument appears in the paranthesis after the sigma.
a) Predicates, relation
b) Relation, Predicates
c) Operation, Predicates
d) Relation, Operation

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 89
4. The ___________ operation, denoted by −, allows us to find tuples that are in one
relation but are not in another.
a) Union b) Set-difference c) Difference d) Intersection

5. The operation of a relation X, produces Y, such that Y contains only selected attributes of
X. Such an operation is :
A Projection B Intersection C Union D Difference

6. Which of the following is not valid unary operation in the relational algebra ?
A select B min C project D rename

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 90
Binary relation operation-Cartesian Product

• Cross Product(X): Cross product is used to join two relations


• For every row of Relation1, each row of Relation2 is concatenated.
• If Relation1 has m tuples and Relation2 has n tuples, cross product of Relation1 and
Relation2 will have m X n tuples.
• Cartesian product is also called cross join
Syntax:
Relation1 X Relation2

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 91
Example

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 92
Properties

• Number of columns of the Cartesian product is 7(4colums in S1 + 3 columns in R1)


• Number of Rows of the Cartesian product 6 (3 rows in S1 X 2 rows in R1 )

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 93
CROSS PRODUCT

• Generally, CROSS PRODUCT is not a meaningful operation


• Can become meaningful when followed by other operations
• Example (not meaningful):
• FEMALE_EMPS   SEX=’F’(EMPLOYEE)
• EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
• EMP_DEPENDENTS  EMPNAMES x DEPENDENT
• EMP_DEPENDENTS will contain every combination of EMPNAMES and
DEPENDENT
• whether or not they are actually related

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 94
Modify Query

• To keep only combinations where the DEPENDENT is related to the


EMPLOYEE, we add a SELECT operation as follows
• Relational Expression(more meaningful):
• FEMALE_EMPS   SEX=’F’(EMPLOYEE)
• EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
• EMP_DEPENDENTS  EMPNAMES x DEPENDENT
• ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)
• RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)
• RESULT will now contain the name of female employees and their dependents

Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 95
Approved by AICTE |Affiliated to VTU | Recognized by UGC with 2(f) & 12(B) status |Accredited by NBA and NAAC 96

You might also like