Diya_lab
Diya_lab
CYCLE-1
ID character 5
DeptID numeric 2
Name character 15
Designation character 15
Basic numeric 10,2
Gender character 1
user@user-Desktop:~$ sudomysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Owners.
3. Display the ID, name, designation and basic salary of all the employees.
5. Display ID, name, designation,deptID and basic, DA, HRA and net salary
of all employees with suitable headings as DA, HRA and NET_SAL
respectively.(DA is 7.5% of basic, and NET_SAL is Basic + DA+ HRA)
+-----+---------+-------------+--------+---------+-----------+----------+------------+
| ID | Name | Designation | DeptID | Basic | DA | HRA | NET_SAL |
+-----+---------+-------------+--------+---------+-----------+----------+------------+
| 101 | Ram | Typist | 1 | 2000.00 | 150.00000 | 200.0000 | 2350.00000 |
| 102 | Arun | Analyst | 2 | 6000.00 | 450.00000 | 600.0000 | 7050.00000 |
| 114 | Menon | Clerk | 4 | 1500.00 | 112.50000 | 150.0000 | 1762.50000 |
| 115 | Tim | Clerk | 4 | 1500.00 | 112.50000 | 150.0000 | 1762.50000 |
| 121 | Ruby | Typist | 1 | 2010.00 | 150.75000 | 201.0000 | 2361.75000 |
| 123 | Mridula | Analyst | 2 | 6000.00 | 450.00000 | 600.0000 | 7050.00000 |
| 127 | Kiran | Manager | 2 | 4000.00 | 300.00000 | 400.0000 | 4700.00000 |
| 156 | Mary | Manager | 3 | 4500.00 | 337.50000 | 450.0000 | 5287.50000 |
+-----+---------+-------------+--------+---------+-----------+----------+------------+
8 rows in set (0.01 sec)
6. Display ID, name, desig, deptID and basic salary in the descending order
of basic pay.
mysql> select ID, Name, Designation, DeptID, Basic from Emp26 order by
Basic desc;
+-----+---------+-------------+--------+---------+
| ID | Name | Designation | DeptID | Basic |
+-----+---------+-------------+--------+---------+
| 102 | Arun | Analyst | 2 | 6000.00 |
| 123 | Mridula | Analyst | 2 | 6000.00 |
| 156 | Mary | Manager | 3 | 4500.00 |
| 127 | Kiran | Manager | 2 | 4000.00 |
| 156 | Maya | Clerk | 2 | 4000.00 |
| 121 | Ruby | Typist | 1 | 2010.00 |
| 101 | Ram | Typist | 1 | 2000.00 |
| 114 | Menon | Clerk | 4 | 1500.00 |
| 115 | Tim | Clerk | 4 | 1500.00 |
+-----+---------+-------------+--------+---------+
8 rows in set (0.00 sec)
7
+-------------+
| Designation |
+-------------+
| Typist |
| Analyst |
| Clerk |
| Manager |
+-------------+
4 rows in set (0.00 sec)
10. Display the ID, name, department and basic of all the employees who are
either MANAGER or CLERK and the basic salary is in the range of 1400 and
4500.
mysql> select ID, Name, DeptID, Basic from Emp26 where Designation in
('Manager', 'Clerk') and Basic between 1400 and 4500;
8
+-----+-------+--------+---------+
| ID | Name | DeptID | Basic |
+-----+-------+--------+---------+
| 114 | Menon | 4 | 1500.00 |
| 115 | Tim | 4 | 1500.00 |
| 127 | Kiran | 2 | 4000.00 |
| 156 | Mary | 3 | 4500.00 |
+-----+-------+--------+---------+
4 rows in set (0.00 sec)
14. Update values of manager id of employees as null for 101, 101 for 102,
121, 156. 102 for 123,114,115.121 for 127.
+---------------+--------------+
| Employee_Name | Manager_Name |
+---------------+--------------+
| Ram | NULL |
| Arun | Ram |
| Menon | Arun |
| Tim | Arun |
| Ruby | Ram |
| Mridula | Arun |
| Kiran | Ruby |
| Mary | Ram |
+---------------+--------------+
8 rows in set (0.00 sec)
+-------------+
| Designation |
+-------------+
| Typist |
| Analyst |
| Clerk |
| Manager |
+-------------+
4 rows in set (0.00 sec)
mysql> select * from Emp26 where Designation = 'Typist' and Gender = 'F';
+-----+--------+------+-------------+---------+--------+------------+
| ID | DeptID | Name | Designation | Basic | Gender | Manager_ID |
+-----+--------+------+-------------+---------+--------+------------+
| 121 | 1 | Ruby | Typist | 2010.00 | F | 101 |
+-----+--------+------+-------------+---------+--------+------------+
1 row in set (0.00 sec)
25. Display the male clerks getting salary more than 3000.
mysql> select * from Emp26 where Designation = 'Clerk' and Gender = 'M' and
Basic > 3000;
+-----+--------+------+-------------+---------+--------+------------+
12
+-----+--------+---------+-------------+---------+--------+------------+
| ID | DeptID | Name | Designation | Basic | Gender | Manager_ID |
+-----+--------+---------+-------------+---------+--------+------------+
| 102 | 2 | Arun | Analyst | 6000.00 | M | 101 |
| 123 | 2 | Mridula | Analyst | 6000.00 | F | 102 |
| 127 | 2 | Kiran | Manager | 4000.00 | M | 121 |
+-----+--------+---------+-------------+---------+--------+------------+
3 rows in set (0.00 sec)
27. Display the designation and salary of Ruby. 28. Add a column joining
date to the above table.
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| ID | DeptID | Name | Designation | Basic | Gender | Manager_ID | Joining_Date |
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| 101 | 1 | Ram | Typist | 2000.00 | M | NULL | 2022-01-10 |
| 102 | 2 | Arun | Analyst | 6000.00 | M | 101 | 2022-02-15 |
| 114 | 4 | Menon | Clerk | 1500.00 | M | 102 | 2022-03-05 |
| 115 | 4 | Tim | Clerk | 1500.00 | M | 102 | 2022-03-20 |
| 121 | 1 | Ruby | Typist | 2010.00 | F | 101 | 2022-04-10 |
| 123 | 2 | Mridula | Analyst | 6000.00 | F | 102 | 2022-05-25 |
| 127 | 2 | Kiran | Manager | 4000.00 | M | 121 | 2022-06-01 |
| 147 | 2 | Amal | Clerk | 4000.00 | M | 101 | 2022-07-15 |
| 156 | 3 | Mary | Manager | 4500.00 | F | 101 | 2022-08-10 |
+-----+--------+---------+-------------+---------+--------+------------+------------+
9 rows in set (0.00 sec)
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| ID | DeptID | Name | Designation | Basic | Gender | Manager_ID | Joining_Date |
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| 102 | 2 | Arun | Analyst | 6000.00 | M | 101 | 2022-02-15 |
| 123 | 2 | Mridula | Analyst | 6000.00 | F | 102 | 2022-05-25 |
| 156 | 3 | Mary | Manager | 4500.00 | F | 101 | 2022-08-10 |
| 127 | 2 | Kiran | Manager | 4000.00 | M | 121 | 2022-06-01 |
| 147 | 2 | Amal | Clerk | 4000.00 | M | 101 | 2022-07-15 |
| 121 | 1 | Ruby | Typist | 2010.00 | F | 101 | 2022-04-10 |
| 101 | 1 | Ram | Typist | 2000.00 | M | NULL | 2022-01-10 |
| 114 | 4 | Menon | Clerk | 1500.00 | M | 102 | 2022-03-05 |
| 115 | 4 | Tim | Clerk | 1500.00 | M | 102 | 2022-03-20 |
+-----+--------+---------+-------------+---------+--------+------------+--------------+
9 rows in set (0.00 sec)
32. Create a new table DEPARTMENT with fields DEPTID and DNAME. Make DEPTID
as the
primary key.
34. Insert values into the DEPARTMENT table. Make sure that all the
existing values for DEPTID in emp is inserted into this table. Sample
values are DESIGN,CODING,TESTING,RESEARCH.
mysql> insert into Dept26 (DeptID, DName) values ('1', 'DESIGN'), ('2',
'CODING'), ('3', 'TESTING'), ('4', 'RESEARCH');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
15
+-----+-------+-------------+---------+--------+
16
+-----+-------+-------------+---------+--------+-----------------+
| ID | Name | Designation | Basic | Gender | Department_Name |
+-----+-------+-------------+---------+--------+-----------------+
| 101 | Ram | Typist | 2000.00 | M | DESIGN |
| 121 | Ruby | Typist | 2010.00 | F | DESIGN |
| 156 | Mary | Manager | 4500.00 | F | TESTING |
+-----+-------+-------------+---------+--------+-----------------+
3 rows in set (0.00 sec)
46. Display the names of employees getting salary greater than the average
salary of their department.
47. Display the names of employees working under the manager Ram.
+--------+--------------------------+
19
49. Display the deptid and minimum salary as "Lowest Salary" for those
departments with minimum salary above 2500.
+--------+---------------+
| DeptID | Lowest Salary |
+--------+---------------+
| 2 | 3000.00 |
+--------+---------------+
1 row in set (0.00 sec)
50. Display the names of employees whose salary is the maximum given by
their department.
51. Display the names of the employees, if their salary is greater than the
salary of some other Employees.
+-------+
20
| Name |
+-------+
| Arun |
| Ruby |
| Kiran |
| Mary |
+-------+
4 rows in set (0.00 sec)
52. Display the names of the employees, if their salary is greater than the
salary of some other employees or less than the salary of some other
employees.
55. Find the names of employees who are from the same city as their
company.
| Name |
+-------+
| Tim |
| Kiran |
+-------+
2 rows in set (0.00 sec)
56. Display the names of the departments giving smallest total salary.
+-----------+
| Name |
+-----------+
| Ram |
| Arun |
| Ruby |
| Kiran |
+-----------+
4 rows in set (0.00 sec)
58. Display the names of employees joined during the month of August.
+----------+
22
| Name |
+----------+
| Menon |
| Tim |
+----------+
2 rows in set (0.00 sec)
59. Display the details of departments not having any employees (take the
help of exists clause to do this)
mysql> select *
-> from Dept26 d
-> where not exists (
-> select 1
-> from Emp26 e
-> where e.DeptID = d.DeptID
);
+--------+------------+-------------+
| DeptID | DName | City |
+--------+------------+-------------+
| 4 | TESTING | NULL |
+--------+------------+-------------+
1 row in set (0.00 sec)
mysql> select *
-> from Emp26
-> where Basic > 5000;
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| ID | DeptID | Name | Designation | Basic | Gender | Manager_ID | Joining_Date |
+-----+--------+---------+-------------+---------+--------+------------+--------------+
| 102 | 2 | Arun | Analyst | 6000.00 | M | 101 | 2022-02-15 |
| 123 | 2 | Mridula | Analyst | 6000.00 | F | 102 | 2022-05-25 |
+-----+--------+---------+-------------+---------+--------+------------+--------------+
2 rows in set (0.09 sec)
62. Insert the details of some employees who are not assigned with a
department.(did is null);
23
mysql> insert into Emp26 (ID, DeptID, Name, Designation, Basic, Gender,
Manager_ID, Joining_Date)
-> values (201, NULL, 'John', 'Typist', 2500.00, 'M', NULL, '2024-09-
10'),(202, NULL, 'Sarah', 'Clerk', 2700.00, 'F', NULL, '2024-09-15');
Query OK, 2 rows affected (0.01 sec)
63. Display the names of employees and their department ids. If an employee
is not assigned with a department, display his name with department id as
“null”.
64. Display the names of employees and their department ids. If an employee
is not assigned with a department, display his name with department id as
0.
Result:
CYCLE- 2
BOOK TABLE
26
EXERCISES
BOOK Table:
Book_Id (Primary Key, int)
Title (varchar)
Language_Id (Foreign Key, int)
MRP (decimal)
Publisher_Id (Foreign Key, int)
Published_Date (date)
Volume (int)
Status (varchar)
AUTHOR Table:
Author_Id (Primary Key, int)
Name (varchar)
Email (varchar)
Phone_Number (varchar)
Status (varchar)
BOOK_AUTHOR Table:
27
PUBLISHER Table:
Publisher_Id (Primary Key, int)
Name (varchar)
Address (varchar)
MEMBER Table:
Member_Id (Primary Key, int)
Name (varchar)
Branch_Code (varchar)
Roll_Number (varchar)
Phone_Number (varchar)
Email_Id (varchar)
Date_of_Join (date)
Status (varchar)
BOOK_ISSUE Table:
Issue_Id (Primary Key, int)
Date_Of_Issue (date)
Book_Id (Foreign Key, int)
Member_Id (Foreign Key, int)
Expected_Date_Of_Return (date)
Status (varchar)
BOOK_RETURN Table:
Issue_Id (Primary Key and Foreign Key, int)
Actual_Date_Of_Return (date)
LateDays (int)
LateFee (decimal)
LANGUAGE Table:
Language_Id (Primary Key, int)
Name (varchar)
LATE_FEE_RULE_26 Table:
FromDays (Part of Composite Primary Key, int)
ToDays (Part of Composite Primary Key, int)
Amount (decimal)
28
BOOK
Book_ID Title Language_ID MRP Publisher_ID Published_Date Volume Status
LANGUAGE
Language_ID Name
29
BOOK_AUTHOR
Book_ID Author_ID
BOOK_ISSUE
Issue_ID Date_of_Issue Book_ID Member_ID Expected_Date_of_Return Status
PUBLISHER
Publisher_ID Name Address
AUTHOR
Author_ID Name Email Phone_Number Status
MEMBER
Member_ID Name Branch_Code Roll_No Phone_Number Email Date_of_Join Status
BOOK_RETURN
Issue_ID Actual_Date_of_Return LateDays LateFee
LATE_FEE_RULE_26
FromDays ToDays Amount
PUBLISHER_26(Publisher_Id));
Query OK, 0 rows affected (1.83 sec)
+---------+------------------------------------+-------------+---------+--------------+----------------+--------+-----------+
| Book_Id | Title | Language_Id | MRP | Publisher_Id | Published_Date | Volume | Status |
+---------+------------------------------------+-------------+---------+--------------+----------------+--------+-----------+
| 1 | Sherlock Holmes | 1 | 500.00 | 1 | 1892-01-01 | 1 | Available |
| 2 | GOT (VOL 1&2) | 1 | 1000.00 | 2 | 1996-08-06 | 2 | Available |
| 3 | Randamoozham | 1 | 300.00 | 3 | 1984-01-01 | 1 | Available |
| 4 | Aadujeevitham | 1 | 350.00 | 4 | 2008-01-01 | 1 | Available |
| 5 | RAM C/O ANANDHI | 1 | 250.00 | 5 | 2019-01-01 | 1 | Available |
| 6 | Ponniyin Selvan (VOL 1&2) | 1 | 600.00 | 6 | 1950-01-01 | 2 | Available |
| 7 | 2 States: The Story of My Marriage | 1 | 200.00 | 7 | 2009-10-01 | 1 | Available |
| 8 | Half Girlfriend | 1 | 150.00 | 7 | 2014-10-01 | 1 | Available |
+---------+------------------------------------+-------------+---------+--------------+----------------+--------+-----------+
8 rows in set (0.01 sec)
+---------+-----------+
| Book_Id | Author_Id |
+---------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 7 |
+---------+-----------+
8 rows in set (0.00 sec)
b. Create and execute DROP TABLE command in tables with and without FOREIGN
KEY constraints.
c. Create and execute ALTER TABLE command in tables with data and without
data.
b. Get the list of publishers and the number of books published by each
publisher
c. Get the list of books that are issued but not returned
mysql> SELECT Title FROM BOOK_26 WHERE Book_Id IN (SELECT Book_Id FROM
BOOK_ISSUE_26 WHERE Status = 'Issued');
36
+-----------------+
| Title |
+-----------------+
| Sherlock Holmes |
| GOT (VOL 1&2) |
| RAM C/O ANANDHI |
+-----------------+
3 rows in set (0.02 sec)
+-------+
| Name |
+-------+
| Avani |
+-------+
1 row in set (0.00 sec)
e. Get the total fine collected for the current month and current quarter.
mysql> SELECT COALESCE(SUM(LateFee),0) AS TotalFine FROM BOOK_RETURN_26
WHERE MONTH(Actual_Date_of_Return) = MONTH(CURDATE()) AND
YEAR(Actual_Date_of_Return) = YEAR(CURDATE());
+-----------+
| TotalFine |
+-----------+
| 0.00 |
+-----------+
1 row in set (0.01 sec)
f. Get the list of students who have overdue (not returned the books even
on due date)
g. Calculate the fine (as of today) to be collected from each overdue book.
mysql> SELECT Issue_Id, DATEDIFF(CURDATE(), Expected_Date_of_Return) *
(SELECT Amount FROM LATE_FEE_RULE_26 WHERE FromDays<= DATEDIFF(CURDATE(),
Expected_Date_of_Return) AND ToDays>= DATEDIFF(CURDATE(),
Expected_Date_of_Return)) AS Fine FROM BOOK_ISSUE_26 WHERE
Expected_Date_of_Return< CURDATE() AND Status = 'Issued';
+----------+---------+
| Issue_Id | Fine |
+----------+---------+
| 1 | 3770.00 |
| 2 | 3730.00 |
| 3 | 5390.00 |
+----------+---------+
3 rows in set (0.00 sec)
h. MEMBER_26s who joined after Jan 1 2021 but has not taken any books
Result:
CYCLE-3
1. Write a PL/SQL block to read two numbers and find the greatest among
them.
mysql> DELIMITER //
mysql> CREATE PROCEDURE MAX (A INT,B INT)
-> BEGIN
-> IF (A>B) THEN
-> SELECT A;
-> ELSE
-> SELECT B;
-> END IF;
-> END;//
Query OK, 0 rows affected (0.13 sec)
mysql> CALL MAX (9,0);//
+------+
| A |
+------+
| 9 |
+------+
1 row in set (0.02 sec)
2. Write a PL/SQL block to read three numbers and find the greatest among
them.
mysql> DELIMITER //
mysql> CREATE PROCEDURE MAX3 (A INT,B INT, C INT)
-> BEGIN
-> IF (A>B) AND (A>C) THEN
-> SELECT A;
-> ELSEIF (B>A) AND (B>C) THEN
-> SELECT B;
-> ELSE
-> SELECT C;
-> END IF;
-> END;//
Query OK, 0 rows affected (0.14 sec)
mysql> CALL MAX3 (12,34,23);//
+------+
| B |
+------+
| 34 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
3. Write a PL/SQL block to read two numbers and print all the numbers
between them.
mysql> DELIMITER //
mysql> CREATE PROCEDURE NUMRANGE(A INT,B INT)
40
-> BEGIN
-> DECLARE I INT;
-> SET I=A;
-> WHILE I<B DO
-> SELECT I+1 AS NUMBER;
-> SET I=I+1;
-> END WHILE;
-> END;//
Query OK, 0 rows affected (0.16 sec)
4. Write a PL/SQL block to read N and find the sum of the series 1+2+3 +...
41
N.
6. Write a PL/SQL block to read a number and invert the given number.
mysql> DELIMITER //
mysql> CREATE PROCEDURE ID_Salary (EmpID INT)
-> BEGIN
-> DECLARE Emp_Salary DECIMAL(10,2);
-> SELECT Basic INTO Emp_Salary FROM Emp26 WHERE ID=EmpID;
-> IF Emp_Salary IS NULL THEN
-> SELECT 'Employee not found';
-> ELSE
-> SELECT Emp_Salary AS Salary;
-> END IF;
-> END;//
Query OK, 0 rows affected (0.16 sec)
mysql> CALL ID_Salary (127);//
+---------+
| Salary |
+---------+
| 4000.00 |
+---------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
9. Write a PL/SQL block to read ID of an employee and display his name and
birthdate.
mysql> CREATE PROCEDURE ID_BDAY (Emp_ID INT)
-> BEGIN
-> SELECT Name, BDate FROM Emp26 WHERE ID=Emp_ID;
-> END;//
Query OK, 0 rows affected (0.18 sec)
mysql> CALL ID_BDAY (108);//
+-------+------------+
| Name | BDate |
+-------+------------+
| Vivek | 2004-02-13 |
+-------+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
10. Write a PL/SQL block to read ID of an employee and display his month of
birth.
mysql> DELIMITER //
mysql> CREATE PROCEDURE MONTH(Emp_ID INT)
-> BEGIN
-> SELECT DATE_FORMAT(BDate, '%M') AS Birth_Month FROM Emp26 WHERE
ID=Emp_ID;
-> END;//
Query OK, 0 rows affected (0.14 sec)
44
11. Write a PL/SQL block to read IDs of two employees and display the
difference in salary
between them.
mysql> DELIMITER //
mysql> CREATE PROCEDURE SALARYDIFF (Emp_ID1 INT, Emp_ID2 INT)
-> BEGIN
-> SELECT (e1.Basic - e2.Basic) AS Salary_Difference FROM Emp26 e1,
Emp26 e2 WHERE e1.ID=Emp_ID1 AND e2.ID=Emp_ID2;
-> END;//
Query OK, 0 rows affected (0.13 sec)
mysql> DELIMITER
mysql> CREATE PROCEDURE TOPSAL ()
-> BEGIN
-> DECLARE done INT DEFAULT 0;
-> DECLARE emp_salary DECIMAL(10,2);
-> DECLARE cur CURSOR FOR
-> SELECT Basic FROM Emp26 ORDER BY Basic DESC LIMIT 10;
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-> OPEN cur;
->read_loop
-> : LOOP
-> FETCH cur INTO emp_salary;
-> IF done THEN
-> LEAVE read_loop;
-> END IF;
45
+------------+
| emp_salary |
+------------+
| 6000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 6000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 4500.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 4000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 2010.00 |
+------------+
1 row in set (0.01 sec)
46
+------------+
| emp_salary |
+------------+
| 2000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 2000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 2000.00 |
+------------+
1 row in set (0.01 sec)
+------------+
| emp_salary |
+------------+
| 1500.00 |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
13. Create a procedure to accept the dno and display the id, name and
salary of all the
employees working in that department. Execute this procedure and show the
result.
mysql> DELIMITER //
mysql> CREATE PROCEDURE GETDEPT (dept_no INT)
-> BEGIN
-> SELECT ID,Name,Basic
-> FROM Emp26
-> WHERE DeptID=dept_no; END;//
Query OK, 0 rows affected (0.13 sec)
mysql> CALL GETDEPT (3);//
+-----+--------+---------+
| ID | Name | Basic |
+-----+--------+---------+
| 131 | Raju | 2000.00 |
| 132 | Aleena | 1500.00 |
| 156 | Mary | 4500.00 |
| 201 | Nithin | 6000.00 |
+-----+--------+---------+
4 rows in set (0.00 sec)
47
15. Create a trigger to maintain an audit trail for employee table. When
insert, update or delete is
performed on employee table insert a row into emp_trail table with value
specifying the operation and
date of operation.
mysql> delimiter //
mysql> create trigger trail_audit_insert after insert on Emp26 for each row
-> begin
-> insert into emp_trail(operation,date) values
('insert',current_date());
-> end; //
Query OK, 0 rows affected (0.16 sec)
mysql> delimiter //
mysql> create trigger trail_audit_update after update on Emp26 for each row
-> begin
-> insert into emp_trail (operation,date) values
('update',current_date());
-> end;//
Query OK, 0 rows affected (0.18 sec)
mysql> delimiter //
mysql> create trigger trail_audit_delete after delete on Emp26 for each row
-> begin
-> insert into emp_trail(operation,date) values
('delete',current_date());
48
-> end; //
mysql> delimiter //
mysql> create trigger trail_audit_delete after delete on Emp26 for each row
-> begin
-> insert into emp_trail(operation,date) values
('delete',current_date());
-> end; //
Query OK, 0 rows affected (0.24 sec)
mysql> INSERT INTO Emp26 VALUES ('165', '1', 'Ansif', 'Clerk', 2000, 'M',
NULL, NULL, NULL, NULL);//
Query OK, 1 row affected (0.08 sec)
16. Create a trigger to maintain an audit trail for employee table for
tracking salary modifications. When salary is updates, insert into
emp_sal_trail table a row with values of employee id, name, salary before
modification, salary after modification and date of modification
mysql> delimiter //
mysql> create trigger p_sal before update on Emp26 for each row
-> begin
-> if(new.Basic<old.Basic) then signal sqlstate '45000'
-> set message_text='cannot decrease salary';
-> end if;
-> end;//
Query OK, 0 rows affected (0.20 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER update_on_total_sal
-> AFTER UPDATE ON Emp26
-> FOR EACH ROW
-> BEGIN
-> UPDATE Dept
-> SET total_sal = total_sal + (NEW.basic - OLD.basic)
-> WHERE DeptID = NEW.DeptID;
-> END;//
Query OK, 0 rows affected (0.22 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER update_on_insert_total_sal
-> AFTER INSERT ON Emp26
-> FOR EACH ROW
-> BEGIN
-> UPDATE Dept
-> SET total_sal = total_sal + NEW.Basic
-> WHERE DeptID = NEW.DeptID;
-> END;//
Query OK, 0 rows affected (0.18 sec)
mysql> INSERT INTO Emp26 (ID, DeptID, Name, Designation, Basic, Gender,
ManagerID, Join_date, City, BDate,total_sal) VALUES ('133', '4', 'Amal',
'Clerk', 4500, 'M', '156', '2022-01-23', 'Kannur', '2000-01-15',0);//
Query OK, 1 row affected (0.15 sec)
21. Create a database view ‘Available_Books’, which will list out books
that are currently available in the Library.
mysql> DELIMITER //
mysql> CREATE VIEW avail_book AS
-> SELECT b.Book_Id,b.Title,a.Name AS Author,p.Name AS Publisher from
52
BOOK_26 b
-> INNER JOIN BOOK_AUTHOR_26 ba on b.Book_Id=ba.Book_Id
-> INNER JOIN AUTHOR_26 a on ba.Author_Id=a.Author_Id
-> INNER JOIN PUBLISHER_26 p on b.Publisher_Id=p.Publisher_Id
-> WHERE b.Status="Available";//
Query OK, 0 rows affected (0.15 sec)
22. Create a database procedure to add, update and delete a book to the
Library database (use parameters).
23. Use cursors and create a procedure to print Books Issue Register .
+---------+------------+--------+----------+------------------+---------+
| IssueID | IssueDate | BookID | MEMBER_26ID | ExpectReturnDate | Status
|
+---------+------------+--------+----------+------------------+---------+
| 2 | 2023-09-05 | 2 | 2| 2023-09-12 | Pending |
+---------+------------+--------+----------+------------------+---------+
1 row in set (0.06 sec)
54
+---------+------------+--------+----------+------------------+---------+
| IssueID | IssueDate | BookID | MEMBER_26ID | ExpectReturnDate | Status
|
+---------+------------+--------+----------+------------------+---------+
| 3 | 2023-09-10 | 3 | 1| 2023-09-20 | Overdue |
+---------+------------+--------+----------+------------------+---------+
1 row in set (0.06 sec)
+---------+------------+--------+----------+------------------+----------+
| IssueID | IssueDate | BookID | MEMBER_26ID | ExpectReturnDate | Status
|
+---------+------------+--------+----------+------------------+----------+
| 4 | 2023-09-15 | 5 | 3 | 2023-09-30 | Returned |
+---------+------------+--------+----------+------------------+----------+
1 row in set (0.06 sec)
+---------+------------+--------+----------+------------------+----------+
| IssueID | IssueDate | BookID | MEMBER_26ID | ExpectReturnDate | Status
|
+---------+------------+--------+----------+------------------+----------+
| 4 | 2023-09-15 | 5 | 3 | 2023-09-30 | Returned |
+---------+------------+--------+----------+------------------+----------+
1 row in set (0.06 sec)
24. Create a history table (you may use the same structure without any
keys) for the MEMBER_26 table and
copy the original values of the row being updated to the history table
using a TRIGGER.
-> Status)
-> VALUES (
->OLD.MemberId,
->OLD.Name,
->OLD.BranchCode,
->OLD.RollNo,
->OLD.PhoneNumber,
->OLD.EMail,
->OLD.JoinDate,
->OLD.Status);
-> END;//
Query OK, 0 rows affected (0.18 sec)
Result:
CYCLE-4
GROUP PROJECT
57
Abstract
The system includes functionalities for entering patient details such as name,
age, gender, department, and contact information, which are securely stored
in a MySQL database. It facilitates updating existing patient records through a
unique patient ID and allows for the deletion of records after confirming the
specified ID's existence. Users can also search for specific patients using their
ID and view all patient records in a structured format for enhanced visibility.
1.ER Diagram
2.Relational Schema
PATIENT
ID Name Age Gender Department Contact
3.Screenshot
62
Figure 3: Insert
63
Figure 4:Update
64
Figure 5: Search
65
Figure 6:Delete
66
Figure 7: Display
67
4.Program Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Random;
private JComboBox<String>actionDropdown;
public EnhancedPatientManagementGUI() {
setTitle("Patient Management System");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(actionDropdown);
add(executeButton);
setVisible(true);
executeButton.addActionListener(e ->executeAction());
}
if ("Add Patient".equals(selectedAction)) {
showAddPatientForm();
} else if ("Update Patient".equals(selectedAction)) {
showUpdatePatientForm();
} else if ("Delete Patient".equals(selectedAction)) {
showDeletePatientForm();
} else if ("Search Patient".equals(selectedAction)) {
showSearchPatientForm();
} else if ("Display Patients".equals(selectedAction)) {
showAllPatientsForm();
} else {
JOptionPane.showMessageDialog(this, "Please select a valid action.");
}
}
addPatientFrame.add(nameLabel);
addPatientFrame.add(nameField);
addPatientFrame.add(ageLabel);
addPatientFrame.add(ageField);
addPatientFrame.add(genderLabel);
addPatientFrame.add(genderField);
addPatientFrame.add(specialtyLabel);
addPatientFrame.add(specialtyField);
addPatientFrame.add(contactLabel);
addPatientFrame.add(contactField);
addPatientFrame.add(new JLabel());
69
addPatientFrame.add(submitButton);
addPatientFrame.setVisible(true);
submitButton.addActionListener(e -> {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = (String) genderField.getSelectedItem();
String specialty = specialtyField.getText();
String contact = contactField.getText();
addPatientToDatabase(name, age, gender, specialty, contact);
addPatientFrame.dispose();
});
}
updatePatientFrame.add(idLabel);
updatePatientFrame.add(idField);
updatePatientFrame.add(nameLabel);
updatePatientFrame.add(nameField);
updatePatientFrame.add(specializationLabel);
updatePatientFrame.add(specializationField);
updatePatientFrame.add(new JLabel());
updatePatientFrame.add(submitButton);
updatePatientFrame.setVisible(true);
submitButton.addActionListener(e -> {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String newSpecialization = specializationField.getText();
updatePatientInDatabase(id, name, newSpecialization);
70
updatePatientFrame.dispose();
});
}
deletePatientFrame.add(idLabel);
deletePatientFrame.add(idField);
deletePatientFrame.add(new JLabel());
deletePatientFrame.add(deleteButton);
deletePatientFrame.setVisible(true);
deleteButton.addActionListener(e -> {
int id = Integer.parseInt(idField.getText());
deletePatientFromDatabase(id);
deletePatientFrame.dispose();
});
}
searchPatientFrame.add(idLabel);
searchPatientFrame.add(idField);
searchPatientFrame.add(new JLabel());
searchPatientFrame.add(searchButton);
searchPatientFrame.setVisible(true);
searchButton.addActionListener(e -> {
71
int id = Integer.parseInt(idField.getText());
searchPatientInDatabase(id);
});
}
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
String specialty = resultSet.getString("specialty");
String contact = resultSet.getString("contact");
patientDetails.append(id).append("\t")
.append(name).append("\t")
.append(age).append("\t")
.append(gender).append("\t")
.append(specialty).append("\t")
.append(contact).append("\n");
}
textArea.setText(patientDetails.toString());
} catch (SQLException e) {
e.printStackTrace();
72
allPatientsFrame.setVisible(true);
}
String query = "INSERT INTO patients (id, name, age, gender, specialty,
contact) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatementpreparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, patientId);
preparedStatement.setString(2, name);
preparedStatement.setInt(3, age);
preparedStatement.setString(4, gender);
preparedStatement.setString(5, specialty);
preparedStatement.setString(6, contact);
} catch (SQLException e) {
73
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error while registering patient: " +
e.getMessage());
}
}
}
if (checkResult.next()) {
String deleteQuery = "DELETE FROM patients WHERE id = ?";
PreparedStatementdeleteStatement = connection.prepareStatement(deleteQuery);
deleteStatement.setInt(1, id);
deleteStatement.executeUpdate();
JOptionPane.showMessageDialog(this, "Patient deleted successfully!");
} else {
JOptionPane.showMessageDialog(this, "Error: Patient ID not found.");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error while deleting patient: " +
e.getMessage());
}
}
}
if (checkResult.next()) {
74
ResultSetresultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String patientInfo = "ID: " + resultSet.getInt("id") +
"\nName: " + resultSet.getString("name") +
"\nAge: " + resultSet.getInt("age") +
"\nGender: " + resultSet.getString("gender") +
"\nDepartment: " + resultSet.getString("specialty") +
"\nContact: " + resultSet.getString("contact");
JOptionPane.showMessageDialog(this, patientInfo);
} else {
JOptionPane.showMessageDialog(this, "Patient not found.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
75