|
1 | | --- Create Database |
| 1 | +-- Example MySQL database schema with common features |
2 | 2 | CREATE DATABASE IF NOT EXISTS example_db; |
3 | 3 | USE example_db; |
4 | 4 |
|
5 | | --- Create Tables |
| 5 | +-- User management tables |
6 | 6 | CREATE TABLE users ( |
7 | 7 | id INT AUTO_INCREMENT PRIMARY KEY, |
8 | 8 | username VARCHAR(50) NOT NULL UNIQUE, |
9 | | - email VARCHAR(100) NOT NULL, |
10 | | - password VARCHAR(255) NOT NULL, |
11 | | - status ENUM('active', 'inactive') DEFAULT 'active', |
| 9 | + email VARCHAR(100) NOT NULL UNIQUE, |
| 10 | + password_hash VARCHAR(255) NOT NULL, |
| 11 | + first_name VARCHAR(50), |
| 12 | + last_name VARCHAR(50), |
| 13 | + date_of_birth DATE, |
| 14 | + status ENUM('active', 'inactive', 'suspended') DEFAULT 'active', |
12 | 15 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
13 | | - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
| 16 | + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 17 | + last_login TIMESTAMP NULL, |
| 18 | + is_admin BOOLEAN DEFAULT FALSE, |
| 19 | + metadata JSON, |
| 20 | + INDEX idx_user_status (status), |
| 21 | + INDEX idx_user_email (email) |
14 | 22 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
15 | 23 |
|
16 | | -CREATE TABLE posts ( |
| 24 | +-- Product catalog |
| 25 | +CREATE TABLE categories ( |
17 | 26 | id INT AUTO_INCREMENT PRIMARY KEY, |
18 | | - user_id INT NOT NULL, |
19 | | - title VARCHAR(255) NOT NULL, |
20 | | - content TEXT, |
| 27 | + name VARCHAR(100) NOT NULL, |
| 28 | + description TEXT, |
| 29 | + parent_id INT NULL, |
| 30 | + level INT GENERATED ALWAYS AS ( |
| 31 | + CASE |
| 32 | + WHEN parent_id IS NULL THEN 0 |
| 33 | + ELSE 1 + (SELECT level FROM categories p WHERE p.id = categories.parent_id) |
| 34 | + END |
| 35 | + ) STORED, |
| 36 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 37 | + FOREIGN KEY (parent_id) REFERENCES categories(id) ON DELETE CASCADE |
| 38 | +) ENGINE=InnoDB; |
| 39 | + |
| 40 | +CREATE TABLE products ( |
| 41 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 42 | + category_id INT NOT NULL, |
| 43 | + name VARCHAR(200) NOT NULL, |
| 44 | + description TEXT, |
| 45 | + price DECIMAL(10,2) NOT NULL, |
| 46 | + stock_quantity INT UNSIGNED DEFAULT 0, |
21 | 47 | status ENUM('draft', 'published', 'archived') DEFAULT 'draft', |
22 | 48 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
23 | 49 | updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
24 | | - CONSTRAINT fk_posts_users FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE |
25 | | -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
| 50 | + metadata JSON, |
| 51 | + FOREIGN KEY (category_id) REFERENCES categories(id), |
| 52 | + FULLTEXT INDEX idx_product_search (name, description) |
| 53 | +) ENGINE=InnoDB; |
| 54 | + |
| 55 | +-- Order management |
| 56 | +CREATE TABLE orders ( |
| 57 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 58 | + user_id INT NOT NULL, |
| 59 | + order_number VARCHAR(50) NOT NULL UNIQUE, |
| 60 | + status ENUM('pending', 'processing', 'shipped', 'delivered', 'cancelled') DEFAULT 'pending', |
| 61 | + total_amount DECIMAL(12,2) NOT NULL, |
| 62 | + shipping_address TEXT NOT NULL, |
| 63 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 64 | + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 65 | + FOREIGN KEY (user_id) REFERENCES users(id) |
| 66 | +) ENGINE=InnoDB; |
26 | 67 |
|
27 | | --- Create Indexes |
28 | | -CREATE INDEX idx_users_email ON users(email); |
29 | | -CREATE INDEX idx_posts_user_id ON posts(user_id); |
30 | | -CREATE INDEX idx_posts_status ON posts(status); |
| 68 | +CREATE TABLE order_items ( |
| 69 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 70 | + order_id INT NOT NULL, |
| 71 | + product_id INT NOT NULL, |
| 72 | + quantity INT NOT NULL CHECK (quantity > 0), |
| 73 | + unit_price DECIMAL(10,2) NOT NULL, |
| 74 | + total_price DECIMAL(12,2) GENERATED ALWAYS AS (quantity * unit_price) STORED, |
| 75 | + FOREIGN KEY (order_id) REFERENCES orders(id), |
| 76 | + FOREIGN KEY (product_id) REFERENCES products(id) |
| 77 | +) ENGINE=InnoDB; |
31 | 78 |
|
32 | | --- Create View |
33 | | -CREATE OR REPLACE VIEW active_users_view AS |
34 | | -SELECT |
35 | | - u.*, |
36 | | - COUNT(p.id) as post_count, |
37 | | - MAX(p.created_at) as last_post_date |
38 | | -FROM users u |
39 | | -LEFT JOIN posts p ON u.id = p.user_id |
40 | | -WHERE u.status = 'active' |
41 | | -GROUP BY u.id; |
42 | | - |
43 | | --- Create Trigger for Logging |
| 79 | +-- Reviews and ratings |
| 80 | +CREATE TABLE reviews ( |
| 81 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 82 | + product_id INT NOT NULL, |
| 83 | + user_id INT NOT NULL, |
| 84 | + rating TINYINT NOT NULL CHECK (rating BETWEEN 1 AND 5), |
| 85 | + title VARCHAR(200), |
| 86 | + content TEXT, |
| 87 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 88 | + is_verified BOOLEAN DEFAULT FALSE, |
| 89 | + UNIQUE KEY unique_review (product_id, user_id), |
| 90 | + FOREIGN KEY (product_id) REFERENCES products(id), |
| 91 | + FOREIGN KEY (user_id) REFERENCES users(id) |
| 92 | +) ENGINE=InnoDB; |
| 93 | + |
| 94 | +-- Stored procedures |
44 | 95 | DELIMITER // |
45 | | -CREATE TRIGGER users_after_delete |
46 | | -AFTER DELETE ON users |
| 96 | + |
| 97 | +CREATE PROCEDURE calculate_product_rating(IN product_id_param INT, OUT avg_rating DECIMAL(3,2)) |
| 98 | +BEGIN |
| 99 | + SELECT AVG(rating) INTO avg_rating |
| 100 | + FROM reviews |
| 101 | + WHERE product_id = product_id_param; |
| 102 | +END // |
| 103 | + |
| 104 | +CREATE PROCEDURE update_stock( |
| 105 | + IN product_id_param INT, |
| 106 | + IN quantity_param INT, |
| 107 | + OUT new_stock INT |
| 108 | +) |
| 109 | +BEGIN |
| 110 | + UPDATE products |
| 111 | + SET stock_quantity = stock_quantity + quantity_param |
| 112 | + WHERE id = product_id_param; |
| 113 | + |
| 114 | + SELECT stock_quantity INTO new_stock |
| 115 | + FROM products |
| 116 | + WHERE id = product_id_param; |
| 117 | +END // |
| 118 | + |
| 119 | +DELIMITER ; |
| 120 | + |
| 121 | +-- Triggers |
| 122 | +DELIMITER // |
| 123 | + |
| 124 | +CREATE TRIGGER before_order_insert |
| 125 | +BEFORE INSERT ON orders |
47 | 126 | FOR EACH ROW |
48 | 127 | BEGIN |
49 | | - INSERT INTO user_logs (user_id, action, action_date) |
50 | | - VALUES (OLD.id, 'DELETE', NOW()); |
| 128 | + SET NEW.order_number = CONCAT('ORD', DATE_FORMAT(NOW(), '%Y%m%d'), LPAD(FLOOR(RAND() * 10000), 4, '0')); |
51 | 129 | END // |
| 130 | + |
| 131 | +CREATE TRIGGER after_order_item_insert |
| 132 | +AFTER INSERT ON order_items |
| 133 | +FOR EACH ROW |
| 134 | +BEGIN |
| 135 | + UPDATE products |
| 136 | + SET stock_quantity = stock_quantity - NEW.quantity |
| 137 | + WHERE id = NEW.product_id; |
| 138 | +END // |
| 139 | + |
52 | 140 | DELIMITER ; |
53 | 141 |
|
54 | | --- Insert Data |
55 | | -INSERT INTO users (id, username, email, password) VALUES |
56 | | -(1, 'john_doe', 'john@example.com', 'hashed_password_1'), |
57 | | -(2, 'jane_doe', 'jane@example.com', 'hashed_password_2'), |
58 | | -(3, 'alice_smith', 'alice@example.com', 'hashed_password_3'), |
59 | | -(4, 'bob_wilson', 'bob@example.com', 'hashed_password_4'); |
60 | | - |
61 | | -INSERT INTO posts (id, user_id, title, content, status) VALUES |
62 | | -(1, 1, 'First Post', 'This is my first post content', 'published'), |
63 | | -(2, 1, 'Second Post', 'This is a draft post', 'draft'), |
64 | | -(3, 2, 'Hello World', 'Post by Jane', 'published'), |
65 | | -(4, 2, 'Another Post', 'Another post by Jane', 'published'), |
66 | | -(5, 3, 'Tech News', 'Latest technology news', 'published'), |
67 | | -(6, 3, 'Draft Post', 'Work in progress', 'draft'), |
68 | | -(7, 4, 'Travel Blog', 'My travel experiences', 'published'), |
69 | | -(8, 4, 'Food Blog', 'Best recipes', 'published'); |
| 142 | +-- Sample data |
| 143 | +INSERT INTO categories (name, description, parent_id) VALUES |
| 144 | + ('Electronics', 'Electronic devices and accessories', NULL), |
| 145 | + ('Computers', 'Desktop and laptop computers', 1), |
| 146 | + ('Smartphones', 'Mobile phones and accessories', 1), |
| 147 | + ('Books', 'Physical and digital books', NULL), |
| 148 | + ('Programming', 'Programming and technical books', 4); |
| 149 | + |
| 150 | +INSERT INTO users (username, email, password_hash, first_name, last_name, status) VALUES |
| 151 | + ('john_doe', 'john@example.com', SHA2('password123', 256), 'John', 'Doe', 'active'), |
| 152 | + ('jane_smith', 'jane@example.com', SHA2('password456', 256), 'Jane', 'Smith', 'active'); |
| 153 | + |
| 154 | +INSERT INTO products (category_id, name, description, price, stock_quantity, status) VALUES |
| 155 | + (2, 'Gaming Laptop', '15" Gaming Laptop with RTX 3080', 1499.99, 10, 'published'), |
| 156 | + (3, 'Smartphone X', 'Latest smartphone with 5G support', 999.99, 20, 'published'), |
| 157 | + (5, 'Python Programming', 'Complete guide to Python programming', 49.99, 100, 'published'); |
| 158 | + |
| 159 | +-- Views |
| 160 | +CREATE VIEW product_summary AS |
| 161 | +SELECT |
| 162 | + p.id, |
| 163 | + p.name, |
| 164 | + c.name AS category, |
| 165 | + p.price, |
| 166 | + p.stock_quantity, |
| 167 | + COUNT(r.id) AS review_count, |
| 168 | + AVG(r.rating) AS avg_rating |
| 169 | +FROM products p |
| 170 | +LEFT JOIN categories c ON p.category_id = c.id |
| 171 | +LEFT JOIN reviews r ON p.id = r.product_id |
| 172 | +GROUP BY p.id; |
0 commit comments