赞
踩
- CREATE TABLE products (-- 产品表
- product_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '商品ID',
- product_name VARCHAR(255) NOT NULL COMMENT '商品名称',
- product_category VARCHAR(100) NOT NULL COMMENT '商品类别',
- specification VARCHAR(255) COMMENT '规格',
- unit_price DECIMAL(10, 2) NOT NULL COMMENT '单价',
- stock_quantity INT NOT NULL DEFAULT 0 COMMENT '库存数量'
- );
- CREATE TABLE warehouses (-- 仓库表
- warehouse_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '库房ID',
- warehouse_name VARCHAR(255) NOT NULL COMMENT '库房名称',
- location VARCHAR(255) NOT NULL COMMENT '位置',
- area DECIMAL(10, 2) NOT NULL COMMENT '面积'
- ); CREATE TABLE suppliers (-- 供应商表
- supplier_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '供应商ID',
- supplier_name VARCHAR(255) NOT NULL COMMENT '供应商名称',
- contact_person VARCHAR(100) COMMENT '联系人',
- phone_number VARCHAR(20) COMMENT '联系电话',
- email VARCHAR(100) COMMENT '电子邮箱',
- address VARCHAR(255) COMMENT '地址'
- );CREATE TABLE orders (-- 订单表
- order_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '订单ID',
- customer_name VARCHAR(255) NOT NULL COMMENT '客户名称',
- order_date DATE NOT NULL COMMENT '订单日期',
- total_amount DECIMAL(10, 2) NOT NULL COMMENT '订单总金额',
- status ENUM('Pending', 'Shipped', 'Delivered', 'Cancelled') NOT NULL COMMENT '订单状态'
- );
- CREATE TABLE employees (
- employee_id int(11) NOT NULL AUTO_INCREMENT COMMENT '员工序号',
- employee_name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '员工姓名',
- department varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '所属部门',
- inventory_responsibility varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '员工职责',
- inventory_count int(11) NULL DEFAULT NULL COMMENT '记录员工的库存数量',
- employee_phone VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '员工电话',
- PRIMARY KEY (employee_id) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
- CREATE TABLE inventory_transactions (-- 库存表
- transaction_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '出入库记录ID',
- product_id INT,
- warehouse_id INT,
- employee_id INT,
- transaction_type ENUM('In', 'Out') NOT NULL COMMENT '出入库类型',
- quantity INT NOT NULL COMMENT '数量',
- transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '交易时间',
- FOREIGN KEY (product_id) REFERENCES products(product_id),
- FOREIGN KEY (warehouse_id) REFERENCES warehouses(warehouse_id),
- FOREIGN KEY (employee_id) REFERENCES employees(employee_id) );DROP TABLE IF EXISTS `users`;-- 用户表
- CREATE TABLE `users` (
- `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
- `username` varchar(255) DEFAULT NULL,
- `email` varchar(100) DEFAULT NULL COMMENT '电子邮箱',
- `phone_number` varchar(20) DEFAULT NULL COMMENT '联系电话',
- `role` enum('Admin','User') NOT NULL COMMENT '角色',
- PRIMARY KEY (`user_id`),
- UNIQUE KEY `email` (`email`),
- UNIQUE KEY `username` (`username`(50))
- ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
- CREATE TABLE return_orders (-- 退货单表
- return_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '退货单ID',
- order_id INT NOT NULL COMMENT '原始订单ID',
- customer_name VARCHAR(255) NOT NULL COMMENT '客户名称',
- return_date DATE NOT NULL COMMENT '退货日期',
- status ENUM('Pending', 'Refunded', 'Rejected') NOT NULL COMMENT '退货状态',
- FOREIGN KEY (order_id) REFERENCES orders(order_id) );
- CREATE TABLE return_details (-- 退货明细表
- detail_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '退货明细ID',
- return_id INT NOT NULL COMMENT '退货单ID',
- product_id INT NOT NULL COMMENT '商品ID',
- quantity INT NOT NULL COMMENT '退货数量',
- FOREIGN KEY (return_id) REFERENCES return_orders(return_id),
- FOREIGN KEY (product_id) REFERENCES products(product_id) );
- CREATE TABLE order_details (-- 订单明细表
- detail_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '订单明细ID',
- order_id INT NOT NULL,
- product_id INT NOT NULL,
- quantity INT NOT NULL COMMENT '数量',
- unit_price DECIMAL(10, 2) NOT NULL COMMENT '单价',
- FOREIGN KEY (order_id) REFERENCES orders(order_id),
- FOREIGN KEY (product_id) REFERENCES products(product_id) );
- CREATE TABLE purchase_orders (-- 采购单表
- po_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '采购单ID',
- supplier_id INT NOT NULL,
- purchase_date DATE NOT NULL COMMENT '采购日期',
- total_amount DECIMAL(10, 2) NOT NULL COMMENT '采购总金额',
- status ENUM('Pending', 'Received', 'Cancelled') NOT NULL COMMENT '采购单状态',
- FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id) );
- CREATE TABLE inventory (-- 库存表
- inventory_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '库存记录ID',
- product_id INT NOT NULL COMMENT '商品ID',
- warehouse_id INT NOT NULL COMMENT '库房ID',
- quantity INT NOT NULL DEFAULT 0 COMMENT '库存数量',
- last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
- FOREIGN KEY (product_id) REFERENCES products(product_id),
- FOREIGN KEY (warehouse_id) REFERENCES warehouses(warehouse_id) );
-
-
-
-
- INSERT INTO products (product_name, product_category, specification, unit_price, stock_quantity)
- VALUES ('iPhone 13', '电子产品', '6.1英寸, 5G', 999.99, 100);
- INSERT INTO products (product_name, product_category, specification, unit_price, stock_quantity)
- VALUES ('MacBook Pro', '电子产品', '16英寸, M1芯片', 2499.99, 50);
- INSERT INTO products (product_name, product_category, specification, unit_price, stock_quantity)
- VALUES ('Nike Air Max', '运动鞋', '男款, 42码', 149.99, 200);
- INSERT INTO products (product_name, product_category, specification, unit_price, stock_quantity)
- VALUES ('Adidas Ultra Boost', '运动鞋', '女款, 38码', 129.99, 150);
- INSERT INTO products (product_name, product_category, specification, unit_price, stock_quantity)
- VALUES ('Sony WH-1000XM4', '音频设备', '无线蓝牙耳机, 降噪', 349.99, 80);
- INSERT INTO warehouses (warehouse_name, location, area)
- VALUES ('一号仓库', '北京市朝阳区', 1000.50);
- INSERT INTO warehouses (warehouse_name, location, area)
- VALUES ('二号仓库', '上海市浦东区', 1500.25);
- INSERT INTO warehouses (warehouse_name, location, area)
- VALUES ('三号仓库', '广州市天河区', 800.75);
- INSERT INTO warehouses (warehouse_name, location, area)
- VALUES ('四号仓库', '深圳市南山区', 1200.00);
- INSERT INTO warehouses (warehouse_name, location, area)
- VALUES ('五号仓库', '杭州市西湖区', 950.30);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('张三', '销售部', '库存管理', 500);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('李四', '物流部', '库存盘点', 300);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('王五', '生产部', '库存补货', 800);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('赵六', '财务部', '库存成本核算', NULL);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('孙七', '技术部', '库存系统维护', NULL);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('周八', '销售部', '库存管理', 650);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('吴九', '采购部', '库存采购', 400);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('郑十', '物流部', '库存出库', 250);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('陈十一', '生产部', '库存监控', 900);
- INSERT INTO employees (employee_name, department, inventory_responsibility, inventory_count)
- VALUES ('卫十二', '行政部', NULL, NULL);
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (1, 1, 1, 'In', 10); -- 假设产品ID为1,仓库ID为1,员工ID为1,入库10个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (2, 1, 2, 'Out', 5); -- 假设产品ID为2,仓库ID为1,员工ID为2,出库5个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (1, 2, 3, 'In', 8); -- 假设产品ID为1,仓库ID为2,员工ID为3,入库8个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (3, 2, 4, 'Out', 3); -- 假设产品ID为3,仓库ID为2,员工ID为4,出库3个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (2, 3, 5, 'In', 15); -- 假设产品ID为2,仓库ID为3,员工ID为5,入库15个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (1, 3, 6, 'Out', 7); -- 假设产品ID为1,仓库ID为3,员工ID为6,出库7个产品
- INSERT INTO inventory_transactions (product_id, warehouse_id, employee_id, transaction_type, quantity)
- VALUES (4, 1, 7, 'In', 20); -- 假设产品ID为4,仓库ID为1,员工ID为7,入库20个产品
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商A', '张三', '1234567890', 'supplierA@example.com', '北京市朝阳区');
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商B', '李四', '0987654321', 'supplierB@example.com', '上海市黄浦区');
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商C', '王五', '1112223333', 'supplierC@example.com', '广州市天河区');
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商D', '赵六', '2223334444', 'supplierD@example.com', '深圳市南山区');
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商E', '孙七', '3334445555', 'supplierE@example.com', '杭州市西湖区');
- INSERT INTO suppliers (supplier_name, contact_person, phone_number, email, address)
- VALUES ('供应商F', '周八', '4445556666', 'supplierF@example.com', '成都市武侯区');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('王五', '2023-01-03', 220.75, 'Delivered');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('赵六', '2023-01-04', 75.25, 'Pending');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('孙七', '2023-01-05', 300.00, 'Shipped’);
- INSERT INTO orders (customer_name, order_d
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('张三', '2023-01-01', 100.00, 'Pending');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('李四', '2023-01-02', 150.50, 'Shipped');
- ate, total_amount, status)
- VALUES ('周八', '2023-01-06', 125.50, 'Delivered');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('吴九', '2023-01-07', 80.00, 'Cancelled');
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('陈十', '2023-01-08', 180.25, 'Pending’);
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES ('郑十一', '2023-01-09', 250.00, 'Shipped');
- INSERT INTO order_details (order_id, product_id, quantity, unit_price)
- VALUES (6, 1001, 1, 50.00);
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (1, '2023-01-01', 500.00, 'Pending');
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (2, '2023-01-05', 700.50, 'Pending');
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (1, '2023-01-10', 300.25, 'Received');
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (3, '2023-01-15', 800.00, 'Pending');
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (2, '2023-01-20', 650.75, 'Received');
- INSERT INTO purchase_orders (supplier_id, purchase_date, total_amount, status)
- VALUES (1, '2023-01-25', 400.00, 'Cancelled’);
- INSERT INTO inventory (product_id, warehouse_id, quantity)
- VALUES (1001, 1, 10);
- INSERT INTO inventory (product_id, warehouse_id, quantity)
- VALUES (1002, 2, 5);
- INSERT INTO inventory (product_id, warehouse_id, quantity)
- VALUES (1003, 1, 20);
- INSERT INTO inventory (product_id, warehouse_id, quantity)
- VALUES (1004, 3, 8);
- INSERT INTO inventory (product_id, warehouse_id, quantity)
- VALUES (1001, 2, 15);
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('adminuser1', 'encrypted_password1', 'adminuser1@example.com', '1234567890', 'Admin');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('standarduser1', 'encrypted_password2', 'standarduser1@example.com', '0987654321', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('alice', 'encrypted_password3', 'alice@alice.com', '5551234567', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('bob', 'encrypted_password4', 'bob@bob.com', '5557654321', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('charlie', 'encrypted_password5', 'charlie@charlie.com', '5556543210', 'Admin');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('david', 'encrypted_password6', 'david@david.com', '5551112222', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('eve', 'encrypted_password7', 'eve@example.net', '5559876543', 'Admin');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('frank', 'encrypted_password8', 'frank@example.org', '5553334444', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('george', 'encrypted_password9', 'george@george.com', '5558887777', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('helen', 'encrypted_password10', 'helen@helen.com', '5555678901', 'Admin');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('ian', 'encrypted_password11', 'ian@ian.com', '5552345678', 'User');
- INSERT INTO users (username, password, email, phone_number, role)
- VALUES ('jane', 'encrypted_password12', 'jane@jane.com', '5559012345', 'User’);
- INSERT INTO return_orders (order_id, customer_name, return_date, status)
- VALUES (1001, 'John Doe', '2023-01-01', 'Pending');
- INSERT INTO return_orders (order_id, customer_name, return_date, status)
- VALUES (1002, 'Jane Smith', '2023-01-05', 'Refunded');
- INSERT INTO return_orders (order_id, customer_name, return_date, status)
- VALUES (1003, 'Bob Johnson', '2023-01-10', 'Rejected');
- INSERT INTO return_orders (order_id, customer_name, return_date, status)
- VALUES (1004, 'Alice Brown', '2023-01-15', 'Pending');
- INSERT INTO return_orders (order_id, customer_name, return_date, status)
- VALUES (1005, 'Mike Williams', '2023-01-20', 'Refunded');
- INSERT INTO return_details (return_id, product_id, quantity)
- VALUES (1, 1001, 2); -- 假设退货单ID为1,商品ID为1001,退货数量为2
- INSERT INTO return_details (return_id, product_id, quantity)
- VALUES (1, 1002, 1); -- 假设退货单ID为1,商品ID为1002,退货数量为1
- INSERT INTO return_details (return_id, product_id, quantity)
- VALUES (2, 1001, 3); -- 假设退货单ID为2,商品ID为1001,退货数量为3
- INSERT INTO return_details (return_id, product_id, quantity)
- VALUES (2, 1003, 1); -- 假设退货单ID为2,商品ID为1003,退货数量为1
- INSERT INTO return_details (return_id, product_id, quantity)
- VALUES (3, 1004, 2); -- 假设退货单ID为3,商品ID为1004,退货数量为2
- --基础查询:
- SELECT username AS '姓名', phone_number AS '手机号' FROM users;
- --模糊查询:
- CREATE INDEX idx_product_name ON products(product_name);
- --统计用户订单信息,查询所有用户的下单数量,并进行倒序排列
- SELECT customer_name, COUNT(order_id) AS order_count FROM orders GROUP BY customer_name ORDER BY order_count DESC;
- --查询用户的基本信息使用多表联合查询
- SELECT user_id, username, email, phone_number, role FROM users;
- --查看订单中下单最多的产品对应的类别
- SELECT p.product_category, SUM(od.quantity) AS total_quantity FROM products p JOIN order_details od ON p.product_id = od.product_id GROUP BY p.product_category ORDER BY total_quantity DESC LIMIT 1;
- --查询下单总金额最多的用户
- SELECT u.*
- FROM users u
- JOIN (
- SELECT customer_name, SUM(total_amount) AS total_spent
- FROM orders
- GROUP BY customer_name
- ORDER BY total_spent DESC
- LIMIT 1
- ) AS top_spender ON u.username = top_spender.customer_name;
- -- 触发器:当新增用户时,在inventory_transactions表中为该用户创建一个初始的库存记录(假设)
- DELIMITER //
- CREATE TRIGGER after_user_insert
- AFTER INSERT ON users
- FOR EACH ROW
- BEGIN
- INSERT INTO inventory_transactions (employee_id, transaction_type, quantity, transaction_date)
- VALUES (NEW.user_id, 'In', 0, NOW()); -- 这里假设员工ID和用户ID是相同的,并且只是作为一个演示,数量为0
- END;
- //
- DELIMITER ;
- --产品表修改语句添加触发器
- DELIMITER //
- CREATE TRIGGER before_product_update
- BEFORE UPDATE ON products
- FOR EACH ROW
- BEGIN
- IF NEW.unit_price < OLD.unit_price * 0.9 OR NEW.unit_price > OLD.unit_price * 1.1 THEN
- SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '商品售价上下浮动不能超过10%';
- END IF;
- END;
- //
- DELIMITER ;
- -- 触发器:当删除订单时,先删除订单详情表中的相关记录
- DELIMITER //
- CREATE TRIGGER before_order_delete
- BEFORE DELETE ON orders
- FOR EACH ROW
- BEGIN
- DELETE FROM order_details WHERE order_id = OLD.order_id;
- END;
- //
- DELIMITER ;
- --查询产品库存是否符合所需产品数量,并统计订单总金额
- DELIMITER //
- CREATE PROCEDURE create_order_infos(
- IN p_customer_name VARCHAR(255),
- IN p_product_id INT,
- IN p_quantity INT,
- IN p_warehouse_id INT )
- BEGIN
- -- 声明变量
- DECLARE v_order_id INT;
- DECLARE v_product_stock INT;
- DECLARE v_remaining_stock INT;
- -- 开始事务
- START TRANSACTION;
- -- 1. 插入订单记录到orders表
- INSERT INTO orders (customer_name, order_date, total_amount, status)
- VALUES (p_customer_name, NOW(), 0, 'Pending');
- -- 获取新订单的ID
- SET v_order_id = LAST_INSERT_ID();
- -- 2. 检查产品库存是否足够
- SELECT quantity INTO v_product_stock FROM inventory WHERE product_id = p_product_id AND warehouse_id = p_warehouse_id FOR UPDATE;
- IF v_product_stock >= p_quantity THEN
- -- 3. 更新库存数量
- SET v_remaining_stock = v_product_stock - p_quantity;
- UPDATE inventory SET quantity = v_remaining_stock WHERE product_id = p_product_id AND warehouse_id = p_warehouse_id;
- -- 假设这里还需要更新产品表的库存(不常见,但为了示例)
- -- UPDATE products SET stock_quantity = stock_quantity - p_quantity WHERE product_id = p_product_id;
- -- 4. 插入订单明细到order_details表
- INSERT INTO order_details (order_id, product_id, quantity, unit_price)
- SELECT v_order_id, p.product_id, p_quantity, p.unit_price
- FROM products p
- WHERE p.product_id = p_product_id;
- -- 5. 计算订单总金额并更新orders表(假设单价不会变)
- UPDATE orders o
- JOIN (
- SELECT order_id, SUM(quantity * unit_price) AS total_amount
- FROM order_details
- WHERE order_id = v_order_id
- ) od ON o.order_id = od.order_id
- SET o.total_amount = od.total_amount
- WHERE o.order_id = v_order_id;
- -- 提交事务
- COMMIT;
- SELECT 'Order created successfully.' AS message;
- ELSE
- -- 库存不足,回滚事务
- ROLLBACK;
- SELECT 'Insufficient stock for the order.' AS message;
- END IF;
- END //
-
- DELIMITER ;
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。