Lab 03: Stored Procedures & Functions
Step 1 — MySQL: Setup and Basic Procedure
-- Create database and table
CREATE DATABASE labdb;
USE labdb;
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer VARCHAR(100),
product VARCHAR(100),
quantity INT,
unit_price DECIMAL(10,2),
total DECIMAL(10,2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO orders (customer, product, quantity, unit_price, total) VALUES
('Alice', 'Widget A', 5, 9.99, 49.95),
('Bob', 'Widget B', 3, 14.99, 44.97),
('Alice', 'Gadget X', 2, 29.99, 59.98),
('Carol', 'Widget A', 10, 9.99, 99.90),
('Bob', 'Gadget Y', 1, 49.99, 49.99);Step 2 — MySQL: IN / OUT / INOUT Parameters
Step 3 — MySQL: IF / WHILE Control Flow
Step 4 — MySQL: LOOP and Cursor Pattern
Step 5 — PostgreSQL: CREATE FUNCTION with PL/pgSQL
Step 6 — PostgreSQL: DECLARE and Exception Handling
Step 7 — PostgreSQL: RETURNS TABLE with Set-Returning Functions
Step 8 — Capstone: Audit-Enabled Order Processing
Summary
Feature
MySQL
PostgreSQL
Last updated
