MySQL实训项目——餐饮点餐系统

07-02 1779阅读

       项目简介:餐饮点餐系统是一款为餐厅和顾客提供便捷点餐服务的在线平台。通过该系统,餐厅能够展示其菜单,顾客可以浏览菜品,并将其加入购物车或直接下单。系统还提供了订单管理功能,方便餐厅跟踪和处理顾客的订单。

1. 建表DDL

CREATE TABLE `carts` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车ID',
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `restaurant_id` int(11) NOT NULL COMMENT '餐厅ID',
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `restaurant_id` (`restaurant_id`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COMMENT='购物车表';
CREATE TABLE `cart_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车项ID',
  `cart_id` int(11) NOT NULL COMMENT '购物车ID',
  `dish_id` int(11) NOT NULL COMMENT '菜品ID',
  `quantity` int(11) NOT NULL COMMENT '数量',
  PRIMARY KEY (`id`),
  KEY `cart_id` (`cart_id`),
  KEY `dish_id` (`dish_id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT='购物车项表';
CREATE TABLE `dishes` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品ID',
  `name` varchar(100) NOT NULL COMMENT '菜品名称',
  `description` text COMMENT '菜品描述',
  `price` decimal(10,2) NOT NULL COMMENT '菜品价格',
  `category_id` int(11) NOT NULL COMMENT '所属分类ID',
  `restaurant_id` int(11) NOT NULL COMMENT '所属餐厅ID',
  PRIMARY KEY (`id`),
  KEY `category_id` (`category_id`),
  KEY `restaurant_id` (`restaurant_id`)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COMMENT='菜品表';
CREATE TABLE `dish_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品分类ID',
  `name` varchar(50) NOT NULL COMMENT '分类名称',
  `restaurant_id` int(11) NOT NULL COMMENT '所属餐厅ID',
  PRIMARY KEY (`id`),
  KEY `restaurant_id` (`restaurant_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='菜品分类表';
CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `restaurant_id` int(11) DEFAULT NULL COMMENT '餐厅ID',
  `order_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单日期',
  `total_price` decimal(10,2) DEFAULT '0.00' COMMENT '订单总价',
  `status` enum('待支付','已支付','已取消','已完成') DEFAULT '待支付' COMMENT '订单状态',
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `restaurant_id` (`restaurant_id`)
) ENGINE=MyISAM AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 COMMENT='订单表';
CREATE TABLE `order_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单项ID',
  `order_id` int(11) NOT NULL COMMENT '订单ID',
  `dish_id` int(11) NOT NULL COMMENT '菜品ID',
  `quantity` int(11) NOT NULL COMMENT '数量',
  `price_per_item` decimal(10,2) NOT NULL COMMENT '单价',
  PRIMARY KEY (`id`),
  KEY `order_id` (`order_id`),
  KEY `dish_id` (`dish_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='订单项表';
CREATE TABLE `restaurants` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '餐厅ID',
  `name` varchar(100) NOT NULL COMMENT '餐厅名称',
  `address` varchar(255) NOT NULL COMMENT '餐厅地址',
  `opening_hours` varchar(50) DEFAULT NULL COMMENT '营业时间',
  `contact_number` varchar(20) DEFAULT NULL COMMENT '联系电话',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='餐厅表';
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(255) NOT NULL COMMENT '密码',
  `email` varchar(100) DEFAULT NULL COMMENT '邮箱地址',
  `gender` enum('男','女') NOT NULL COMMENT '性别',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话号码',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 COMMENT='用户表';

2. 插入数据(DML)

INSERT INTO `carts` VALUES ('1', '1', '1');
INSERT INTO `carts` VALUES ('2', '1', '2');
INSERT INTO `carts` VALUES ('3', '2', '4');
INSERT INTO `carts` VALUES ('4', '3', '3');
INSERT INTO `carts` VALUES ('5', '3', '1');
INSERT INTO `carts` VALUES ('6', '4', '1');
INSERT INTO `carts` VALUES ('7', '8', '2');
INSERT INTO `carts` VALUES ('8', '5', '1');
INSERT INTO `carts` VALUES ('9', '3', '1');
INSERT INTO `carts` VALUES ('10', '1', '1');
INSERT INTO `carts` VALUES ('11', '9', '1');
INSERT INTO `carts` VALUES ('12', '10', '8');
INSERT INTO `carts` VALUES ('13', '8', '6');
INSERT INTO `carts` VALUES ('14', '16', '4');
INSERT INTO `carts` VALUES ('15', '4', '6');
INSERT INTO `carts` VALUES ('16', '11', '5');
INSERT INTO `carts` VALUES ('17', '12', '8');
INSERT INTO `carts` VALUES ('18', '9', '1');
INSERT INTO `cart_items` VALUES ('1', '1', '1', '2');
INSERT INTO `cart_items` VALUES ('2', '1', '2', '1');
INSERT INTO `cart_items` VALUES ('3', '2', '4', '1');
INSERT INTO `cart_items` VALUES ('4', '2', '5', '3');
INSERT INTO `cart_items` VALUES ('5', '2', '1', '7');
INSERT INTO `cart_items` VALUES ('6', '1', '7', '2');
INSERT INTO `cart_items` VALUES ('7', '3', '3', '1');
INSERT INTO `cart_items` VALUES ('8', '5', '6', '1');
INSERT INTO `cart_items` VALUES ('9', '6', '2', '7');
INSERT INTO `cart_items` VALUES ('10', '6', '2', '6');
INSERT INTO `cart_items` VALUES ('11', '4', '7', '3');
INSERT INTO `cart_items` VALUES ('12', '8', '1', '3');
INSERT INTO `cart_items` VALUES ('13', '5', '3', '9');
INSERT INTO `cart_items` VALUES ('14', '9', '3', '1');
INSERT INTO `cart_items` VALUES ('15', '8', '1', '0');
INSERT INTO `cart_items` VALUES ('16', '2', '4', '6');
INSERT INTO `dishes` VALUES ('1', '水煮鱼', '麻辣鲜香,回味无穷', '58.00', '1', '1');
INSERT INTO `dishes` VALUES ('2', '宫保鸡丁', '色泽红亮,口感鲜美', '48.00', '1', '1');
INSERT INTO `dishes` VALUES ('3', '麻婆豆腐', '麻辣可口,下饭佳品', '38.00', '1', '1');
INSERT INTO `dishes` VALUES ('4', '白切鸡', '皮爽肉滑,鲜美无比', '68.00', '2', '2');
INSERT INTO `dishes` VALUES ('5', '清蒸鲈鱼', '鲜嫩可口,营养丰富', '78.00', '2', '2');
INSERT INTO `dishes` VALUES ('6', '菠萝咕噜肉', '酸甜可口,色泽诱人', '52.00', '2', '2');
INSERT INTO `dishes` VALUES ('7', '剁椒鱼头', '香辣可口,回味无穷', '62.00', '3', '3');
INSERT INTO `dishes` VALUES ('8', '辣椒炒肉', '香辣可口,下饭佳品', '42.00', '3', '3');
INSERT INTO `dishes` VALUES ('9', '红烧肉', '肥而不腻,入口即化', '55.00', '3', '3');
INSERT INTO `dishes` VALUES ('10', '扣碗小酥肉', '口感鲜嫩,色泽诱人', '56.00', '6', '4');
INSERT INTO `dishes` VALUES ('11', '开水白菜', '香味醇厚,汤味浓厚', '199.00', '4', '3');
INSERT INTO `dishes` VALUES ('12', '夫妻肺片', '色泽美观,质嫩鲜美', '89.00', '5', '9');
INSERT INTO `dishes` VALUES ('13', '鸡豆花', '吃鸡不见鸡,吃肉不见肉', '88.00', '3', '7');
INSERT INTO `dishes` VALUES ('14', '九转大肠', '色泽红润,质地软嫩', '199.00', '7', '4');
INSERT INTO `dishes` VALUES ('15', '爆炒腰花', '鲜嫩,润滑不腻', '189.00', '6', '4');
INSERT INTO `dishes` VALUES ('16', '白切鸡', '形状美观,原汁原味', '189.00', '2', '2');
INSERT INTO `dishes` VALUES ('17', '红烧乳鸽', '皮脆,肉滑,骨嫩,多汁', '88.00', '1', '1');
INSERT INTO `dish_categories` VALUES ('1', '川菜', '1');
INSERT INTO `dish_categories` VALUES ('2', '粤菜', '2');
INSERT INTO `dish_categories` VALUES ('3', '湘菜', '3');
INSERT INTO `dish_categories` VALUES ('4', '鲁菜', '1');
INSERT INTO `dish_categories` VALUES ('5', '苏菜', '2');
INSERT INTO `dish_categories` VALUES ('6', '浙菜', '3');
INSERT INTO `dish_categories` VALUES ('7', '闽菜', '5');
INSERT INTO `dish_categories` VALUES ('8', '徽菜', '8');
INSERT INTO `dish_categories` VALUES ('9', '湘菜', '7');
INSERT INTO `orders` VALUES ('1', '1', '1', '2024-06-27 13:59:06', '99.00', '待支付');
INSERT INTO `orders` VALUES ('5', '6', '4', '2024-06-27 17:47:18', '328.00', '待支付');
INSERT INTO `orders` VALUES ('4', '5', '2', '2024-06-27 17:44:19', '1100.00', '待支付');
INSERT INTO `orders` VALUES ('6', '7', '5', '2024-06-28 10:05:38', '43.00', '待支付');
INSERT INTO `orders` VALUES ('7', '8', '1', '2024-06-28 10:12:14', '159.00', '已支付');
INSERT INTO `orders` VALUES ('8', '9', '8', '2024-06-28 10:13:58', '190.00', '待支付');
INSERT INTO `orders` VALUES ('9', '10', '1', '2024-06-28 10:14:51', '126.00', '待支付');
INSERT INTO `orders` VALUES ('10', '11', '3', '2024-06-28 10:15:50', '112.00', '待支付');
INSERT INTO `orders` VALUES ('11', '12', '6', '2024-06-28 10:16:46', '442.00', '待支付');
INSERT INTO `orders` VALUES ('12', '13', '2', '2024-06-28 10:17:45', '45.00', '待支付');
INSERT INTO `orders` VALUES ('13', '14', '3', '2024-06-28 10:19:02', '123.00', '已支付');
INSERT INTO `orders` VALUES ('14', '15', '4', '2024-06-28 10:19:46', '232.00', '已支付');
INSERT INTO `orders` VALUES ('15', '16', '7', '2024-06-28 10:20:43', '565.00', '待支付');
INSERT INTO `orders` VALUES ('16', '17', '2', '2024-06-28 10:21:23', '123.00', '已支付');
INSERT INTO `orders` VALUES ('17', '18', '3', '2024-06-28 10:22:20', '563.00', '待支付');
INSERT INTO `orders` VALUES ('18', '19', '1', '2024-06-28 10:23:36', '213.00', '已取消');
INSERT INTO `orders` VALUES ('19', '20', '2', '2024-06-28 10:24:41', '123.00', '待支付');
INSERT INTO `orders` VALUES ('20', '21', '4', '2024-06-28 10:25:05', '13.00', '待支付');
INSERT INTO `orders` VALUES ('21', '22', '7', '2024-06-28 10:26:09', '123.00', '待支付');
INSERT INTO `orders` VALUES ('22', '23', '4', '2024-06-28 10:30:40', '112.00', '待支付');
INSERT INTO `orders` VALUES ('23', '11', '8', '2024-06-28 10:30:42', '762.00', '待支付');
INSERT INTO `orders` VALUES ('24', '12', '5', '2024-06-28 10:30:54', '199.00', '待支付');
INSERT INTO `orders` VALUES ('25', '5', '6', '2024-06-28 10:30:55', '192.00', '待支付');
INSERT INTO `orders` VALUES ('26', '2', '4', '2024-06-28 10:30:56', '172.00', '待支付');
INSERT INTO `orders` VALUES ('27', '1', '2', '2024-06-28 10:30:57', '234.00', '待支付');
INSERT INTO `orders` VALUES ('28', '7', '5', '2024-06-28 10:30:58', '423.00', '待支付');
INSERT INTO `orders` VALUES ('29', '13', '10', '2024-06-28 10:31:00', '2312.00', '待支付');
INSERT INTO `orders` VALUES ('30', '14', '3', '2024-06-28 10:31:01', '123.00', '已取消');
INSERT INTO `orders` VALUES ('31', '4', '3', '2024-06-28 10:31:03', '123.00', '待支付');
INSERT INTO `orders` VALUES ('32', '6', '6', '2024-06-28 10:31:04', '1534.00', '待支付');
INSERT INTO `orders` VALUES ('33', '8', '17', '2024-06-28 10:31:06', '2314.00', '待支付');
INSERT INTO `orders` VALUES ('34', '3', '2', '2024-06-28 10:31:07', '213.00', '待支付');
INSERT INTO `orders` VALUES ('35', '7', '7', '2024-06-28 10:31:09', '872.00', '已取消');
INSERT INTO `orders` VALUES ('36', '9', '1', '2024-06-28 10:31:10', '69.00', '待支付');
INSERT INTO `orders` VALUES ('37', '10', '3', '2024-06-28 10:31:11', '199.00', '待支付');
INSERT INTO `orders` VALUES ('38', '13', '1', '2024-06-28 10:31:12', '32.00', '已支付');
INSERT INTO `orders` VALUES ('39', '11', '4', '2024-06-28 10:31:13', '234.00', '待支付');
INSERT INTO `orders` VALUES ('40', '14', '1', '2024-06-28 10:31:15', '44.00', '已取消');
INSERT INTO `orders` VALUES ('41', '16', '1', '2024-06-28 10:31:16', '32.00', '待支付');
INSERT INTO `orders` VALUES ('42', '13', '1', '2024-06-28 10:51:13', '34.00', '待支付');
INSERT INTO `order_items` VALUES ('1', '1', '1', '2', '58.00');
INSERT INTO `order_items` VALUES ('2', '1', '2', '1', '48.00');
INSERT INTO `order_items` VALUES ('3', '2', '2', '1', '99.00');
INSERT INTO `order_items` VALUES ('4', '4', '2', '4', '199.00');
INSERT INTO `order_items` VALUES ('5', '3', '6', '2', '169.00');
INSERT INTO `order_items` VALUES ('6', '6', '1', '5', '1100.00');
INSERT INTO `order_items` VALUES ('7', '5', '3', '1', '99.00');
INSERT INTO `order_items` VALUES ('8', '2', '1', '6', '499.00');
INSERT INTO `order_items` VALUES ('9', '3', '1', '5', '1099.00');
INSERT INTO `restaurants` VALUES ('1', '江湖酒楼', '京城大街1号', '09:00-22:00', '12345678');
INSERT INTO `restaurants` VALUES ('2', '美味轩', '长安路88号', '10:00-21:30', '87654321');
INSERT INTO `restaurants` VALUES ('3', '清风阁', '西湖路123号', '11:00-23:00', '98765432');
INSERT INTO `restaurants` VALUES ('4', '湘味阁', '南京路338', '3:00-3:00', '32809000');
INSERT INTO `restaurants` VALUES ('5', '百香馆', '长安街1号', '8:00-23:30', '87892747');
INSERT INTO `restaurants` VALUES ('6', '味里香', '信安大厦770号', '9:00-23:00', '37182173');
INSERT INTO `restaurants` VALUES ('7', '鲁香楼', '城南花苑824号', '10:00-10:00', '73731389');
INSERT INTO `restaurants` VALUES ('8', '西湖谷味', '西湖路188号', '12:00-3:00', '17362172');
INSERT INTO `restaurants` VALUES ('9', '木叶谷', '长江大道990号', '13:00-23:00', '72838381');
INSERT INTO `restaurants` VALUES ('10', '烟火味道', '梧桐大道779号', '7:00-17:00', '82173261');
INSERT INTO `restaurants` VALUES ('11', '川上人家', '川南小街990号', '24:00', '89232781');
INSERT INTO `restaurants` VALUES ('12', '雅寻名苑', '寻南路889号', '17:00-4:00', '72182373');
INSERT INTO `restaurants` VALUES ('13', '一品鱼乡', '江南小镇770号', '8:00-23:30', '81276232');
INSERT INTO `users` VALUES ('1', '小鱼儿', 'xiaoyu_pass', 'xiaoyu@example.com', '男', '15273663822');
INSERT INTO `users` VALUES ('2', '花无缺', 'huawu_pass', 'huawu@example.com', '男', '17532698837');
INSERT INTO `users` VALUES ('3', '苏樱', 'suying_pass', 'suying@example.com', '女', '16728366253');
INSERT INTO `users` VALUES ('4', '散兵', 'sanbing', 'sanbing@example.com', '男', '15738826639');
INSERT INTO `users` VALUES ('5', '管容祖', 'rongzu_pass', 'rongzu@example.com', '女', '15632782993');
INSERT INTO `users` VALUES ('6', '荣祖管', 'zuguan_pass', 'zurong@example.com', '男', '17644552789');
INSERT INTO `users` VALUES ('7', '张三', 'zhangsan_pass', 'zhangsan@example.com', '男', '17237368137');
INSERT INTO `users` VALUES ('8', '李三', 'lisi_pass', 'lisi@example.com', '男', '16273828372');
INSERT INTO `users` VALUES ('9', '李四', 'lisi_pass', 'lisi@examole.com', '女', '17261537183');
INSERT INTO `users` VALUES ('10', '刘五', 'liuwu_Pass', 'liuwu@example.com', '男', '15624416819');
INSERT INTO `users` VALUES ('11', '张先生', 'zhangxiansheng_pass', 'zhangxiansheng@example.com', '男', '18862715218');
INSERT INTO `users` VALUES ('12', '刘先生', 'liu_pass', 'liuxiansheng@example', '男', '19173762517');
INSERT INTO `users` VALUES ('13', '甄嬛', 'zhenhuan_pass', 'zhenhaun@examaple', '女', '18374625174');
INSERT INTO `users` VALUES ('14', '四郎', 'silang_pass', 'silang@example', '男', '18937253518');
INSERT INTO `users` VALUES ('15', '沈梅庄', 'shen_pass', 'meizhuang@example', '女', '19347264628');
INSERT INTO `users` VALUES ('16', '华妃', 'hua_pass', 'huafei@example', '女', '18427461731');
INSERT INTO `users` VALUES ('17', '皇后', 'huanghou_pass', 'huanghou@ashdascm.com', '女', '17637512653');
INSERT INTO `users` VALUES ('18', '浣碧', 'huanbi_pass', 'huanbi@example.com', '女', '15276837683');
INSERT INTO `users` VALUES ('19', '太后', 'taihou_pass', 'taihou@example.com', '女', '18631763837');
INSERT INTO `users` VALUES ('20', '齐妃', 'qifei_pass', 'qifei@example.com', '女', '17649279127');
INSERT INTO `users` VALUES ('21', '祺贵人', 'qiguiren_pass', 'qiguiren@example.com', '女', '13842947274');
INSERT INTO `users` VALUES ('22', '叶澜依', 'yelanyi_pass', 'lanyi@example.com', '女', '14723727382');

3. 简单查询

# 简单查询
# 1. 查询所有菜品名称和价格:
SELECT name, price FROM dishes;
# 2. 查询所有餐厅的名称和地址:
SELECT name, address FROM restaurants;
# 3. 查询所有用户的用户名和性别:
SELECT username, gender FROM users;

效果:

MySQL实训项目——餐饮点餐系统

MySQL实训项目——餐饮点餐系统 

MySQL实训项目——餐饮点餐系统 

4. 多表联合复杂查询 

#多表联合复杂查询
# 1. 查询用户名为“小鱼儿”的用户的购物车中所有菜品的信息:
SELECT dishes.name, dishes.price, cart_items.quantity
FROM users
JOIN carts ON users.id = carts.user_id
JOIN cart_items ON carts.id = cart_items.cart_id
JOIN dishes ON cart_items.dish_id = dishes.id
WHERE users.username = '小鱼儿';
# 2. 查询所有订单中包含“水煮鱼”的订单信息,包括订单号、用户名、订单日期和订单总价:
SELECT orders.id, users.username, orders.order_date, orders.total_price
FROM orders
JOIN order_items ON orders.id = order_items.order_id
JOIN users ON orders.user_id = users.id
JOIN dishes ON order_items.dish_id = dishes.id
WHERE dishes.name = '水煮鱼';
# 3. 查询每个餐厅的菜品数量:
SELECT restaurants.name, COUNT(dishes.id) as dish_count
FROM restaurants
LEFT JOIN dishes ON restaurants.id = dishes.restaurant_id
GROUP BY restaurants.id;
# 4.查询每个用户在购物车中添加的菜品总数量:
SELECT users.username, SUM(cart_items.quantity) as total_quantity
FROM users
JOIN carts ON users.id = carts.user_id
JOIN cart_items ON carts.id = cart_items.cart_id
GROUP BY users.id;
# 5. 查询每个餐厅的订单总金额:
SELECT restaurants.name, SUM(orders.total_price) as total_revenue
FROM orders
JOIN restaurants ON orders.restaurant_id = restaurants.id
GROUP BY orders.restaurant_id;

效果:

MySQL实训项目——餐饮点餐系统

MySQL实训项目——餐饮点餐系统 

MySQL实训项目——餐饮点餐系统 

MySQL实训项目——餐饮点餐系统 

MySQL实训项目——餐饮点餐系统 

5. 触发器 插入-修改-删除 

create trigger after_inster_user -- 创建触发器的名称
after insert on `users` -- 用户表执行插入操作之后被出发的触发器
for each row -- 每行数据都要进行触发
begin
    INSERT INTO orders (user_id, total_price)  -- 在orders表中插入一行数据
     VALUES (NEW.id, 0.00);  -- 使用NEW.id作为新插入行的user_id,total_price为0.00
end ;
$$
delimiter ; -- 结束$$的有效性更换成;
insert into users values (0,'散兵','sanbing','sanbing','男','99999999999')
-- 删除触发器
DROP TRIGGER IF EXISTS `after_delete_user`;
DELIMITER ;;
CREATE TRIGGER `after_delete_user` AFTER DELETE ON `users` FOR EACH ROW
BEGIN
    -- 删除与该用户相关的订单
    DELETE FROM `orders` WHERE `user_id` = OLD.id;
END
;;
DELIMITER ;
-- 更新触发器
DROP TRIGGER IF EXISTS `after_update_user`;
DELIMITER ;;
CREATE TRIGGER `after_update_user` AFTER UPDATE ON `users` FOR EACH ROW
BEGIN
    -- 更新与该用户相关的订单中的某些信息(例如,更新订单中的用户名)
    UPDATE `orders` SET `user_id` = NEW.id WHERE `user_id` = OLD.id;
END
;;
DELIMITER ;

6. 创建存储过程

DELIMITER //
CREATE PROCEDURE CreateOrder(
    IN p_user_id INT,
    IN p_restaurant_id INT,
    IN p_dish_id INT,
    IN p_quantity INT
)
BEGIN
    DECLARE v_order_id INT;
    DECLARE v_price_per_item DECIMAL(10,2);
    -- 获取菜品价格
    SELECT price INTO v_price_per_item FROM dishes WHERE id = p_dish_id;
    -- 创建新订单
    INSERT INTO orders (id,user_id, restaurant_id,order_date, total_price,status)
    VALUES (22,6,1,'2024-06-27 17:47:18,880','已支付');
    -- 获取新插入订单的ID
    SET v_order_id = LAST_INSERT_ID();
    -- 插入订单项
    INSERT INTO order_items (order_id, dish_id, quantity, price_per_item)
    VALUES (v_order_id, p_dish_id, p_quantity, v_price_per_item);
    -- 更新订单总价
    UPDATE orders
    SET total_price = (SELECT SUM(price_per_item * quantity) FROM order_items WHERE order_id = v_order_id)
    WHERE id = v_order_id;
END //
DELIMITER ;
 -- CALL CreateOrder(1, 1,1 ,99 );

       本项目实现了餐饮点餐系统的基本功能,包括菜品浏览、购物车管理、订单处理等,达到了预期目标。在项目中我们遇到了数据库优化、并发控制等技术难题,通过查阅资料和团队讨论,成功找到解决方案。未来我们计划对餐饮点餐系统进行优化升级,增加用户评价功能,提高用户体验,同时考虑移动端的适配问题。

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]