ANAND DBMSLR
ANAND DBMSLR
LAB RECORD
22-382–0207- DBMS LAB
DBMS PROGRAMMING
LABORATORY RECORD
Certified that this is a bonafide record of work done by Mr. ANAND KUMAR in
the DBMS Programming Laboratory of the Department of Computer Applications,
Cochin University of Science and Technology.
SI No Title Page
1 Create a table using SQL DDL commands with integrity constraints 4
and modify schema using ALTER and DROP.
2 Modify table by inserting, deleting, and updating records using SQL 5
DML commands.
3 Create a Bank table and perform various queries including selection, 6
updates, and deletions.
4 Perform branch-wise sorting, add a new column, update values, 8
count records, and remove specific accounts.
5 Create CLIENT_MASTER and SALE_ORDER tables, insert values, 11
and perform queries on client details and orders.
6 Create supplier, parts, and shipment tables, insert values, and run 14
queries on supplier and shipment details.
7 Implement student-class-faculty database and execute queries on 18
enrollment and faculty records.
8 Create Product and Vendor tables, insert values, and perform queries 22
related to vendor-product relationships.
9 Implement Library Database schema and perform retrieval, deletion, 24
partitioning, and view creation.
10 Implement Company Database schema and execute queries related 29
to employees and departments.
11 Implement control structures like IF-THEN, IF-THEN-ELSE, 33
CASE, WHILE using PL/SQL.
12 Implement a cursor to fetch the top 5 highest-paid employees and 36
insert them into another table.
13 Create Student and Student_Grade tables, use a cursor to calculate 38
total, percentage, and assign grades.
14 Implement a procedure for a bank transaction using stored 41
procedures.
15 Implement a function for a bank transaction using stored functions. 43
16 Implement a trigger for loan generation when balance goes below 45
zero.
17 Create a TRIGGER to ensure student total_marks is not less than 47
zero.
18 Create a TRIGGER to delete rows if quantity entered is zero in the 49
supplier table.
19 Write a stored procedure to check if a number is an Armstrong 51
number.
PROGRAM-1
Question:
Create a table using SQL DDL commands. Apply integrity constraints (primary key, foreign key , not
null) and change the existing schema definition using ALTER and DROP.
Query:
CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(20) NOT NULL
);
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(20) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);
ALTER TABLE Employee ADD salary DECIMAL(10,2);
DROP TABLE Employee;
4
PROGRAM-2
Question:
Modify the table by inserting, deleting, and updating records using SQL DML commands.
Query:
CREATE TABLE Department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(20) NOT NULL
);
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(20) NOT NULL,
dept_id INT,
5
PROGRAM-3
Question:
Create a table Bank with fields (Acc_no integer primary key, Acc_name varchar(20), branch_name
varchar(20), Acc_type varchar(10), amount decimal(10,2)). Insert at least 5 records.
1. Display “Savings Account” details in Kochi branch.
2. Change “Trivandrum” to “Thiruvananthapuram”.
3. Display details of customers in Thiruvananthapuram, Kochi, Palakkad.
4. List customers in Thrissur with min balance Rs5000.
5. Delete all current accounts in Mahe branch.
Query:
CREATE TABLE Bank (
Acc_no INT PRIMARY KEY,
Acc_name VARCHAR(20),
branch_name VARCHAR(20),
Acc_type VARCHAR(10),
amount DECIMAL(10,2)
);
INSERT INTO Bank VALUES
(1, 'Alice', 'Kochi', 'Savings', 10000.00),
(2, 'Bob', 'Trivandrum', 'Current', 5000.00),
(3, 'Charlie', 'Thrissur', 'Savings', 7000.00),
(4, 'David', 'Mahe', 'Current', 3000.00),
(5, 'Eve', 'Palakkad', 'Savings', 12000.00);
SELECT * FROM Bank WHERE Acc_type = 'Savings' AND branch_name = 'Kochi';
UPDATE Bank SET branch_name = 'Thiruvananthapuram' WHERE branch_name = 'Trivandrum';
SELECT * FROM Bank WHERE branch_name IN ('Thiruvananthapuram', 'Kochi', 'Palakkad');
SELECT * FROM Bank WHERE branch_name = 'Thrissur' AND amount >= 5000;
DELETE FROM Bank WHERE branch_name = 'Mahe' AND Acc_type = 'Current';
6
Output:
1.
2. Branch Updated
3.
4.
7
PROGRAM-4
Question:
Use a Bank table and write SQL statements for the following.
1.Display the branch wise details of account holders in the ascending order of the amount.
2.Insert a new column named Minimum_amount into the table with default value 1000.
3.Update the Minimum_amount column with the value 1000 for the customers in branches other than
Alappuzha and Malappuram.
4.Find the number of customers who do not have the minimum amount 1000.
5.Remove the details of SB accounts from Thiruvananthapuram branch who have zero (0) balance in
their account.
Query:
CREATE TABLE Bank (
Account_No VARCHAR(10) PRIMARY KEY,
Account_Holder_Name VARCHAR(50),
Branch VARCHAR(50),
Amount DECIMAL(10, 2)
);
INSERT INTO Bank VALUES
('SB001', 'John', 'Thiruvananthapuram', 5000),
('SB002', 'Alice', 'Alappuzha', 1500),
('SB003', 'Bob', 'Malappuram', 800),
('SB004', 'Charlie', 'Thiruvananthapuram', 0),
('SB005', 'David', 'Kochi', 2000),
('SB006', 'Eve', 'Alappuzha', 300);
1. SELECT Branch, Account_No, Account_Holder_Name, Amount
FROM Bank
ORDER BY Branch, Amount ASC;
2. ALTER TABLE Bank
ADD Minimum_amount DECIMAL(10, 2) DEFAULT 1000;
3. UPDATE Bank
SET Minimum_amount = 1000
WHERE Branch NOT IN ('Alappuzha', 'Malappuram');
4. SELECT COUNT(*) AS Low_Balance_Customers
FROM Bank
8
WHERE Amount < 1000;
5. DELETE FROM Bank
WHERE Branch = 'Thiruvananthapuram'
AND Amount = 0
AND Account_No LIKE 'SB%';
Output:
1.
2.
3. Since Minimum_amount was already 1000 for all rows due to the default value, no visible change
occurs here.
4.
Low_Balance_Customers
9
5.
10
PROGRAM-5
Question:
Create table CLIENT_MASTER with attributes Client_No as primary key, Name, City, Pincode,
and Bal_due.
Create table SALE_ORDER with attributes Order_No, Order_Date, Client_No, Order_Status,
and Dely_Date.
Insert values into tables.
Write SQL queries to:
1. List all details from the CLIENT_MASTER table for clients whose Bal_due = 0.
2. Update table CLIENT_MASTER, change city of Client_No C00004 to Jaipur.
3. Retrieve records of clients residing in Mumbai.
4. Find the Name and Address of customer who has placed Order_No ‘O19003’ and ‘O19002’
respectively.
5. List the Client_No, Name, City, and Pincode of clients whose Order_Status is “In
process”.
Query:
11
);
INSERT INTO CLIENT_MASTER VALUES
('C00001', 'Amit', 'Mumbai', '400001', 0),
('C00002', 'Priya', 'Delhi', '110001', 5000),
('C00003', 'Rahul', 'Mumbai', '400002', 0),
('C00004', 'Suman', 'Chennai', '600001', 2000);
1. SELECT *
FROM CLIENT_MASTER
WHERE Bal_due = 0;
2. UPDATE CLIENT_MASTER
SET City = 'Jaipur'
WHERE Client_No = 'C00004';
3. SELECT *
FROM CLIENT_MASTER
WHERE City = 'Mumbai';
4. SELECT CM.Name, CM.City, CM.Pincode
FROM CLIENT_MASTER CM
JOIN SALE_ORDER SO ON CM.Client_No = SO.Client_No
WHERE SO.Order_No IN ('O19003', 'O19002');
5. SELECT CM.Client_No, CM.Name, CM.City, CM.Pincode
FROM CLIENT_MASTER CM
JOIN SALE_ORDER SO ON CM.Client_No = SO.Client_No
WHERE SO.Order_Status = 'In process';
Output:
1.
12
2.
3.
4.
5.
13
PROGRAM-6
Question:
Create table supplier with attributes supplier number as primary key, supplier name and city.
Create a table parts with attributes partno as primary key, partname, color, weight and city.
Create a table shipment with attributes sno as references supplier number of supplier table, pno
references partnumber of parts table, quantity, sno and pno as primary key.
Insert values into 3 tables.
Write SQL queries to
1. Change the city of suppliers whose sno is S1 to Hyderabad.
2. Update the quantity of all parts in the shipment table to quantity +10.
3. Get supplier name for all suppliers who supply part p1.
4. Get supplier number for suppliers who are located in same city as sno=S1.
5. Get supplier number for suppliers who supply at least one part supplied by sno=S2.
6. Get Sno’s for suppliers who do not supply any part supplied by sno=S2.
Query:
CREATE TABLE Supplier (
14
pno VARCHAR(10),
quantity INT,
PRIMARY KEY (sno, pno),
FOREIGN KEY (sno) REFERENCES Supplier(sno),
FOREIGN KEY (pno) REFERENCES Parts(partno)
);
INSERT INTO Supplier (sno, supplier_name, city) VALUES
('S1', 'Supplier A', 'Mumbai'), ('S2', 'Supplier B', 'Delhi'),
('S3', 'Supplier C', 'Bangalore'), ('S4', 'Supplier D', 'Chennai');
('P3', 'Screw', 'Green', 4.0, 'Chennai'), ('P4', 'Washer', 'Yellow', 2.5, 'Bangalore');
15
Output:
1.
S1 Supplier A Hyderabad
S2 Supplier B Delhi
S3 Supplier C Bangalore
S4 Supplier D Chennai
2.
S1 P1 110
S1 P2 160
S2 P2 210
S2 P3 130
S3 P4 190
S4 P1 100
3.
supplier_name
Supplier A
Supplier D
4.
sno
S1
16
5.
sno
S1
6.
sno
S3
S4
17
PROGRAM-7
Question:
Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string,
meets at: time, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer,
fname: string, deptid: integer) The meaning of these relations is straightforward; for example,
Enrolled has one record per student-class pair such that the student is enrolled in the class. Write the
following queries in SQL.
1.Find the names of all Juniors (Level = JR) who are enrolled in a class taught by I. Teach.
2.Find the age of the oldest student who is either a History major or is enrolled in a course taught by I.
Teach.
3.Find the names of all classes that either meet in room R128 or have five or more students enrolled.
4.Find the names of all students who are enrolled in two classes that meet at the same time.
5.Find the names of faculty members who teach in every room in which some class is taught.
Query:
CREATE TABLE Student (
sid VARCHAR(5) PRIMARY KEY,
sname VARCHAR(50),
age INT,
major VARCHAR(20),
level VARCHAR(5)
);
CREATE TABLE Faculty (
fid VARCHAR(5) PRIMARY KEY,
fname VARCHAR(50)
);
CREATE TABLE Class (
cname VARCHAR(10) PRIMARY KEY,
room VARCHAR(10),
fid VARCHAR(5) REFERENCES Faculty(fid),
time_slot VARCHAR(10)
);
CREATE TABLE Enrolled (
sid VARCHAR(5) REFERENCES Student(sid),
cname VARCHAR(10) REFERENCES Class(cname),
18
PRIMARY KEY (sid, cname)
);
INSERT INTO Student (sid, sname, age, major, level) VALUES
('S1', 'Alice Smith', 20, 'History', 'JR'),
('S2', 'Bob Jones', 22, 'Math', 'SR'),
('S3', 'Charlie Brown', 19, 'History', 'JR'),
('S4', 'Diana Lee', 21, 'Physics', 'JR'),
('S5', 'Eve White', 23, 'History', 'SR');
19
SELECT c.cname
FROM Class c
JOIN Enrolled e ON c.cname = e.cname
GROUP BY c.cname
HAVING COUNT(e.sid) >= 5;
4. SELECT DISTINCT s.sname
FROM Student s
JOIN Enrolled e1 ON s.sid = e1.sid
JOIN Enrolled e2 ON s.sid = e2.sid
JOIN Class c1 ON e1.cname = c1.cname
JOIN Class c2 ON e2.cname = c2.cname
WHERE c1.cname != c2.cname
AND c1.time_slot = c2.time_slot;
5. SELECT f.fname
FROM Faculty f
WHERE NOT EXISTS (
SELECT room
FROM Class c1
WHERE NOT EXISTS (
SELECT *
FROM Class c2
WHERE c2.fid = f.fid
AND c2.room = c1.room ));
Output:
1.
sname
Alice Smith
Charlie Brown
Diana Lee
2.
MAX(s.age)
23
3.
cname
C1
C3
20
4.
sname
Alice Smith
Diana Lee
5.
fname
(none)
21
PROGRAM-8
Question:
Consider the following relations Product (P_code, Description, Stocking_date, QtyOnHand, MinQty,
Prices, Discount, V_code) . Vendor (V_code, Name, Address, Phone). Here a vendor can supply more
than one product but a product is supplied by only one vendor. (NOTE: Identify the primary keys and
foreign key from this statement) Write SQL queries for the following:
1.List the names of all the vendors who supply more than one product.
2.List the details of the products whose prices exceed the average product price.
3.Create a view that contains Name, Address and Phone of the vendors who are currently not
supplying any product.
Query:
CREATE TABLE Vendor (
V_code INT PRIMARY KEY,
Name VARCHAR(100),
Address VARCHAR(255),
Phone VARCHAR(20)
);
22
INSERT INTO Product (P_code, Description, Stocking_date, QtyOnHand, MinQty, Prices, Discount,
V_code) VALUES
(101, 'Product X', '2024-03-01', 50, 10, 200.00, 5.00, 1),
(102, 'Product Y', '2024-03-02', 30, 5, 150.00, 10.00, 2),
(103, 'Product Z', '2024-03-03', 20, 5, 300.00, 8.00, 1);
1. SELECT V.Name
FROM Vendor V
JOIN Product P ON V.V_code = P.V_code
GROUP BY V.V_code, V.Name
HAVING COUNT(P.P_code) > 1;
2. SELECT *
FROM Product
WHERE Prices > (SELECT AVG(Prices) FROM Product);
3. CREATE VIEW InactiveVendors AS
SELECT V.Name, V.Address, V.Phone
FROM Vendor V
LEFT JOIN Product P ON V.V_code = P.V_code
WHERE P.P_code IS NULL;
SELECT * FROM InactiveVendors;
Output:
1.
Name
Vendor A
2.
3.
23
PROGRAM-9
Question:
Consider the following schema for a Library Database: BOOK(Book_id, Title, Publisher_Name,
Pub_Year) BOOK_AUTHORS(Book_id, Author_Name) PUBLISHER(Name, Address, Phone)
BOOK_COPIES(Book_id, Programme_id, No-of_Copies) BOOK_LENDING(Book_id,
Programme_id, Card_No, Date_Out, Due_Date) LIBRARY_PROGRAMME(Programme_id,
Programme_Name, Address) Write SQL queries to
1.Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in
each Programme, etc.
2.Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun
2017.
3.Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.
4.Partition the BOOK table based on year of publication.
5.Create a view of all books and its number of copies that are currently available in the Library.
Query:
CREATE TABLE BOOK (
Book_id INT PRIMARY KEY,
Title VARCHAR(255),
Publisher_Name VARCHAR(255),
Pub_Year INT
);
24
CREATE TABLE BOOK_COPIES (
Book_id INT,
Programme_id INT,
No_of_Copies INT,
PRIMARY KEY (Book_id, Programme_id),
FOREIGN KEY (Book_id) REFERENCES BOOK(Book_id) ON DELETE CASCADE
);
CREATE TABLE BOOK_LENDING (
Book_id INT,
Programme_id INT,
Card_No INT,
Date_Out DATE,
Due_Date DATE,
FOREIGN KEY (Book_id) REFERENCES BOOK(Book_id) ON DELETE CASCADE
);
CREATE TABLE LIBRARY_PROGRAMME (
Programme_id INT PRIMARY KEY,
Programme_Name VARCHAR(255),
Address VARCHAR(255)
);
25
INSERT INTO BOOK_AUTHORS (Book_id, Author_Name) VALUES
(101, 'Navathe'), (101, 'Korth'), (102, 'Stuart Russell'),
(103, 'Hadley Wickham'), (104, 'Guido van Rossum');
26
4. CREATE TABLE BOOK_PARTITIONED (
Book_id INT PRIMARY KEY,
Title VARCHAR(255),
Publisher_Name VARCHAR(255),
Pub_Year INT
) PARTITION BY RANGE (Pub_Year) (
PARTITION p1 VALUES LESS THAN (2000),
PARTITION p2 VALUES LESS THAN (2010),
PARTITION p3 VALUES LESS THAN (2020),
PARTITION p4 VALUES LESS THAN MAXVALUE
);
5. CREATE VIEW Available_Books AS
SELECT B.Book_id, B.Title, SUM(C.No_of_Copies) AS Total_Available_Copies
FROM BOOK B
JOIN BOOK_COPIES C ON B.Book_id = C.Book_id
LEFT JOIN BOOK_LENDING L ON B.Book_id = L.Book_id
GROUP BY B.Book_id, B.Title;
SELECT * FROM Available_Books;
Output:
1.
DB
Navat
MS
101 Pearson 2015 he, 1 Engineering 5
Basic
Korth
s
AI Stuart
102 for O'Reilly 2018 Russel 2 CS Department 3
All l
2.
Card_No Books_Borrowed
5001 4
27
Book_id Title Total_Available_Copies
28
PROGRAM-10
Question:
Consider the Company database Employee(emp_no,emp_name, age, address, doj, mobile_no,
dept_no,salary). Department (dept_no, dept_name, location, budjet). Write SQL queries to
1.Display records from Employee table whose age is between 25 and 45.
2.Display the details of all employees whose names staring with letter C
3.Display the details of all employees whose department is in the location Kochi in the decreasing
order of their salary
4.Display the name of department whose budjet is less than the total salary of all employees in that
department
5.Create a view containing the empno empname mobile no, deptnodeptname in the alphabetical order
of the department name.
Query:
CREATE TABLE Employee (
emp_no INT PRIMARY KEY,
emp_name VARCHAR(255),
age INT,
address VARCHAR(255),
doj DATE,
mobile_no VARCHAR(15),
dept_no INT,
salary DECIMAL(10,2),
FOREIGN KEY (dept_no) REFERENCES Department(dept_no) ON DELETE CASCADE
);
29
INSERT INTO Department (dept_no, dept_name, location, budget) VALUES
(1, 'HR', 'Mumbai', 500000),
(2, 'IT', 'Kochi', 1000000),
(3, 'Finance', 'Bangalore', 750000),
(4, 'Marketing', 'Kochi', 1200000);
INSERT INTO Employee (emp_no, emp_name, age, address, doj, mobile_no, dept_no, salary)
VALUES
(101, 'Alice', 30, 'Mumbai', '2015-03-15', '9876543210', 1, 60000),
(102, 'Bob', 40, 'Bangalore', '2018-06-20', '9876543221', 3, 75000),
(103, 'Charlie', 35, 'Kochi', '2020-01-10', '9876543232', 2, 90000),
(104, 'David', 50, 'Kochi', '2012-07-23', '9876543243', 4, 110000),
(105, 'Catherine', 28, 'Delhi', '2019-09-05', '9876543254', 1, 50000),
(106, 'Chris', 32, 'Kochi', '2021-04-15', '9876543265', 2, 95000),
(107, 'Eve', 45, 'Mumbai', '2016-11-30', '9876543276', 3, 80000);
30
Output:
1.
2.
3.
4.
dept_name
IT
5.
31
emp_no emp_name mobile_no dept_no dept_name
32
PROGRAM-11
Question:
Implementation of various control structures like IF-THEN,IF-THEN-ELSE, ,IF-THEN-ELSIF,
CASE, WHILE using PL/SQL
Query:
1. IF-THEN Statement
DECLARE
salary NUMBER := 50000;
BEGIN
IF salary > 40000 THEN
DBMS_OUTPUT.PUT_LINE('High salary');
END IF;
END;
2. IF-THEN-ELSE Statement
DECLARE
salary NUMBER := 30000;
BEGIN
IF salary > 40000 THEN
DBMS_OUTPUT.PUT_LINE('High salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low salary');
END IF;
END;
3. IF-THEN-ELSIF Statement
DECLARE
salary NUMBER := 50000;
BEGIN
IF salary > 70000 THEN
DBMS_OUTPUT.PUT_LINE('Very High salary');
ELSIF salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('High salary');
ELSIF salary > 30000 THEN
DBMS_OUTPUT.PUT_LINE('Medium salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low salary');
END IF;
END;
4. CASE Statement
DECLARE
grade CHAR := 'B';
BEGIN
CASE grade
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');
33
WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Good');
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Average');
ELSE DBMS_OUTPUT.PUT_LINE('Poor');
END CASE;
END;
5. WHILE Loop
DECLARE
counter NUMBER := 1;
BEGIN
WHILE counter <= 5 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || counter);
counter := counter + 1;
END LOOP;
END;
6. FOR Loop
BEGIN
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || i);
END LOOP;
END;
Output:
1. High salary
2. Low salary
3. Medium salary
4. Good
5. Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
6. Iteration: 1
Iteration: 2
Iteration: 3
34
Iteration: 4
Iteration: 5
7. Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
35
PROGRAM-12
Question:
Implementation of cursor
1.Create a table employee with empno, empname, empsal.
2.Insert values into table employee
3.Create another table temp with salary, eno and ename.
4.Write a program using cursor to find the first 5 highest paid employee and insert into temp.
Query:
1. CREATE TABLE Employee (
empno INT PRIMARY KEY,
empname VARCHAR(255),
empsal DECIMAL(10,2)
);
2. INSERT INTO Employee (empno, empname, empsal) VALUES
(101, 'Alice', 75000), (102, 'Bob', 90000), (103, 'Charlie', 85000),
(104, 'David', 95000), (105, 'Eve', 70000), (106, 'Frank', 65000),
(107, 'Grace', 80000), (108, 'Hank', 88000), (109, 'Ivy', 92000),
(110, 'Jack', 98000);
3. CREATE TABLE Temp (
salary DECIMAL(10,2),
eno INT,
ename VARCHAR(255)
);
4. DELIMITER $$
36
OPEN empcursor;
DELIMITER ;
Output:
37
PROGRAM-13
Question:
Create a table Student(Rollno, Name, Sub1, Sub2, Sub3).
1.Insert values into the table.
2.Create another table Student_grade with Rollno, Total, percentage, grade.
3.Create a cursor to calculate total and percentage of students. Then find grade of students as given
below:
< 40% FAIL
40 - 49.99% C
50 - 59.99% B
60 - 79.99% A
>= 80% HONORS
and insert into Student_grade.
Query:
1. CREATE TABLE Student (
Rollno INT PRIMARY KEY,
Name VARCHAR(255),
Sub1 INT,
Sub2 INT,
Sub3 INT
);
2. INSERT INTO Student (Rollno, Name, Sub1, Sub2, Sub3) VALUES
(101, 'Alice', 85, 90, 80),
(102, 'Bob', 40, 50, 60),
(103, 'Charlie', 30, 20, 25),
(104, 'David', 55, 65, 70),
(105, 'Eve', 75, 80, 70);
3. CREATE TABLE Student_grade (
Rollno INT PRIMARY KEY,
Total INT,
Percentage DECIMAL(5,2),
Grade VARCHAR(10)
);
4. DELIMITER $$
38
DECLARE v_sub1 INT;
DECLARE v_sub2 INT;
DECLARE v_sub3 INT;
DECLARE v_total INT;
DECLARE v_percentage DECIMAL(5,2);
DECLARE v_grade VARCHAR(10);
DECLARE done INT DEFAULT 0;
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_rollno, v_sub1, v_sub2, v_sub3;
IF done = 1 THEN
LEAVE read_loop;
END IF;
39
SET v_grade = 'C';
ELSEIF v_percentage >= 50 AND v_percentage < 60 THEN
SET v_grade = 'B';
ELSEIF v_percentage >= 60 AND v_percentage < 80 THEN
SET v_grade = 'A';
ELSE
SET v_grade = 'HONORS';
END IF;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
Output:
40
PROGRAM-14
Question:
Procedure for bank transaction:
1. Create a table with attributes id, nm, account number and balance.
2.Write a program for bank transaction using procedure.
Query:
1. CREATE TABLE Bank (
id INT PRIMARY KEY,
nm VARCHAR(255),
account_number VARCHAR(20) UNIQUE,
balance DECIMAL(10,2)
);
2. INSERT INTO Bank (id, nm, account_number, balance) VALUES
(1, 'Alice', 'ACC1001', 5000.00),
(2, 'Bob', 'ACC1002', 3000.00),
(3, 'Charlie', 'ACC1003', 10000.00);
3. DELIMITER $$
41
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: Insufficient balance!';
END IF;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: Invalid transaction type!';
END IF;
END $$
DELIMITER;
4. - -Deposit Example
CALL BankTransaction('ACC1001', 'DEPOSIT', 2000.00);
Output:
1. --Withdraw Example
CALL BankTransaction('ACC1002', 'WITHDRAW', 1000.00);
id nm account_number balance
42
PROGRAM-15
Question:
Function for bank transaction:
1.Create a table customer attributes id, num, accno , bal.
2.Write a program for bank transaction using function.
Query:
1. CREATE TABLE Customer (
id INT PRIMARY KEY,
num VARCHAR(255),
accno VARCHAR(20) UNIQUE,
bal DECIMAL(10,2)
);
2. INSERT INTO Customer (id, num, accno, bal) VALUES
(1, 'Alice', 'ACC1001', 5000.00),
(2, 'Bob', 'ACC1002', 3000.00),
(3, 'Charlie', 'ACC1003', 10000.00);
3. DELIMITER $$
43
ELSE
SET message = 'Error: Invalid transaction type!';
END IF;
END IF;
RETURN message;
END $$
DELIMITER ;
Output:
1. Deposit Example
SELECT BankTransaction('ACC1001', 'DEPOSIT', 2000.00);
2. Withdraw Example
SELECT BankTransaction('ACC1002', 'WITHDRAW', 1000.00);
44
PROGRAM-16
Question:
Trigger for loan generation:
1.Create a table account with attributes customerid, name and balance.
2.Create another loan with attributes cusid and amount.
3.Insert values into account table.
4.Create a trigger to check if the balance has gone below 0, if so, make necessary conditions
Query:
CREATE TABLE Account (
customerid INT PRIMARY KEY,
name VARCHAR(255),
balance DECIMAL(10,2)
);
CREATE TABLE Loan (
cusid INT,
amount DECIMAL(10,2),
FOREIGN KEY (cusid) REFERENCES Account(customerid)
);
INSERT INTO Account (customerid, name, balance) VALUES
(1, 'Alice', 5000.00),
(2, 'Bob', 3000.00),
(3, 'Charlie', 10000.00);
DELIMITER $$
45
VALUES (NEW.customerid, ABS(NEW.balance));
END IF;
END $$
DELIMITER;
Output:
1. Withdraw Money Leading to Negative Balance
UPDATE Account SET balance = balance - 6000 WHERE customerid = 1;
SELECT * FROM Loan;
cusid amount
1 1000.00
cusid amount
1 1000.00
2 2000.00
46
PROGRAM-17
Question:
Create a table STUDENT with attributes student_ number, student_ name and total_ marks.
1.Insert values into the table.
2.Create a TRIGGER to check whether the total_ marks is less than zero if so, then set it as zero.
Query:
CREATE TABLE STUDENT (
student_number INT PRIMARY KEY,
student_name VARCHAR(255),
total_marks INT
);
1. INSERT INTO STUDENT (student_number, student_name, total_marks) VALUES
(1, 'Alice', 85), (2, 'Bob', 92), (3, 'Charlie', 78);
2. DELIMITER $$
DELIMITER;
Output:
1. Insert a Student with Negative Marks
INSERT INTO STUDENT (student_number, student_name, total_marks) VALUES
(4, 'David', -20);
1 Alice 85
47
student_number student_name total_marks
2 Bob 92
3 Charlie 78
4 David 0
48
PROGRAM-18
Question:
Create a table supplier with attributes supplier_ name, quantity, item_ name.
1.Enter values into the table.
2.Create a TRIGGER to check whether the quantity entered is zero if so, then delete the entire row.
Query:
CREATE TABLE SUPPLIER (
supplier_name VARCHAR(255),
quantity INT,
item_name VARCHAR(255),
PRIMARY KEY (supplier_name, item_name)
);
2. DELIMITER $$
DELIMITER;
Output:
1. Insert a Supplier with Zero Quantity
INSERT INTO SUPPLIER (supplier_name, quantity, item_name) VALUES
('DEF Corp', 0, 'Salt');
49
supplier_name quantity item_name
50
PROGRAM-19
Question:
Write a program to check whether a given number is Armstrong or not using the concept of stored
procedure in MySQL.
Query:
DELIMITER $$
51
END WHILE;
DELIMITER;
Output:
52