DBMS Lab Manual
DBMS Lab Manual
LAB MANUAL
IV Semester
Prepared by
Course Code Course Title L T P S C
EXPERIENTIAL LEARNING
LIST OF EXERCISES:
1.Design and analyze an ER Model for the following use case.
The Library Management System database keeps track of readers with the
following considerations –
• The system keeps track of the staff with a single point
authentication system comprising login Id and password.
• Staff maintains the book catalog with its ISBN, Book title, price
(in INR), category (novel, general, story), edition, author Number and details.
• A publisher has publisher Id, Year when the book was published,
and name of the book.
• Readers are registered with their user_id, email, name (first
name, last name), Phone no (multiple entries allowed), communication
address. The staff keeps track of readers.
Choose appropriate Entities and attributes and relationships and draw the
complete ER diagram.
2.a) Implement Data Definition Language commands -Create, Alter, Drop,
Truncate and Rename.
b). Implement Data Manipulation Language commands - Insert, Select,
Update, and Delete.
c) Implement Single Row functions - Character, Numeric and Date functions.
Basic PL/SQL:
6. Construct PL/SQL block for the following:
a. To determine whether a number is palindrome
b. To determine whether a number is an Armstrong number
c. To find greatest of three numbers
d. To display Fibonacci series
Control Structures:
7.a) Write a program in PL/SQL to update the salary of a specific employee
by 20% if the salary less than 30000/- and 10% when the salary in between
30000/- and 60000/- and 5% when the salary is above 60000/- and display
the salary with a suitable message.
b) Write a PL/SQL program to display the description of the grade against a
student’s grade using CASE statement.
Exception Handling:
8. a) Develop a PL/SQL program that displays the name and address of a
student whose ID is given. If there is no student with the given student ID in
the database, the program should raise a run-time exception
NO_DATA_FOUND, which should be captured in the EXCEPTION block.
b). Construct the user-defined exceptions to get the salary of an employee
and check it with the job’s salary range. If the salary is below the range,
raise an exception BELOW_SALARY_RANGE. If the salary is above the range,
raise the exception ABOVE_SALARY_RANGE.
Functions:
9.a) Write a function that accepts two numbers A and B and performs the
following operations.
1. Addition
2. Subtraction
3. Multiplication
4. Division
Procedures:
10. a) Write a procedure that accepts two numbers and displays their sum.
b) Write procedures to demonstrate IN, IN OUT and OUT parameters.
Cursors:
11.a) Write a block in PL/SQL to create a Cursor that displays the employee
name and
number of jobs he or she has done in the past.
b) Write a program in PL/SQL to create a cursor to display the name and
salary of
each employee in the EMPLOYEES table whose salary is less than that
specified
by a passed-in parameter value.
Triggers:
12. Develop a suitable employee database application by considering
appropriate attributes.
a) Whenever the inserted or updated salary is more than 10 lacs per month a
trigger should be fired.
b) Whenever, the inserted or updated salary is less than 5000 per month a
trigger should be activated.
1. Design and analyze an ER Model for the following use case.
The Library Management System database keeps track of readers with the
following considerations –
• The system keeps track of the staff with a single point
authentication system comprising login Id and password.
• Staff maintains the book catalog with its ISBN, Book title, price
(in INR), category (novel, general, story), edition, author Number and details.
• A publisher has publisher Id, Year when the book was published,
and name of the book.
• Readers are registered with their user_id, email, name (first
name, last name), Phone no (multiple entries allowed), communication
address. The staff keeps track of readers.
Choose appropriate Entities and attributes and relationships and draw the
complete ER diagram.
Based on the use case, the following entities and their attributes are
identified:
1. Staff
o StaffID (Primary Key)
o Name
o LoginID (Unique)
o Password
2. Book
o Title
o Price
o Edition
3. Author
o Biography
4. Publisher
o Name
o YearPublished
5. Reader
o Email
o FirstName
o LastName
o CommunicationAddress
6. Catalog
o DateAdded
Relationships
o Relationship: "Tracks"
o Relationship: "Has"
ER Diagram
Create Table
CREATE TABLE Books (
ISBN VARCHAR(13) PRIMARY KEY,
Title VARCHAR(100),
Price DECIMAL(10, 2),
Category VARCHAR(20),
Edition INT
);
Alter Table
ALTER TABLE Books ADD AuthorID INT;
Drop Table
DROP TABLE Books;
Truncate Table
TRUNCATE TABLE Books;
Rename Table
RENAME TABLE Books TO LibraryBooks;
Select Data
SELECT * FROM Books WHERE Category = 'Education';
Update Data
UPDATE Books
SET Price = 550.00
WHERE ISBN = '9781234567890';
Delete Data
DELETE FROM Books WHERE ISBN = '9781234567890';
Numeric Functions
SELECT ROUND(Price, 1) AS RoundedPrice, CEIL(Price) AS CeilingPrice,
FLOOR(Price) AS FloorPrice
FROM Books;
Date Functions
SELECT CURRENT_DATE AS Today,
DATE_ADD(CURRENT_DATE, INTERVAL 7 DAY) AS OneWeekLater,
DATE_FORMAT(CURRENT_DATE, '%Y-%m-%d') AS FormattedDate;
3. Implement various types of integrity constraints - NOT NULL
constraint, DEFAULT constraint, UNIQUE constraint, PRIMARY key,
FOREIGN key, CHECK constraint.
Explanation of Constraints
1. NOT NULL
o Ensures that a column cannot have a NULL value.
o Example: Title must always have a value.
2. DEFAULT
o Provides a default value if no value is specified during insertion.
o Example: Category will default to 'General' if no value is
provided.
3. UNIQUE
o Ensures that all values in a column are unique.
o Example: Title must be unique for each row.
4. PRIMARY KEY
o Combines the properties of NOT NULL and UNIQUE. Each table
must have one primary key.
o Example: ISBN is a unique identifier for each book.
5. FOREIGN KEY
o Enforces a relationship between columns in two tables.
o Example: PublisherID in the Books table references the
PublisherID column in the Publishers table.
6. CHECK
o Ensures that all values in a column satisfy a specific condition.
o Example: Price must always be greater than 0.
o
4.a) Implement group functions with different operators such as
aggregate operators, group by, having and order by.
b) Implement nested and correlated nested queries using set
operators and set comparison operators.
2. Group By
Groups the data by a specific column to perform aggregate functions.
SELECT PublisherID, SUM(Price) AS TotalPrice
FROM Books
GROUP BY PublisherID;
3. Having
Filters groups based on aggregate results.
SELECT PublisherID, COUNT(*) AS TotalBooks
FROM Books
GROUP BY PublisherID
HAVING COUNT(*) > 5; -- Only publishers with more than 5 books
4. Order By
Orders the results of a query.
SELECT Category, COUNT(*) AS TotalBooks
FROM Books
GROUP BY Category
ORDER BY TotalBooks DESC; -- Orders by the total number of books in
descending order
Union Example:
SELECT Title FROM Books WHERE Category = 'Novel'
UNION
SELECT Title FROM Books WHERE Price > 500;
Intersect Example:
SELECT Title FROM Books WHERE Category = 'Story'
INTERSECT
SELECT Title FROM Books WHERE PublisherID = 1;
Except Example:
SELECT Title FROM Books WHERE Category = 'General'
EXCEPT
SELECT Title FROM Books WHERE Price < 300;
5. a) Creation of views, synonyms, sequence, indexes and save
point.
b) Implement various types of joins - outer join and inner join.
2. Create a Synonym
CREATE SYNONYM PubBooks FOR Books;
Use Case: Create an alternate name (PubBooks) for the Books table.
3. Create a Sequence
CREATE SEQUENCE BookSeq
START WITH 1
INCREMENT BY 1;
Use Case: A sequence to generate unique IDs for new books.
4. Create an Index
CREATE INDEX idx_book_category ON Books (Category);
Use Case: Create an index on the Category column to speed up searches.
5. Savepoint
BEGIN;
INSERT INTO Books (ISBN, Title, Price, Category, Edition, PublisherID)
VALUES ('9786666666666', 'SQL Guide', 400.00, 'Education', 2, 1);
SAVEPOINT BeforeDelete;
5. Cross Join
SELECT b.Title, p.Name AS PublisherName
FROM Books b
CROSS JOIN Publishers p;
Use Case: Create a Cartesian product of books and publishers. Each book is
paired with every publisher.
Basic PL/SQL:
6. Construct PL/SQL block for the following:
a. To determine whether a number is palindrome
b. To determine whether a number is an Armstrong number
c. To find greatest of three numbers
d. To display Fibonacci series
Example 1:
DECLARE
name varchar(10);
id number(5);
BEGIN
SELECT sname, sid
INTO name, id
FROM sailors
WHERE sid = 101;
DBMS_OUTPUT.PUT_LINE ('Name: '||name);
DBMS_OUTPUT.PUT_LINE ('ID: ' ||id);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
Example 2:
declare
name varchar(10);
age number(3);
begin
select sname, age
into name, age
from sailors
where sid=101;
dbms_output.put_line('Sailor Name: '||name||chr(9)||'Age: '||age);
exception
when no_data_found then
dbms_output.put_line('No such sailors id available in the database');
when others then
dbms_output.put_line('Error.....!');
end;
/
Example 3:
declare
x number(5):=&x;
y number(5):=&y;
div number(5);
exp1 exception;
exp2 exception;
begin
if y=0 then
raise exp1;
else if y>x then
raise exp2;
else
div:=x/y;
dbms_output.put_line('Result: '||div);
end if;
end if;
exception
when exp1 then
dbms_output.put_line('Error...!');
dbms_output.put_line('Divison by zero is not allowed');
when exp2 then
dbms_output.put_line('Error...!');
dbms_output.put_line('Y is greater than X, please check the values');
end;
/
Functions:
9.a) Write a function that accepts two numbers A and B and
performs the following operations.
1. Addition
2. Subtraction
3. Multiplication
4. Division
declare
a int;
b int;
c int;
procedure find_min(x in int, y in int, z out int)
is
begin
if x>y then z:=x;
else z:=y;
end if;
end;
begin
a:=&a;
b:=&b;
find_min(a,b,c);
dbms_output.put_line('Minimum of '||a||' and '||b||' is '||c);
end;
/
Cursors:
11.a) Write a block in PL/SQL to create a Cursor that displays the
employee’s name and
number of jobs he or she has done in the past.
b) Write a program in PL/SQL to create a cursor to display the name
and salary of
each employee in the EMPLOYEES table whose salary is less than
that specified
by a passed-in parameter value.
DECLARE
total number(2);
BEGIN
UPDATE empsal
SET esal = esal - 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total := sql%rowcount;
dbms_output.put_line( total || ' customers selected ');
END IF;
END;
/
declare
id empsal.eid%type;
total number;
begin
id:=&eid;
delete from empsal where eid=id;
if sql%found then
total:=sql%rowcount;
dbms_output.put_line(total||' record deleted');
else
dbms_output.put_line('Record not found');
end if;
commit;
end;
/
declare
cursor c1 is select sid,sname,age from sailors;
--id sailors.sid%type;
--name sailors.sname%type;
--age sailors.age%type;
vchar c1%rowtype;
begin
open c1;
dbms_output.put_line('ID'||chr(9)||'NAME'||chr(9)||'AGE');
dbms_output.put_line('--'||chr(9)||'----'||chr(9)||'---');
loop
fetch c1 into vchar;
exit when c1%notfound;
dbms_output.put_line(vchar.sid||chr(9)||vchar.sname||chr(9)||
vchar.age);
end loop;
close c1;
end;
/
Triggers:
12. Develop a suitable employee database application by
considering appropriate attributes.
a) Whenever the inserted or updated salary is more than 10 lacs per
month a trigger should be fired.