7,8
7,8
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10,2)
);
DELIMITER //
BEGIN
END //
DELIMITER ;
-- Call the stored procedure
CALL UpdateCustomerSalaries();
7B
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10,2)
);
DELIMITER //
FROM customers;
OPEN cur;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
END //
DELIMITER ;
CALL ListCustomerDetails();
8A
This trigger will fire before an UPDATE operation on the customers table and will log the old
salary, new salary, and the difference.
DELIMITER //
-- Output the result (In practice, you would insert this into a log
table)
SELECT CONCAT('Old salary: ', old_salary) AS Old_Salary,
CONCAT('New salary: ', new_salary) AS New_Salary,
CONCAT('Salary difference: ', salary_difference) AS
Salary_Difference;
END //
DELIMITER ;
Sample Output
+--------------------+--------------------+-----------------------+
| Old_Salary | New_Salary | Salary_Difference |
+--------------------+--------------------+-----------------------+
| Old salary: 20000 | New salary: 25000 | Salary difference: 5000 |
+--------------------+--------------------+-----------------------+
Explanation
8B
In MySQL, you can create a similar functionality using stored procedures and triggers, but
MySQL does not support PL/SQL packages directly. However, you can achieve similar behavior
by creating separate stored procedures and calling them as needed.
Here’s how you can implement the functionalities described using MySQL.
DELIMITER ;
2.2 Delete Customer
DELIMITER //
DELIMITER ;
2.3 List Customers
DELIMITER //
OPEN cur;
read_loop: LOOP
FETCH cur INTO v_id, v_name;
IF done THEN
LEAVE read_loop;
END IF;
-- Print the results (In practice, you would select this to a log
table or use another method to display)
SELECT CONCAT('Customer(', v_id, '): ', v_name) AS Customer_Details;
END LOOP;
CLOSE cur;
END //
DELIMITER ;
Add Customers
CALL addCustomer(1, 'Ramesh', 32, 'Ahmedabad', 3000.00);
CALL addCustomer(2, 'Khilan', 25, 'Delhi', 3000.00);
CALL addCustomer(3, 'Kaushik', 23, 'Kota', 3000.00);
CALL addCustomer(4, 'Chaitali', 25, 'Mumbai', 7500.00);
CALL addCustomer(5, 'Hardik', 27, 'Bhopal', 9500.00);
CALL addCustomer(6, 'Komal', 22, 'MP', 5500.00);
List Customers
CALL listCustomer();
Delete a Customer
CALL delCustomer(4); -- Example to delete customer with ID 4
List Customers Again
CALL listCustomer();
Sample Output
Here’s the expected output from listing customers before and after deletion:
Before Deletion:
+---------------------------+
| Customer_Details |
+---------------------------+
| Customer(1): Ramesh |
| Customer(2): Khilan |
| Customer(3): Kaushik |
| Customer(4): Chaitali |
| Customer(5): Hardik |
| Customer(6): Komal |
+---------------------------+
+---------------------------+
| Customer_Details |
+---------------------------+
| Customer(1): Ramesh |
| Customer(2): Khilan |
| Customer(3): Kaushik |
| Customer(5): Hardik |
| Customer(6): Komal |
+---------------------------+
Explanation
Procedures:
o addCustomer: Adds a new customer to the customers table.
o delCustomer: Deletes a customer from the customers table based on the ID.
o listCustomer: Lists all customers from the customers table using a cursor.
Cursor:
o Declare Cursor: DECLARE cur CURSOR FOR SELECT ID, NAME FROM
customers; specifies the cursor to iterate through customer records.
o Handler: DECLARE CONTINUE HANDLER FOR NOT FOUND handles the end of the
cursor’s result set.
o Loop: Loops through the cursor to fetch and display each customer.