DBMS Unit2
DBMS Unit2
1 2
FROM Students
GROUP BY Status;
Views in SQL : A view is a virtual table in a database that represents the result of a
stored query. It does not store data itself but provides a way to simplify complex You cannot update this view since it involves aggregation (COUNT and GROUP BY).
queries, encapsulate query logic, and present data in a specific format or subset. Views
are created using the SELECT statement to display data from one or more tables.
How to Create a View: To create a view, use the CREATE VIEW statement followed
by the view name and the SELECT query that defines the data the view will display. SQL Indexes : An index is a database object that improves the speed of data retrieval
operations on a table. It is a data structure (often a B-tree or hash table) that stores a
CREATE VIEW ActiveStudents AS
sorted version of the values in one or more columns of a table, along with pointers to
SELECT StudentID, Name the corresponding rows. Indexes make searching, sorting, and querying data faster,
but they come with some trade-offs.
FROM Students
Advantages: Faster Queries: Speeds up data retrieval. Efficient
WHERE Status = 'Active';
Sorting/Searching: Improves performance for ORDER BY and GROUP BY. Better
This creates a view named ActiveStudents that shows only students with a status of Join Performance: Accelerates JOIN operations. Enforces Uniqueness: Supports
"Active." unique constraints.
Can You Update a View?: Yes, you can update a view, but with limitations. Disadvantages: More Storage: Requires additional disk space. Slower Data
Modifications: INSERT, UPDATE, and DELETE operations can be slower. Maintenance
1. Updatable Views: A view is updatable if it directly references columns from one
Needed: Requires regular upkeep. Not Always Effective: Can degrade performance
base table and does not contain any of the following:
if misused.
Aggregate functions (SUM, AVG, etc.)
Indexes are useful for speeding up queries but can have drawbacks like extra storage
DISTINCT keyword and slower data updates.
GROUP BY clause Stored Procedure: A stored procedure is a precompiled collection of one or more
SQL statements stored in the database. It can be executed as a single unit and is often
JOIN statements (in most cases)
used to perform repetitive tasks, complex operations, or encapsulate business logic.
Subqueries or derived columns Stored procedures can accept input parameters, return output parameters, and provide
better performance and security compared to executing individual SQL queries.
UPDATE ActiveStudents
Example of creating and calling a stored procedure:
SET Name = 'Jane Doe'
Creating a Stored Procedure: Create a procedure named GetEmployeeByID to fetch
WHERE StudentID = 1; employee details by EmployeeID:
This will update the Name column in the underlying Students table for StudentID 1. CREATE PROCEDURE GetEmployeeByID
@EmployeeID INT
3 4
AS
BEGIN Problem 2: Consider following schema Student_fee_details (rollno, name,
fee_deposited, date) Write a trigger to preserve old values of student fee details before
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
updating in the table.
END;
Answer: CREATE TABLE StudentFeeAudit (
Calling the Stored Procedure: Execute the stored procedure with an EmployeeID of
rollno INT,
101:
name VARCHAR(100),
EXEC GetEmployeeByID @EmployeeID = 101;
fee_deposited DECIMAL(10, 2),
Problem 1: Consider the following schemas
date DATE,
Emp(Emp_no, Emp_name, Dept_no)
old_fee_deposited DECIMAL(10, 2),
Dept(Dept_no, Dept_name)
old_date DATE,
Address(Dept_name, Dept_location)
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Write SQL queries for the following
);
i) Display the location of department where employee ‘Ram’ is working.
CREATE TRIGGER BeforeFeeUpdate
ii) Create a view to store total no of employees working in each
ON Student_fee_details
department in ascending order.
AFTER UPDATE
iii) Find the name of the department in which no employee is working.
AS
Answer:
BEGIN
i) SELECT A.Dept_location
INSERT INTO StudentFeeAudit (rollno, name, fee_deposited, date,
FROM Emp E
old_fee_deposited, old_date)
JOIN Dept D ON E.Dept_no = D.Dept_no
SELECT d.rollno, d.name, d.fee_deposited, d.date, i.fee_deposited, i.date
JOIN Address A ON D.Dept_name = A.Dept_name
FROM deleted d
WHERE E.Emp_name = 'Ram';
JOIN inserted i ON d.rollno = i.rollno;
ii) CREATE VIEW DeptEmployeeCount AS
END;
SELECT D.Dept_no, D.Dept_name, COUNT(E.Emp_no) AS
TotalEmployees
FROM Dept D
LEFT JOIN Emp E ON D.Dept_no = E.Dept_no
GROUP BY D.Dept_no, D.Dept_name
ORDER BY TotalEmployees ASC;
iii) SELECT D.Dept_name
FROM Dept D
LEFT JOIN Emp E ON D.Dept_no = E.Dept_no
Problem 3: Write a PL/SQL block of code which accepts the rollno from user. The
WHERE E.Emp_no IS NULL;
attendance of rollno entered by user will be checked instudent_attendance(RollNo,
Attendance) table and display on the screen.
Answer: DECLARE
5 6