DATABASE MANAGEMENT SYSTEM
LAB MANUAL
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment no: - 9
Experiment Name: - SET operators and Views
Aim: - Performing practical by using SET operators and creating Views
Theory: -
• SET OPERATORS:
1. UNION / UNION ALL:
- UNION: Returns result from both queries after eliminating duplications.
e.g.: SELECT employee_id, job_id
FROM employees
UNION
SELECT employee_id, job_id
FROM job_history;
- UNION ALL: returns results from both queries, including all duplications.
e.g.: SELECT employee_id, job_id, department_id
FROM employees
UNION ALL
SELECT employee_id, job_id, department_id
FROM job_history
ORDER BY employee_id;
2. INTERSECT:
e.g.: SELECT employee_id, job_id
FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;
3. MINUS:
e.g.: SELECT employee_id, job_id
FROM employees
MINUS
SELECT employee_id, job_id
FROM job_history;
The Oracle Server and SET Operators:
- Duplicate rows are automatically eliminated except in UNION ALL.
- Column names from the first query appear in the result.
- The output is sorted in ascending order by default except in UNION ALL.
VIEWS:
- Logically represents subsets of data from one or more tables.
TERNA ENGG. COLLEGE, NERUL 1
DATABASE MANAGEMENT SYSTEM
Why it is used:
- To restrict data access.
- To make complex queries easy
- To represent different views of the same data.
Creating a View:
Syntax: CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias [, alias]…)]
AS subquery
[WITH CHECK OPTION
[CONSTRA
INT constraint]] [WITH READ ONLY
[CONSTRAINT constraint]];
e.g.: CREATE VIEW empvu80
AS SELECT employee_id,
last-name, salary FROM
employees
WHERE DEPARTMENT_ID=80;
Conclusion:
In this practical, learned how to use SET operators and Views
TERNA ENGG. COLLEGE, NERUL 2
DATABASE MANAGEMENT SYSTEM
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge
faculties at the end of the practical in case the there is no Black board access available)
Roll. No.: C-68 Name: Sandesh Bhagat
Class: SE-C Batch: C4
Date of Experiment: Date of Submission:
Grade:
B.1: Read the given question carefully and write a SQL statement and check the
output.
(Put the query and its output after each question.)
Tables: PROJECT-NETWORKING
ENO ENAME
101 KEVIN
102 SIDHARTH
103 UJJWAL
104 ROHIT
105 RAHUL
PROJECT-CADCAM
ENO ENAME
108 SANDEEP
102 SIDHARTH
106 PIYUSH
104 ROHIT
107 ANIKET
Create the above TWO tables
1. Use the Union Operator on the query output of the above two tables.
Display the Output:
TERNA ENGG. COLLEGE, NERUL 3
DATABASE MANAGEMENT SYSTEM
2. Use the Intersect Operator on the query output of the above two tables:
Display the Output:
3. Use the Minus Operator: on the query output of the above two tables
Display the Output:
TERNA ENGG. COLLEGE, NERUL 4
DATABASE MANAGEMENT SYSTEM
4. Using the EMPLOYEES and DEPARTMENTS tables, display the
department_id’s that do not contain the job_id ST_CLERK ( using
SET operators.)
TERNA ENGG. COLLEGE, NERUL 5
DATABASE MANAGEMENT SYSTEM
5. Modify the above query to display also the department_name in the output..
6. Create a view called EMPLOYEES_VU based on the employee id, last
names, and department ids from the EMPLOYEES table. Display the
contents of the EMPLOYEE_VU view.
B.2: Write an observation according to the query execution.
• View Creation:
TERNA ENGG. COLLEGE, NERUL 6
DATABASE MANAGEMENT SYSTEM
• The CREATE VIEW statement successfully creates a virtual table (EMPLOYEES_VU) that
contains the specified columns (employee_id, last_name, and department_id) from
the EMPLOYEES table.
• A view does not store data itself; it only stores the SQL query definition. Each time the
view is queried, the data is dynamically fetched from the underlying EMPLOYEES table.
• Querying the View:
• After creating the view, executing the SELECT * FROM EMPLOYEES_VU query retrieves
the data from the view, which in turn pulls the relevant columns (employee_id,
last_name, and department_id) from the EMPLOYEES table.
• The result should show the specific employee IDs, last names, and their corresponding
department IDs as per the data present in the EMPLOYEES table at the time the view is
queried.
• View Behaviour:
• The data displayed from the view will always be the latest data in the underlying
EMPLOYEES table because the view does not hold static data.
• The view serves as a convenient way to structure and simplify complex queries,
especially when frequently needing the same columns from a table without having to
repeatedly write the SELECT statement.
• Any updates to the underlying EMPLOYEES table (such as additions, deletions, or
modifications) will automatically reflect in the view when queried.
• No Data Modification in the View:
• The view EMPLOYEES_VU only displays data. It does not store data permanently or allow
direct modifications to the data. If updates or changes are needed, they should be made in
the EMPLOYEES table directly, and the changes will automatically be reflected in the view.
• Efficiency:
• Using a view simplifies SQL code, particularly when frequently querying the same
columns from a table. Instead of writing a SELECT query repeatedly, the view provides a
reusable structure.
• However, it should be noted that since views do not store data, querying the view will
still involve executing the underlying query every time it is called. This may affect
performance if the view's underlying query is complex or if the table contains a large
amount of data.
B.3: Write an answer of the following questions
Q1. Differentiate union and union all operation.
TERNA ENGG. COLLEGE, NERUL 7
DATABASE MANAGEMENT SYSTEM
Feature UNION UNION ALL
Duplicate rows Removed Included
Performance Slower Faster
Use Case When you need unique records When you need all records
Result Size Smaller Larger
TERNA ENGG. COLLEGE, NERUL 8