DBMS Unit 2
Unit 2:-
7Q) Illustrate different set operations in Relational algebra with
an example?
A:
Relational algebra is a foundational concept in database management systems.
There are different set operations in relational algebra:
1. Union ( ): ∪
The union operation combines tuples from two relations, eliminating any
duplicate tuples, and produces a new relation containing all distinct tuples
from both relations.
Example:
Let's consider two relations:
Relation A: { (1, 'John'), (2, 'Alice'), (3, 'Bob') }
Relation B: { (2, 'Alice'), (4, 'Emma'), (5, 'Mike') }
The union of A and B (A ∪ B) will be:
{ (1, 'John'), (2, 'Alice'), (3, 'Bob'), (4, 'Emma'), (5, 'Mike') }
2. Intersection ( ∩ ):
The intersection operation returns a relation containing all tuples that are
present in both input relations.
Example:
Using the same relations as before:
Relation A: { (1, 'John'), (2, 'Alice'), (3, 'Bob') }
Relation B: { (2, 'Alice'), (4, 'Emma'), (5, 'Mike') }
The intersection of A and B (A ∩ B) will be:
{ (2, 'Alice') }
3. Difference ( \ ):
The difference operation returns a relation containing all tuples that are
present in the first relation but not in the second relation.
DBMS Unit 2 1
Example:
Again, using the same relations:
Relation A: { (1, 'John'), (2, 'Alice'), (3, 'Bob') }
Relation B: { (2, 'Alice'), (4, 'Emma'), (5, 'Mike') }
The difference of A and B (A \ B) will be:
{ (1, 'John'), (3, 'Bob') }
4. Cartesian Product ( × ):
The Cartesian product combines each tuple from the first relation with each
tuple from the second relation, resulting in a new relation.
Example:
Let's consider two relations:
Relation A: { (1, 'John'), (2, 'Alice') }
Relation B: { ('Red'), ('Blue') }
The Cartesian product of A and B (A × B) will be:
{ (1, 'John', 'Red'), (1, 'John', 'Blue'), (2, 'Alice', 'Red'), (2, 'Alice', 'Blue') }
5. Projection ( π ):
The projection operation selects specific attributes from a relation, creating
a new relation with only those attributes.
Example:
Consider a relation:
Relation Student: { (ID: 1, Name: 'John', Age: 20), (ID: 2, Name: 'Alice',
Age: 22) }
The projection of Student on attributes ID and Name (π<sub>ID,
Name</sub>(Student)) will be:
{ (ID: 1, Name: 'John'), (ID: 2, Name: 'Alice') }
6. Selection (σ):
The selection operation selects tuples from a relation that satisfy a given
condition.
Example:
Consider a relation representing students and their ages:
Relation Students: { (ID: 1, Name: 'John', Age: 20), (ID: 2, Name: 'Alice',
Age: 22), (ID: 3, Name: 'Bob', Age: 25) }
DBMS Unit 2 2
Now, let's say we want to select students who are older than 21 years old.
The selection of Students where Age > 21 (σ<sub>Age>21</sub>
(Students)) will be:
{ (ID: 2, Name: 'Alice', Age: 22), (ID: 3, Name: 'Bob', Age: 25) }
8Q) Explain various data manipulation and data definition
statements in SQL with examples.
A:
Structured Query Language (SQL), as we all know, is the database
language by the use of which we can perform certain operations on the
existing database, and also we can use this language to create a database.
These SQL commands are mainly categorized into five categories:
1. DDL – Data Definition Language
2. DQL – Data Query Language
3. DML – Data Manipulation Language
4. DCL – Data Control Language
5. TCL – Transaction Control Language
DML(Data Manipulation Language)
The SQL commands that deal with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes
most of the SQL statements.
a. SELECT: Retrieves data from one or more tables.
Example:
SELECT * FROM employees WHERE department = 'IT';
b. INSERT: Adds new rows of data into a table.
Example:
INSERT INTO employees (name, department, salary) VALUES ('
DBMS Unit 2 3
c. UPDATE: Modifies existing data in a table.
Example:
UPDATE employees SET salary = 55000 WHERE name = 'John Doe';
d. DELETE: Removes rows of data from a table.
Example:
DELETE FROM employees WHERE name = 'John Doe';
Data Definition Language (DDL):
Data Definition Language actually consists of the SQL commands that can
be used to define the database schema.
It simply deals with descriptions of the database schema and is used to
create and modify the structure of database objects in the database.
a. CREATE TABLE: Defines a new table.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10,2)
);
b. ALTER TABLE: Modifies an existing table structure.
Example:
ALTER TABLE employees ADD COLUMN email VARCHAR(100);
c. DROP TABLE: Deletes an existing table and all its data.
Example:
DBMS Unit 2 4
DROP TABLE employees;
d. CREATE INDEX: Creates an index on a table to improve query performance.
Example:
CREATE INDEX idx_department ON employees(department);
9Q)
a) Discuss about the operators SELECT,PROJECT,UNION?
b) Discuss about the operators renaming, Joins, Division, Set
difference.
a)
A:
1. Selection(σ): It is used to select required tuples of the relations.
Example:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C
1 2 4
4 3 4
Note: The selection operator only selects the required tuples but does not
display them. For display, the data projection operator is used.
2. Projection(π): It is used to project required column data from a relation.
Example: Consider Table 1. Suppose we want columns B and C from Relation R.
DBMS Unit 2 5
π(B,C)R will show following columns.
B C
2 4
2 3
3 4
Note: By Default, projection removes duplicate data.
3. Union(U): Union operation in relational algebra is the same as union
operation in set theory.
Example:
FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Consider the following table of Students having different optional subjects in
their course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
DBMS Unit 2 6
Vivek
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations
must have the same set of Attributes.
b)
A:
1. Rename(ρ):-
The RENAME operation is used to rename the output of a relation.
Notation:
ρ X (R)
where the symbol ‘ρ’ is used to denote the RENAME operator and R is the result
of the sequence of operation or expression which is saved with the name X.
Example-1: Query to rename the relation Student as Male Student and the
attributes of Student – RollNo, SName as (Sno, Name).
Sno Name
2600 Ronny
2655 Raja
ρ MaleStudent(Sno, Name) πRollNo, SName(σCondition(Studen
t))
2. Natural Join(⋈): Natural join is a binary operator. Natural join between two
or more relations will result in a set of all combinations of tuples where they
have an equal common attribute.
Example:
EMP
Name ID Dept_Name
DBMS Unit 2 7
A 120 IT
B 125 HR
C 110 Sales
D 111 IT
DEPT
Dept_Name Manager
Sales Y
Production Z
IT A
Natural join between EMP and DEPT with condition :
EMP.Dept_Name = DEPT.Dept_Name
EMP ⋈ DEPT
Name ID Dept_Name Manager
A 120 IT A
C 110 Sales Y
D 111 IT A
3. Division(÷):
Division operator A÷B or A/B can be applied if and only if:
Attributes of B is proper subset of Attributes of A.
The relation returned by division operator will have attributes = (All
attributes of A – All Attributes of B)
The relation returned by division operator will return those tuples from
relation A which are associated to every B’s tuple.
x y
a 1
b 2
a 2
d 4
DBMS Unit 2 8
B
The resultant of A/B is
4. Set Difference(-): Set Difference in relational algebra is the same set
difference operation as in set theory.
Example:
FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
DBMS Unit 2 9
Note: The only constraint in the Set Difference between two relations is that
both relations must have the same set of Attributes.
10Q)
Consider the following database schema to write queries in
SQL.
EMP(eno, ename, ecity)
COMPANY(cid, cname, ccity)
WORKS-FOR(eno, cid, salary)
MANAGES(eno, mno){Manager is also an employee}
a) Find the names of employees working for virtusa.
b) Find the company names situated in Hyderabad
c) List the employees working/reporting to Mohan .
A:
a) Find the names of employees working for Virtusa
SELECT ename
FROM EMP
INNER JOIN WORKS-FOR ON EMP.eno = WORKS-FOR.eno
INNER JOIN COMPANY ON WORKS-FOR.cid = COMPANY.cid
WHERE COMPANY.cname = 'Virtusa';
b) Find the company names situated in Hyderabad
SELECT cname
FROM COMPANY
WHERE ccity = 'Hyderabad';
c) List the employees working/reporting to Mohan
SELECT e2.ename
FROM MANAGES AS m
INNER JOIN EMP AS e1 ON m.mno = e1.eno
DBMS Unit 2 10
INNER JOIN EMP AS e2 ON m.eno = e2.eno
WHERE e1.ename = 'Mohan';
DBMS Unit 2 11