0% found this document useful (0 votes)
1 views

DBMS - Unit 2

The document outlines fundamental concepts of the relational model and relational algebra, including definitions of domains, attributes, tuples, relations, and relational schemas. It discusses constraints such as entity integrity and referential integrity, as well as operations for updating databases and handling constraint violations. Additionally, it introduces relational algebra operations like SELECT and PROJECT, which are used to manipulate and query relational data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

DBMS - Unit 2

The document outlines fundamental concepts of the relational model and relational algebra, including definitions of domains, attributes, tuples, relations, and relational schemas. It discusses constraints such as entity integrity and referential integrity, as well as operations for updating databases and handling constraint violations. Additionally, it introduces relational algebra operations like SELECT and PROJECT, which are used to manipulate and query relational data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

UNIT 2

RELATIONAL MODEL AND


RELATIONAL ALGEBRA
RELATIONAL MODEL CONCEPTS

Domain: A (usually named) set/universe of atomic values, where


by "atomic" we mean simply that, from the point of view of the
database, each value in the domain is indivisible (i.e., cannot be
broken down into component parts).
Examples of domains
SSN: string of digits of length nine
Name: string of characters beginning with an upper case letter

Attribute: the name of the role played by some value (coming


from some domain) in the context of a relational schema. The
domain of attribute A is denoted dom(A).

Tuple: A tuple is a mapping from attributes to values drawn from


the respective domains of those attributes. A tuple is intended to
describe some entity (or relationship between entities) in the mini
world.
As an example, a tuple for a PERSON entity might be
{Name  "John", Gender  Male, IQ  143 }
RELATIONAL MODEL CONCEPTS
Relation: A (named) set of tuples all of the same form (i.e., having the
same set of attributes). The term table is a loose synonym. (Some
database purists would argue that a table is "only" a physical
manifestation of a relation.)

Relational Schema: used for describing (the structure of) a relation.


E.g., R(A1, A2, ..., An) says that R is a relation with attributes A1, ... An. The
degree of a relation is the number of attributes it has, here n.

Example: STUDENT (Name, SSN, Address)

Relational Database: A collection of relations, each one consistent


with its specified relational schema.
Characteristics of Relations
Ordering of Tuples: A relation is a set of tuples; hence, there is no order
associated with them. That is, it makes no sense to refer to, for example,
the 5th tuple in a relation. When a relation is depicted as a table, the
tuples are necessarily listed in some order, of course, but you should
attach no significance to that order. Similarly, when tuples are
represented on a storage device, they must be organized in some
fashion, and it may be advantageous, from a performance standpoint, to
organize them in a way that depends upon their content.

Ordering of Attributes: A tuple is best viewed as a mapping from its


attributes (i.e., the names we give to the roles played by the values
comprising the tuple) to the corresponding values. Hence, the order in
which the attributes are listed in a table is irrelevant.

Values of Attributes: For a relation to be in First Normal Form, each of


its attribute domains must consist of atomic (neither composite nor multi-
valued) values. Much of the theory underlying the relational model was
based upon this assumption.

Interpretation of a Relation: Each relation can be viewed as a


predicate and each tuple in that relation can be viewed as an assertion
for which that predicate is satisfied (i.e., has value true) for the
Relational Model Notation
• R(A1, A2, ..., An) is a relational schema of degree n denoting that
there is a relation R having as its attributes A1, A2, ..., An.

• By convention, Q, R, and S denote relation names.

• By convention, q, r, and s denote relation states. For example,


r(R) denotes one possible state of relation R. If R is understood
from context, this could be written, more simply, as r.

• By convention, t, u, and v denote tuples.

• The "dot notation" R.A (e.g., STUDENT.Name) is used to qualify


an attribute name, usually for the purpose of distinguishing it
from a same-named attribute in a different relation
(e.g., DEPARTMENT.Name).
Relational Model Constraints and Relational Database Schemas

Constraints on databases can be categorized as follows:

Inherent model-based: Example: no two tuples in a relation can


be duplicates (because a relation is a set of tuples)

Schema-based: Can be expressed using DDL;

Application-based: Are specific to the "business rules" of the


mini world and typically difficult or impossible to express and
enforce within the data model. Hence, it is left to application
programs to enforce.

Elaborating upon schema-based constraints:

Domain Constraints: Each attribute value must be either null


(which is really a non-value) or drawn from the domain of that
attribute. Note that some DBMS's allow you to impose the not
null constraint upon an attribute, which is to say that that
attribute may not have the (non-)value null.
Relational Model Constraints and Relational Database Schemas

Key Constraints: A relation is a set of tuples, and each tuple's "identity"


is given by the values of its attributes. Hence, it makes no sense for two
tuples in a relation to be identical (because then the two tuples are
actually one and the same tuple). That is, no two tuples may have the
same combination of values in their attributes.

A key k of a relation schema R is a superkey of R with the additional


property that removing any attribute A from K leaves a set of attributes K
′ that is not a superkey of R any more. Hence, a key satisfies two
properties:
1. Two distinct tuples in any state of the relation cannot have identical
values for (all) the attributes in the key. This uniqueness property also
applies to a superkey.
2. It is a minimal superkey—that is, a superkey from which we cannot
remove any attributes and still have the uniqueness constraint hold. This
minimality property is required for a key but is optional for a superkey.

Candidate key: a relation schema may have more than one key. In this
case, each of the keys is called a candidate key. It is common to
designate one of the candidate keys as the primary key of the relation.

Primary key: a key chosen to act as the means by which to identify


Relational Database Schemas
A relational database schema S is a set of relation schemas S = {R1,
R2, … , Rm} and a set of integrity constraints (IC).

A relational database state DB of S is a set of relation states DB = {r 1,


r2, … , rm} such that each ri is a state of Ri and such that the ri relation
states satisfy the integrity constraints specified in IC.
OR
A relational database state/instance/snapshot is a set of states of
its relations such that no integrity constraint is violated.

Entity Integrity, Referential Integrity, and Foreign Keys:

The entity integrity constraint states that no primary key value can
be NULL. This is because the primary key value is used to identify
individual tuples in a relation. Having NULL values for the primary key
implies that we cannot identify some tuples.

The referential integrity constraint is specified between two relations


and is used to maintain the consistency among tuples in the two
relations. Informally, the referential integrity constraint states that a tuple
in one relation that refers to another relation must refer to an existing
Relational Database Schemas
Foreign key : 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. In the former case, we have t1[FK] = t2[PK],
and we say that the tuple t1 references or refers to the tuple
t2.

Semantic Integrity Constraints: application-specific


restrictions those are unlikely to be expressible in DDL. Examples:
• Salary of a supervisee cannot be greater than that of her/his
supervisor
• Salary of an employee cannot be lowered
Update Operations , Transactions, and Dealing with
Constraint Violations
For each of the update operations (Insert, Delete, and Update), we
consider what kinds of constraint violations may result from applying it
and how we might choose to react.

The Insert Operation: The Insert operation provides a list of attribute


values for a new tuple t that is to be inserted into a relation R. Insert can
violate any of the four types of constraints.
 Domain constraints can be violated if an attribute value is given that
does not appear in the corresponding domain or is not of the appropriate
data type.
 Key constraints can be violated if a key value in the new tuple t already
exists in another tuple in the relation r(R).
 Entity integrity can be violated if any part of the primary key of the
new tuple t is NULL.
 Referential integrity can be violated if the value of any foreign key in t
refers to a tuple that does not exist in the referenced relation.

Ways of dealing with it:


 If an insertion violates one or more constraints, the default option is to
reject the insertion.
Give user opportunity to try again with different attribute values.
Update Operations , Transactions, and Dealing with
Constraint Violations
The Delete Operation: The Delete operation can violate only
referential integrity constraint. This occurs if the tuple being
deleted is referenced by foreign keys from other tuples in the
database.

Ways of dealing with it: Several options are available if a


deletion operation causes a violation.
 The first option, called restrict, is to reject the deletion.
 The second option, called cascade, is to attempt to cascade
(or propagate) the deletion by deleting tuples that reference the
tuple that is being deleted.
 The third option, called set null or set default, is to modify
the referencing attribute values that cause the violation; each
such value is either set to NULL or changed to reference another
default valid tuple.
 Combinations of these three options are also possible.
Update Operations , Transactions, and Dealing with
Constraint Violations
The Update Operation: The Update (or Modify) operation is
used to change the values of one or more attributes in a tuple (or
tuples) of some relation R. It is necessary to specify a condition on
the attributes of the relation to select the tuple (or tuples) to be
modified.
 Key constraint violation: The primary key is changed so as to
become same as another tuple‘s key.
 Referential integrity violation:
o Foreign key is changed and new one refers to
nonexistent tuple.
o Primary key is changed and now other tuples that
had referred to this one violate the constraint.

Ways of dealing with it: Modifying a primary key value is


similar to deleting one tuple and inserting another in its place
because we use the primary key to identify tuples. Hence, the
Update Operations , Transactions, and Dealing with
Constraint Violations
The Transaction Concept:
 A transaction is an executing program (logical unit of work)
that includes some database operations, such as reading from the
database, or applying insertions, deletions, or updates to the
database. (such as what might be required to reserve several
seats on an airplane flight).
 At the end of the transaction, it must leave the database in a
valid or consistent state that satisfies all the constraints specified
on the database schema.
 This concept is relevant in the context where multiple users
and/or application programs are accessing and updating the
database concurrently.
 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. (Example of
simultaneous withdrawals from same checking account.)
Relational Algebra
• The basic set of operations for the formal relational model is the
relational algebra.
• The algebra operations produce new relations, which can be
further manipulated using operations of the same algebra.
•A sequence of relational algebra operations forms a relational
algebra expression, whose result will also be a relation that
represents the result of a database query.

Unary Relational Operations:


SELECT and PROJECT

The SELECT Operation: It is denoted by the symbol σ (sigma)


The SELECT operation is used to choose a subset of the
tuples from a relation that satisfies a selection condition. We
can consider the SELECT operation to be a filter that keeps only
those tuples that satisfy a qualifying condition.
The SELECT operation can also be visualized as a
horizontal partition of the relation into two sets of tuples—those
tuples that satisfy the condition and are selected, and those
tuples that do not satisfy the condition and are filtered out.
Relational Algebra - The SELECT Operation
For example:
1. To select the EMPLOYEE tuples whose department is 4
σDno=4(EMPLOYEE)
2. To select the EMPLOYEE tuples of those whose salary is greater
than $30,000.
σSalary>30000(EMPLOYEE)

The symbol σ (sigma) is used to denote the SELECT operator and


the selection
condition is a Boolean expression (condition) specified on the
attributes of
relation R. The Boolean expression specified in <selection
condition> is made up of a number of clauses of the form
<attribute name> <comparison op> <constant value>
or
<attribute name> <comparison op> <attribute name>
where <attribute name> is the name of an attribute of R,
<comparison op> is normally one of the operators {=, <, ≤, >,
≥, ≠}, and <constant value> is a constant value from the
attribute domain. Clauses can be connected by the standard
Relational Algebra - The SELECT Operation
For example, to select the tuples for all employees who either
work in department 4 and make over $25,000 per year, or work in
department 5 and make over $30,000, we can specify the
following SELECT operation:
σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)

The result is shown below

The SELECT operator is unary; that is, it is applied to a single


relation. Moreover, the selection operation is applied to each
tuple individually; hence, selection conditions cannot involve
more than one tuple. The degree of the relation resulting from
a SELECT operation—its number of attributes—is the same as
the degree of R. The number of tuples in the resulting relation
is always less than or equal to the number of tuples in R.
Relational Algebra - The SELECT Operation
The SELECT operation is commutative, that is,
σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))

In SQL, the SELECT condition is typically specified in


the WHERE clause of a query. For example, the
following operation:
σDno=4 AND Salary>25000 (EMPLOYEE)

would correspond to the following SQL query:

SELECT *
FROM EMPLOYEE
WHERE Dno=4 AND Salary>25000;
Relational Algebra - The PROJECT Operation
The PROJECT operation: It is denoted by the symbol π (Pi)
The Project operation selects certain columns from the
table and discards the other columns. If we are interested in only
certain attributes of a relation, we use the PROJECT operation to
project the relation over these attributes only.
Therefore, the result of the PROJECT operation can be
visualized as a vertical partition of the relation into two relations:
one has the needed columns attributes) and contains the result of
the operation, and the other contains the discarded columns.
For example, to list each employee’s first and last name and
salary, we can use the PROJECT operation as follows:
πLname, Fname, Salary(EMPLOYEE)
The result of the above relational algebra query is shown here.
Relational Algebra - The PROJECT Operation
The general form of the PROJECT operation is
π<attribute list>(R)
where π (pi) is the symbol used to represent the PROJECT
operation, and <attribute list> is the desired sub-list of attributes
from the attributes of relation R. Again, notice that R is, in
general, a relational algebra expression whose result is a relation,
which in the simplest case is just the name of a database relation.
The result of the PROJECT operation has only the attributes
specified in <attribute list> in the same order as they appear in
the list. Hence, its degree is equal to the number of attributes in
<attribute list>.
The PROJECT operation removes any duplicate tuples, so
the result of the PROJECT operation is a set of distinct tuples.
In SQL, the PROJECT attribute list is specified in the SELECT
clause of a query. For example, the following operation:
πAge, Salary(EMPLOYEE)
would correspond to the following SQL query:
SELECT DISTINCT Age, Salary
FROM EMPLOYEE;
Sequences of Operations and the RENAME Operation

Usually, for most queries, we need 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.

For 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, also known as
an in-line expression, as follows:
πFname, Lname, Salary(σDno=5(EMPLOYEE))
Sequences of Operations and the RENAME Operation

Alternatively, we can explicitly show the


sequence of operations, giving a name to each
intermediate relation, and using the assignment
operation, denoted by ← (left arrow), as follows:
DEPT5_EMPS ← σDno=5(EMPLOYEE)
RESULT ← πFname, Lname, Salary(DEPT5_EMPS)
It is sometimes simpler to break down a complex
sequence of operations by specifying intermediate
result relations than to write a single relational algebra
expression.
Sequences of Operations and the RENAME Operation

We can also use this technique to rename the attributes


in the intermediate and result relations. This can be useful in
connection with more complex operations such as UNION and
JOIN, as we shall see. To rename the attributes in a relation, we
simply list the new attribute names in parentheses, as in the
following example:
TEMP ← σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary) ← πFname, Lname, Salary(TEMP)

The result of the above 2 relational algebra queries is shown


below.
Sequences of Operations and the RENAME Operation

We can also define a formal RENAME operation—


which can rename either the relation name or the attribute
names, or both—as a unary operator. The general RENAME
operation when applied to a relation R of degree n is
denoted by any of the following three forms:
ρS(B1, B2, ... , Bn)(R)
or
ρS(R)
or
ρ(B1, B2, ... , Bn)(R)
where the symbol ρ (rho) is used to denote the RENAME
operator, S is the new relation name, and B1, B2, … , Bn are
the new attribute names. The first expression renames both
the relation and its attributes, the second renames the
relation only, and the third renames the attributes only. If
the attributes of R are (A1, A2, … , An) in that order, then
each Ai is renamed as Bi.
Relational Algebra Operations from Set
Theory
The
TheUNION, INTERSECTION,
next group andalgebra
of relational MINUS Operations
operations are
the standard mathematical operations on sets. We can
define the three operations UNION, INTERSECTION, and SET
DIFFERENCE on two union-compatible relations R and S as
follows:
■ UNION: The result of this operation, denoted by 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.
■ INTERSECTION: The result of this operation, denoted by R
∩ S, is a relation that includes all tuples that are in both R
and S.
■ SET DIFFERENCE (or MINUS): The result of this operation,
denoted by R – S, is a relation that includes all tuples that
are in R but not in S.
Relational Algebra Operations from Set
Theory
ForUNION,
The ∪, ∩, -,INTERSECTION,
the operand relations R(A1, Operations
and MINUS A2, ..., An) and
S(B1, B2, ..., Bn) must have the same number of attributes, and
the domains of corresponding attributes must be compatible; that
is, dom(Ai) = dom(Bi) for i=1, 2, ..., n. This condition is called
union compatibility. The resulting relation for ∪, ∩ or - has the
same attribute names as the first operand relation R (by
convention).
Relational Algebra Operations from Set
Theory
ItThe
is denoted by ×.
CARTESIAN This is also
PRODUCT a binary
/ CROSS set operation,
PRODUCT/ but
CROSS JOIN
the relations on which it Operation
is applied do not have to be union
compatible. In its binary form, this set operation produces a
new element by combining every member (tuple) from one
relation (set) with every member (tuple) from the other
relation (set). In general, the result of R(A1, A2, … , An) ×
S(B1, B2, … , Bm) is a relation Q with degree n + m
attributes Q(A1, A2, … , An, B1, B2, … , Bm), in that order.
The resulting relation Q has one tuple for each combination
of tuples—one from R and one from S.
Example: Combine each DEPARTMENT tuple with the
EMPLOYEE tuple of the manager.

DEP_EMP ← DEPARTMENT X EMPLOYEE

DEPT_MANAGER ← σ MGRSSN=SSN(DEP_EMP)
Binary Relational Operations:
JOIN and DIVISION
The JOIN Operation: The JOIN operation, denoted by , is used
to combine related tuples from two relations into single “longer”
tuples. This operation is very important for any relational
database with more than a single relation because it allows us to
process relationships among relations.
To illustrate JOIN, suppose that we want to retrieve the name of
the manager of each department. To get the manager’s name, we
need to combine each department tuple with the employee tuple
whose Ssn value matches the Mgr_ssn value in the department
tuple. We do this by using the JOIN operation and then projecting
the result over the necessary attributes, as follows:
DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE
RESULT ← πDname, Lname, Fname(DEPT_MGR)

The general form of a JOIN operation on two relations R(A1, A2,


… , An) and
S(B1, B2, … , Bm) is
R <join condition>S
Binary Relational Operations:
JOIN and DIVISION
The result of the JOIN is a relation Q with n + m attributes
Q(A1, A2, … , An, B1, B2,… , Bm) in that order; Q has one
tuple for each combination of tuples—one from R and one
from S—whenever the combination satisfies the join
condition.
This is the main difference between CARTESIAN PRODUCT
and JOIN. In JOIN, only combinations of tuples satisfying the
join condition appear in the result, whereas in the
CARTESIAN PRODUCT all combinations of tuples are
included in the result. The join condition is specified on
attributes from the two relations R and S and is evaluated
for each combination of tuples. Each tuple combination for
which the join condition evaluates to TRUE is included in the
resulting relation Q as a single combined tuple.

INNER JOIN : THETA JOIN, EQUI JOIN & NATURAL JOIN


Binary Relational Operations:
JOIN and DIVISION
INNER JOIN : THETA JOIN, EQUI JOIN & NATURAL JOIN

THETA JOIN: A general join condition is of the form

<condition> AND <condition> AND … AND


<condition>
where each <condition> is of the form Ai θ Bj, Ai is an
attribute of R, Bj is an attribute of S, Ai and Bj have the same
domain, and θ (theta) is one of the comparison operators
{=, <, ≤, >, ≥, ≠}. A JOIN operation with such a general
join condition is called a THETA JOIN.

EQUI JOIN: The most common use of JOIN involves join


conditions with equality comparisons only. Such a JOIN,
where the only comparison operator used is =, is called an
EQUI JOIN.
Binary Relational Operations: JOIN and
DIVISION
NATURAL JOIN: Denoted by *. NATURAL JOIN requires that
the two join attributes (or each pair of join attributes) have
the same name in both relations. If this is not the case, a
renaming operation is applied first.
Suppose we want to combine each PROJECT tuple with the
DEPARTMENT tuple that controls the project. In the following
example, first we rename the Dnumber attribute of
DEPARTMENT to Dnum—so that it has the same name as the
Dnum attribute in PROJECT—and then we apply NATURAL
JOIN:

PROJ_DEPT ← PROJECT * ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)


(DEPARTMENT)

The same query can be done in two steps by creating an


intermediate table DEPT as follows:

DEPT ← ρ (DEPARTMENT)
Binary Relational Operations:
JOIN and DIVISION
OUTER JOIN : LEFT OUTER JOIN, RIGHT OUTER JOIN &
FULL OUTER JOIN

Left Outer Join (A B): This returns all the rows from
the table on the left even if no matching rows have been
found in the table on the right. When no matching record
found in the table on the right, NULL is returned.

Right Outer Join ( A B ): This returns all the columns


from the table on the right even if no matching rows have
been found in the table on the left. Where no matches have
been found in the table on the left, NULL is returned.

Full Outer Join ( A B): In a FULL OUTER JOIN , all


tuples from both relations are included in the result,
irrespective of the matching condition.
Binary Relational Operations:
Summary of JOIN operations:
• There are mainly two types of joins in DBMS 1) Inner Join 2)
Outer Join
• An inner join is the widely used join operation and can be
considered as a default join-type.
• Inner Join is further divided into three subtypes: 1) Theta join 2)
Natural join 3) EQUI join
• Theta Join allows you to merge two tables based on the condition
represented by theta
• When a theta join uses only equivalence condition, it becomes
an equi join.
• Natural join does not utilize any of the comparison operators.
• An outer join doesn't require each record in the two join tables to
have a matching record.
• Outer Join is further divided into three subtypes are: 1)Left Outer
Join 2) Right Outer Join 3) Full Outer Join
• The LEFT Outer Join returns all the rows from the table on the
left, even if no matching rows have been found in the table on the
right.
• The RIGHT Outer Join returns all the columns from the table on
Binary Relational Operations:
The DIVISION Operation : denoted by ÷, is useful for a special
kind of query that
sometimes occurs in database applications.
An example is Retrieve the names of employees who work on all
the projects that ‘John Smith’ works on. To express this query
using the DIVISION operation, proceed as follows. First, retrieve
the list of project numbers that ‘John Smith’ works on in the
intermediate relation SMITH_PNOS:

Next, create a relation that includes a tuple <Pno, Essn>


whenever the employee whose Ssn is Essn works on the project
whose number is Pno in the intermediate relation SSN_PNOS:

Finally, apply the DIVISION operation to the two relations, which


gives the desired employees’ Social Security numbers:
Binary Relational Operations:
The result is shown below:
Binary Relational Operations:
In general, the DIVISION operation is applied to two
relations R(Z) ÷ S(X), where the attributes of S are a subset
of the attributes of R; that is, X ⊆ Z. For a tuple t to appear
in the result T of the DIVISION, the values in t must appear
in R in combination with every tuple in S. The DIVISION
operation is defined for convenience for dealing with
queries that involve universal quantification or the all
condition.
Aggregate Functions and Grouping :
We can define an AGGREGATE FUNCTION operation, using
the symbol ℑ (pronounced script F), to specify these types
of requests as follows:
<grouping attributes> ℑ <function list> (R)
where <grouping attributes> is a list of attributes of the
relation specified in R, and <function list> is a list of
(<function> <attribute>) pairs. In each such pair,
<function> is one of the allowed functions—such as SUM,
AVERAGE, MAXIMUM, MINIMUM, COUNT—and <attribute> is
an attribute of the relation specified by R. The resulting
relation has the grouping attributes plus one attribute for
each element in the function list. Eg:
1. ℑ COUNT Ssn, AVERAGE Salary(EMPLOYEE)

2. ρR(Dno, No_of_employees, Average_sal)(Dno ℑ COUNT Ssn, AVERAGE Salary


(EMPLOYEE)).
Examples of Queries in Relational Algebra
1. Retrieve the name and address of all employees who work for
the ‘Research’ department.

2. For every project located in ‘Stafford’, list the project number,


the controlling
department number, and the department manager’s last name,
address, and birth date.
Examples of Queries in Relational Algebra
3. Find the names of employees who work on all the projects
controlled
by department number 5.

4. Make a list of project numbers for projects that involve an


employee
whose last name is ‘Smith’, either as a worker or as a manager of
the department that controls the project.
Examples of Queries in Relational Algebra
5. List the names of all employees with two or more dependents.
Strictly speaking, this query cannot be done in the basic (original)
relational
algebra. We have to use the AGGREGATE FUNCTION operation
with the COUNT
aggregate function. We assume that dependents of the same
employee have
distinct Dependent_name values.

6. Retrieve the names of employees who have no dependents.


This is an example of the type of query that uses the MINUS (SET
DIFFERENCE)
operation.
Examples of Queries in Relational Algebra
7. List the names of managers who have at least one dependent.
In this query, we retrieve the Ssns of managers in MGRS, and the
Ssns of employees with at least one dependent in
EMPS_WITH_DEPS, then we apply the SET INTERSECTION
operation to get the Ssns of managers who have at least one
dependent.

You might also like