MySQL Assignment – 4
Basic Questions
- Create a table orders with fields: order_id INT AUTO_INCREMENT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2), order_date DATE.
- Insert 5 sample orders in one SQL statement.
- Lock the orders table for write operations.
- Unlock the orders table.
- Create a stored procedure get_all_customers() to return all rows from customers.
- Create a stored procedure get_customer_by_id(IN cust_id INT) that returns customer details.
- Write a stored procedure increase_salary(IN emp_id INT, IN bonus DECIMAL(10,2)) that updates salary.
- Declare a local variable inside a stored procedure and set its value.
- Create a function get_fullname(fname VARCHAR(50), lname VARCHAR(50)) that concatenates names.
- Create a function square_num(num INT) that returns the square.
- Create a trigger before_student_update that sets last_modified = NOW() before updating students.
- Drop a trigger before_student_update.
- Create a new user report_user with password report123.
- Grant SELECT on all tables of salesdb to report_user.
- Revoke INSERT privilege on salesdb.orders from report_user.
- Insert 3 rows into products inside a transaction and commit.
- Insert a row into customers inside a transaction and rollback.
- Create a savepoint sp1 after inserting a row in payments, then rollback to sp1.
- Create an index on employees.salary.
- Drop the index on employees.salary.
Intermediate Questions
- Simulate two transactions locking the same row in orders to show lock contention.
- Write a stored procedure bulk_insert_orders(IN cust_id INT) that inserts 10 dummy orders for the same customer using a loop.
- Write a stored procedure add_order(IN cust_id INT, IN amt DECIMAL(10,2)) that inserts into orders.
- Alter procedure add_order to also accept order_date.
- Drop the procedure add_order.
- Create a function total_order_amount(cust INT) returning total spent by a customer.
- Create a function calculate_age(dob DATE) returning exact age in years.
- Create a trigger prevent_delete_employee that stops deletion from employees.
- Create a trigger log_insert_order that copies inserted rows into orders_audit.
- List all triggers in the current database.
- Create a role readonly_role and grant it SELECT on all tables.
- Grant readonly_role to report_user.
- Drop role readonly_role.
- Create a covering index on customers(city, name, email).
- Run a transaction inserting into orders, updating customers, and rollback if any error occurs.
- Create a transaction inserting into payments and commit if all succeed.
- Write SQL to show query execution plan for SELECT * FROM orders WHERE amount > 5000.
- Create an index on orders(order_date) and run a query using it.
- Export table orders using mysqldump.
- Import table orders into another database using mysql CLI.
Advanced Questions
- Simulate a deadlock with two sessions updating different rows in reverse order.
- Write a stored procedure transfer_amount(IN from_id INT, IN to_id INT, IN amt DECIMAL(10,2)) with error handling using DECLARE HANDLER.
- Write a procedure safe_transaction() that inserts into two tables and rolls back on error.
- Create a function nth_highest_salary(n INT) using a subquery with LIMIT and OFFSET.
- Create a function validate_password(pwd VARCHAR(100)) returning 1 if length > 8 and contains @, else 0.
- Create a trigger before_insert_salary that prevents salary < 10000.
- Create a trigger after_update_salary that logs old vs new salary into salary_history.
- Create roles admin_role, manager_role, clerk_role with different privileges and assign to users.
- Optimize a query joining employees, departments, and projects using proper indexes.
- Write a shell script that runs mysqldump every day and saves file with timestamp in filename.