DBMS Lab Manual
DBMS Lab Manual
PART-A
1. Execute DDL Commands
Consider the table:
STUDENT (regno number, name varchar2, dob date, marks number)
a) Create the above table with suitable constraints.
b) Remove the existing attribute marks from the table.
c) Change the data type of regno from number to varchar2.
d) Add a new attribute phno to the existing table.
e) Insert 5 tuples into the table.
f) Display the tuples in table
Answer
a) CREATE TABLE STUDENT
(
regno NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
dob DATE,
marks NUMBER
);
Answer
a)
CREATE TABLE LIBRARY
(
bid NUMBER PRIMARY KEY,
title VARCHAR2(255),
au thor VARCHAR2(255),
publisher VARCHAR2(255),
year_of _pub NUMBER,
price NUMBER
);
b)
INSERT INTO LIBRARY VALUES (101, 'Sri RAMAYANA Darshanam', 'Sri KUVEMPU', 'IBH Prakashana',
1972, 880);
INSERT INTO LIBRARY VALUES (1 06, 'MacBeth', 'William Shakespear e', 'Dover Publica tions', 2000,
300);
INSERT INTO LIBRARY VALUES (1 07, 'Wings of fire', 'APJ Abdul Kalam', 'Universities Press', 2008,
450);
INSERT INTO LIBRARY VALUES (1 11, 'Naaku Tanti', 'Dr . Da. Ra. Bendre', 'Srimath a Prakashana',
1983, 789);
INSERT INTO LIBRARY VALUES (109, 'HAYAVADANA', 'Girish Karnad', 'OXFORD INDIA PERENNIALS',
2012, 299);
Answer:
a) CREATE TABLE EMPLOYEE
(
EmpNo NUMBER PRIMARY KEY,
EmpName VARCHAR2(100),
Dept VARCHAR2(50),
Salary NUMBER(10, 2),
DOJ DATE,
Branch VARCHAR2(100)
);
b)
INSERT INTO EMPLOYEE VALUES (101, 'Anita ' , 'HR' , 48000.00,' 15-06-2018','Mumbai' );
Dr.Chandrika G, HOD, SIGS Page 3
SEP DBMS LAB MANUAL
INSERT INTO EMPLOYEE VALUES (102, 'Rajesh ', 'Finance' , 55000.00,'10-04-2019' ,' Delhi' );
INSERT INTO EMPLOYEE VALUES (103, 'chaitra' , 'IT', 62000.00,' 10-04-2019','Pune' );
INSERT INTO EMPLOYEE VALUES (104, ' Thanush', 'Marketing' , 51000.00,' 22-05-2013','Hyderabad' );
INSERT INTO EMPLOYEE VALUES (105, 'Sara Ali', 'HR' , 47000.00,' 18-11-2022',' Bangalore' );
f) SELECT
EmpName,
SUM(Salary) AS To tsal,
COUNT(*) AS Namecount
FROM EMPLOYEE
GROUP BY EmpName;
TCL:
1. Create a table accounts ( account_id, account_holder_name, balance)
2. Insert some sample data into the accounts table.
3. Start a transaction and perform the following operations:
- Debit 1000 from account_id 1.
- Credit 1000 to account_id 2.
4. Use the COMMIT command to s1ave the changes.
5. Start a new transaction and perform the following operations:
- Debit 500 from account_id 1.
- Credit 500 to account_id 3.
6. Use the ROLLBACK command to undo the changes.
7. Create the save point and execute ROLLBACK TO previously created save point.
DCL:
1. Create a new user called user1 with a password.
2. Grant SELECT, INSERT, and UPDATE privileges on the accounts table to user1.
3. Login as user1 and perform the following operations:
- Select data from the accounts table.
- Insert a new row into the accounts table.
- Update an existing row in the accounts table.
4. Revoke the INSERT privilege from user1.
5. Login as user1 and try to insert a new row into the accounts table.
Answer:
TCL COMMANDS:
1)
CREATE TABLE accounts
(
account_id INT PRIMARY KEY,
account_holder_name VARCHAR(100),
balance DECIMAL(10, 2)
);
2)
INSERT INTO accounts VALUES (1, ' Ajay', 10000.00);
Dr.Chandrika G, HOD, SIGS Page 5
SEP DBMS LAB MANUAL
UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 2;
4) COMMIT;
5)
UPDATE accounts
SET balance = balance - 500
WHERE account_id = 1;
UPDATE accounts
SET balance = balance + 500
WHERE account_id = 3;
6) ROLLBACK TO;
7) COMMIT;
DCL COMMANDS:
Answer:
a)
CREATE TABLE ITEMS
(
itemcode NUMBER PRIMARY KEY,
name VARCHAR2(50),
price NUMBER
);
itemcode NUMBER,
qty NUMBER,
FOREIGN KEY (itemcode) REFERENCES ITEMS(itemcode)
);
b)
INSERT INTO ITEMS VALUES (101, 'Pen', 10);
INSERT INTO ITEMS VALUES (102, 'Notebook', 25);
INSERT INTO ITEMS VALUES (103, 'Pencil', 5);
INSERT INTO ITEMS VALUES (104, 'Eraser', 3);
INSERT INTO ITEMS VALUES (105, 'Marker', 15);
c)
SELECT I.itemcode, I.name, I.price, P.qty
FROM ITEMS I
JOIN PURCHASE P ON I.itemcode = P.itemcode;
d)
SELECT * FROM ITEMS
WHERE itemcode NOT IN
(
SELECT itemcode
FROM PURCHASE
);
Answer:
CREATE TA BL E EMP
(
SSN VARCHAR(9) PRIMA RY KEY,
Name VARCHAR(50),
Add ress VARCHAR(100),
Sex CHA R(1),
Salary DECI MAL(10,2),
Sup erSSN VARCHAR(9),
DNo INT,
FOREIGN KEY (DNo) REFERENCES DEPA RTMENT(DNo)
);
INSERT INTO EM P VALUES ('111111111' , ' Alice', 'Add r1' , ' F', 50000, NULL, 1);
INSERT INTO EM P VALUES ('222222222' , ' Bob' , 'Add r2' , ' M', 60000, ' 111111111', 2);
INSERT INTO EM P VALUES ('333333333' , ' Charli e', 'Add r3' , ' M', 55000, ' 222222222' , 1);
INSERT INTO EM P VALUES ('444444444' , ' Diana' , ' Add r4' , ' F', 70000, ' 111111111', 5);
INSERT INTO EM P VALUES ('555555555' , ' Et han' , ' Add r5' , ' M', 48000, '333333333' , 5);
a) UPDA TE EMP
SET Salary = Salary * 1.10
WHERE DNo = (SELECT DNo FROM DEPA RTMENT WHERE DName = 'Research');
Query the tables using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT
etc.
1. Display unique PNR_NO of all passengers
2. Display all the names of male passengers.
3. Display the ticket numbers and names of all the passengers.
4. Find the ticket numbers of the passengers whose name start with ‘r’ and ends with
‘h’.
5. Find the names of Passengers whose age is between 30 and 45.
6. Display all the passengers’ names beginning with ‘A’.
7. Display the sorted list of Passengers names
Answer:
INSERT INTO Passen ger 1 VALUES('P001', 'Rahu l', 35, ' M', 'Delhi');
INSERT INTO Passen ger 1 VALUES('P002', 'Anita', 28, 'F', 'Mumbai');
INSERT INTO Passen ger 1 VALUES('P003', 'Ramesh', 42, ' M', 'Kolkata');
INSERT INTO Passen ger 1 VALUES('P004', 'Amit', 33, ' M', 'Bangalor e');
INSERT INTO Passen ger 1 VALUES('P005', 'Resh', 40, 'F', 'Chenn ai');
5. SELECT Name FROM Passen ger 1 WHERE Age BETWEEN 30 AND 40;
6. SELECT Name FROM Passen ger 1 WHERE Name LIKE 'A%';
7. SELECT Name FROM Passen ger 1 ORDER BY Name;
a) Create view sleeper to display train no, start place, destination which have sleeper class
and perform the following
insert new record
update destination= ’Manglore ’ where train no=’RJD16’
delete a record which have train no=’KKE55’
b) Create view details to display train no, train name, class
c) Create view total_seats to display train number, start place, use count function to no
of seats, group by start place and perform the following
insert new record
update start place= ’Hubli’ where train no=’JNS8’
delete last row of the view
d) Rename view sleeper to class
e) Delete view details
Answer:
INSERT INTO TRA IN VALUES ('R16', 'Kon kan Expr ess', 'Pun e', 'Goa');
INSERT INTO TRA IN VALUES ('R17', 'sht habd i Expr ess', 'Hub li ', 'Bangalor e');
INSERT INTO TRA IN VALUES ('R18', 'vand e matharam Expr ess', 'varanasi', 'Ayod ya');
a)
CREATE VIEW sleeper AS
SELECT Tr ainNo, Star tPlace, Destinat ion
FROM AVA ILA BILITY
WHERE Class = 'Sleeper ';
UPDA TE sleeper
SET Destinat ion = ' Manglor e'
WHERE Tr ainNo = 'R16';
b)
CREATE VIEW det ails AS
SELECT T.TrainNo, T.TrainName, A.Class
FROM TRA IN T
JOIN AVA ILA BILITY A ON T.TrainNo = A.Tr ainNo;
c)
CREATE VIEW total_seat s AS
SELECT Tr ainNo, Star tPlace, COUNT(No_of_seats) AS seat_c oun t
FROM AVA ILA BILITY
GROUP BY Tr ainNo, Star tPlace;
d) INSERT INTO TRA IN VALUES ('RD19', 'Rajdh ani Expr ess', ' Mumbai', 'Pun e');
INSERT INTO AVA ILA BILITY VALUES ('RD19', 'Sleeper ', ' Mumbai', 'Pun e', 100);
PART-B
1) Write a PL/SQL program to enter any tw o nu mbers and find ou t their sum,
dif f erence, produ ct , quo t ient and remaind er.
DECLA RE
nu m1 NUMBER;
nu m2 NUMBER;
sum_val NUMBER;
diff NUMBER;
pr od NUMBER;
quo t NUMBER;
rem NUMBER;
BEGIN
-- Accept two nu mbers dynamicall y
nu m1 := &Ent er_Fir st _Number ;
nu m2 := &Ent er_Second _Number ;
IF nu m2 != 0 THEN
quo t := nu m1 / nu m2;
rem : = MOD(nu m1, nu m2);
ELSE
quo t := NULL;
rem : = NULL;
END IF;
-- Display result s
DBMS_OUTPUT.PUT_LINE('Sum: ' | | sum_val);
DBMS_OUTPUT.PUT_LINE('Differen ce: ' | | diff);
DBMS_OUTPUT.PUT_LINE('Pr odu ct : ' | | pr od);
IF nu m2 != 0 THEN
DBMS_OUTPUT.PUT_LINE('Quo tient: ' | | quo t);
DBMS_OUTPUT.PUT_LINE('Remaind er : ' | | rem);
ELSE
Dr.Chandrika G, HOD, SIGS Page 18
SEP DBMS LAB MANUAL
2) Write PL/SQL procedu re to compu t efact orial of a nu mber using recu rsi on .
DECLA RE
n NUMBER;
result NUMBER;
-- Recur sive fun ction to calc ulate factor ial
FUNCTION factor ial(x NUMBER) RETURN NUMBER IS
BEGIN
IF x = 0 THEN
RETURN 1;
ELSE
RETURN x * factor ial(x - 1);
END IF;
END;
BEGIN
-- Inpu t nu mber (run time inpu t)
n := &Ent er_Number ;
DECLARE
-- Define the cursor
CURSOR high_paid_emp_cursor IS
SELECT EmpNo, Name, Sal, Desig, Did
FROM (
SELECT * FROM EMPLOYEE1
ORDER BY Sal DESC
)
WHERE ROWNUM <= 5;
BEGIN
OPEN high_paid_emp_cursor;
LOOP
CLOSE high_paid_emp_cursor;
END;
/
OUTPUT:
EmpNo: 108, Name: Hannah Green, Salary: 112000, Designation: CEO, DeptID: 1
EmpNo: 104, Name: Dana Lee, Salary: 105000, Designation: CTO, DeptID: 10
EmpNo: 110, Name: Jane Blue, Salary: 97000, Designation: Lead Engineer, DeptID:30
EmpNo: 102, Name: Bob Johnson, Salary: 95000, Designation: Director, DeptID: 20
EmpNo: 106, Name: Fiona White, Salary: 91000, Designation: Architect, DeptID: 30
4) Write PL/SQL program to increase the salary for the employees of a particular
department and enter the no of records updated, date, time, deptno and name of the
person who increased the salary into another table called cursor_ret.
DECLARE
Dr.Chandrika G, HOD, SIGS Page 21
SEP DBMS LAB MANUAL
-- Input values
p_dno NUMBER := 10; -- Change as needed
p_percent_increase NUMBER := 10; -- e.g., 10% increase
p_uby VARCHAR2(100) := 'Admin User';
BEGIN
FOR emp_rec IN emp_cursor LOOP
-- Update salary
UPDATE EMPLOYEE1
SET Sal = Sal + (Sal * p_percent_increase / 100)
WHERE CURRENT OF emp_cursor;
vcount := vcount + 1;
END LOOP;
COMMIT;
OUTPUT:
5) Create a row level trigger for the customers table that would fire for INSERT or
UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger
will display the salary difference between the old & new Salary on UPDATE operation
and display the new or old salary for INSERT and DELETE operations, respectively.
CUSTOMERS(ID, NAME, AGE, ADDRESS, SALARY)
INSERT INTO CUSTOMERS VALUES (1, 'John Doe', 30, 'New York', 50000);
OUTPUT:
1 row inserted.
1 row updated.
1 row deleted.
6) Given the table MOVIE (MID, MTitle, Language, Director, Year) write a function in
INSERT INTO MOVIE VALUES (1, 'Inception', 'English', 'Christopher Nolan', 2010);
INSERT INTO MOVIE VALUES (2, 'Parasite', 'Korean', 'Bong Joon-ho', 2019);
INSERT INTO MOVIE VALUES (3, 'Spirited Away', 'Japanese', 'Hayao Miyazaki', 2001);
INSERT INTO MOVIE VALUES (4, 'The Godfather', 'English', 'Francis Ford Coppola',
1972);
INSERT INTO MOVIE VALUES (5, 'Amélie', 'French', 'Jean-Pierre Jeunet', 2001);
INSERT INTO MOVIE VALUES (6, 'Lagaan', 'Hindi', 'Ashutosh Gowariker', 2001);
INSERT INTO MOVIE VALUES (7, 'Roma', 'Spanish', 'Alfonso Cuarón', 2018);
RETURN total_movies;
END;
/
DECLARE
movie_count NUMBER;
BEGIN
movie_count := get_movies;
DBMS_OUTPUT.PUT_LINE('Total Movies: ' || movie_count);
END;
/
OUTPUT:
Total Movies: 7
GETMOVIES
7
7) Given the Table CUSTOMERS (CID, CName, Address) write a PL/SQL program
which asks for customer ID, if the user enters invalid ID then the exception invalid_id
is raised.
DECLARE
-- Variables
v_cid NUMBER := &Enter_Customer_ID; -- Prompts user for input
v_name CUSTOMER.CName%TYPE;
-- User-defined exception
invalid_id EXCEPTION;
-- Declare PRAGMA to associate the exception with no_data_found (optional but clearer)
PRAGMA EXCEPTION_INIT(invalid_id, -20001);
BEGIN
-- Try to fetch the customer name for the given ID
SELECT CName INTO v_name
FROM CUSTOMER
WHERE CID = v_cid;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Raise custom exception if no matching record is found
RAISE_APPLICATION_ERROR(-20001, 'Invalid customer ID: ' || v_cid);
END;
/
OUTPUT:
END emp_pkg;
/
OUTPUT: