DB - Lab 13 - Lab Tasks
DB - Lab 13 - Lab Tasks
Query
Output
Create a procedure that updates the audit log whenever a record is inserted, updated, or deleted from
the database tables. The procedure should take the operation, table name, record ID, and timestamp,
Task 2
and description as inputs. The description can contain the error message if the operation is not
performed successfully.
Query
Output
Write a Python program that gets values from the user as long as the user wants to add records and
Task 3
insert student records into the students table. Also update the audit log accordingly.
Query
Output
Write a Python program that adds multiple courses based on multiple user inputs. Update the log
Task 4
accordingly.
1
Query
Database Systems – Lab 13
sql
Copy code
CREATE DATABASE school;
USE school;
2
Database Systems – Lab 13
IN desc_text TEXT
)
BEGIN
INSERT INTO audit_log (operation, table_name, record_id, timestamp, description)
VALUES (op, tbl_name, rec_id, NOW(), desc_text);
END //
DELIMITER ;
Task 3: Python program to insert student records and update the audit log
python
Copy code
import mysql.connector as c
from mysql.connector import Error
def insert_student():
try:
dataBase = c.connect(
host="localhost",
user="root",
password="Namal.123",
database="school"
)
if dataBase.is_connected():
cursor = dataBase.cursor()
while True:
student_id = int(input("Enter Student ID: "))
student_name = input("Enter Student Name: ")
student_dob = input("Enter Student DOB (YYYY-MM-DD): ")
student_semester = int(input("Enter Student Semester: "))
query = "INSERT INTO students (id, name, dob, semester) VALUES (%s,
%s, %s, %s)"
cursor.execute(query, (student_id, student_name, student_dob,
student_semester))
dataBase.commit()
print("Student record inserted successfully.")
except Error as e:
print(f"Error occurred: {e}")
if dataBase.is_connected():
dataBase.rollback()
finally:
if dataBase.is_connected():
3
Database Systems – Lab 13
cursor.close()
dataBase.close()
print("MySQL connection is closed")
insert_student()
if dataBase.is_connected():
cursor = dataBase.cursor()
while True:
course_id = int(input("Enter Course ID: "))
course_name = input("Enter Course Name: ")
course_description = input("Enter Course Description: ")
query = "INSERT INTO courses (id, name, description) VALUES (%s, %s,
%s)"
cursor.execute(query, (course_id, course_name, course_description))
dataBase.commit()
print("Course record inserted successfully.")
except Error as e:
print(f"Error occurred: {e}")
if dataBase.is_connected():
dataBase.rollback()
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
insert_courses()
4
Database Systems – Lab 13
if dataBase.is_connected():
cursor = dataBase.cursor()
dataBase.commit()
print("Student enrolled successfully.")
except Error as e:
print(f"Error occurred: {e}")
if dataBase.is_connected():
dataBase.rollback()
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
enroll_student()
5
Database Systems – Lab 13
Task 7: Python program to update the semester of students enrolled in a specific course
python
Copy code
def update_student_semester():
try:
dataBase = c.connect(
host="localhost",
user="root",
password="Namal.123",
database="school"
)
if dataBase.is_connected():
cursor = dataBase.cursor()
dataBase.commit()
print("Student semesters updated successfully.")
except Error as e:
print(f"Error occurred: {e}")
if dataBase.is_connected():
dataBase.rollback()
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
update_student_semester()
6
Database Systems – Lab 13
def search_courses():
try:
dataBase = c.connect(
host="localhost",
user="root",
password="Namal.123",
database="school"
)
if dataBase.is_connected():
cursor = dataBase.cursor()
courses = cursor.fetchall()
for course in courses:
print(f"ID: {course[0]}, Name: {course[1]}, Description:
{course[2]}")
except Error as e:
print(f"Error occurred: {e}")
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
search_courses()
Task 9: Procedure to retrieve names of students and their courses, and a Python
program to call it
sql
Copy code
DELIMITER //
CREATE PROCEDURE get_student_courses()
BEGIN
SELECT s.name AS student_name, c.name AS course_name
FROM students s
JOIN enrollments e ON s.id = e.student_id
JOIN courses c ON e.course_id = c.id;
END //
DELIMITER ;
python
Copy code
def get_student_courses():
try:
dataBase = c.connect(
7
Database Systems – Lab 13
host="localhost",
user="root",
password="Namal.123",
database="school"
)
if dataBase.is_connected():
cursor = dataBase.cursor()
cursor.callproc('get_student_courses')
except Error as e:
print(f"Error occurred: {e}")
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
get_student_courses()
To develop a Python program that groups student records by their semester, computes the total number
of students in each semester, and displays the student details for each semester, you can use the
following approach. This program will use MySQL to retrieve and process the data.
1. Connect to the MySQL Database: Establish a connection to the MySQL database using
mysql.connector.
2. Retrieve Data: Use SQL queries to fetch the necessary data from the database.
8
Database Systems – Lab 13
3. Group Data by Semester: Process the data to group students by their semester and count the
number of students in each semester.
4. Display Data: Format and display the grouped data.
def group_students_by_semester():
try:
# Establish connection to the database
dataBase = c.connect(
host="localhost",
user="root",
password="Namal.123",
database="school"
)
if dataBase.is_connected():
cursor = dataBase.cursor()
9
Database Systems – Lab 13
except Error as e:
print(f"Error occurred: {e}")
finally:
if dataBase.is_connected():
cursor.close()
dataBase.close()
print("MySQL connection is closed")
10