• 餐饮点餐的简单MySQL集合


    ER图

    模型图(没有进行排序,混乱) 

     

     DDL和DML

    1. /*
    2. Navicat MySQL Data Transfer
    3. Source Server : Mylink
    4. Source Server Version : 50726
    5. Source Host : localhost:3306
    6. Source Database : schooldb
    7. Target Server Type : MYSQL
    8. Target Server Version : 50726
    9. File Encoding : 65001
    10. Date: 2024-06-28 00:49:34
    11. */
    12. SET FOREIGN_KEY_CHECKS=0;
    13. -- ----------------------------
    14. -- Table structure for `carts`
    15. -- ----------------------------
    16. DROP TABLE IF EXISTS `carts`;
    17. CREATE TABLE `carts` (
    18. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车ID',
    19. `user_id` int(11) NOT NULL COMMENT '用户ID',
    20. `restaurant_id` int(11) NOT NULL COMMENT '餐厅ID',
    21. PRIMARY KEY (`id`) USING BTREE,
    22. KEY `user_id` (`user_id`) USING BTREE,
    23. KEY `restaurant_id` (`restaurant_id`) USING BTREE,
    24. CONSTRAINT `carts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
    25. CONSTRAINT `carts_ibfk_2` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`) ON DELETE CASCADE
    26. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='购物车表';
    27. -- ----------------------------
    28. -- Records of carts
    29. -- ----------------------------
    30. INSERT INTO `carts` VALUES ('2', '1', '2');
    31. INSERT INTO `carts` VALUES ('3', '3', '3');
    32. INSERT INTO `carts` VALUES ('4', '5', '5');
    33. INSERT INTO `carts` VALUES ('5', '4', '6');
    34. -- ----------------------------
    35. -- Table structure for `cart_items`
    36. -- ----------------------------
    37. DROP TABLE IF EXISTS `cart_items`;
    38. CREATE TABLE `cart_items` (
    39. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '购物车项ID',
    40. `cart_id` int(11) NOT NULL COMMENT '购物车ID',
    41. `dish_id` int(11) NOT NULL COMMENT '菜品ID',
    42. `quantity` int(11) NOT NULL COMMENT '数量',
    43. PRIMARY KEY (`id`) USING BTREE,
    44. KEY `cart_id` (`cart_id`) USING BTREE,
    45. KEY `dish_id` (`dish_id`) USING BTREE,
    46. CONSTRAINT `cart_items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `carts` (`id`) ON DELETE CASCADE,
    47. CONSTRAINT `cart_items_ibfk_2` FOREIGN KEY (`dish_id`) REFERENCES `dishes` (`id`) ON DELETE CASCADE
    48. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='购物车项表';
    49. -- ----------------------------
    50. -- Records of cart_items
    51. -- ----------------------------
    52. INSERT INTO `cart_items` VALUES ('3', '2', '4', '1');
    53. INSERT INTO `cart_items` VALUES ('4', '2', '5', '3');
    54. INSERT INTO `cart_items` VALUES ('5', '3', '2', '4');
    55. -- ----------------------------
    56. -- Table structure for `dishes`
    57. -- ----------------------------
    58. DROP TABLE IF EXISTS `dishes`;
    59. CREATE TABLE `dishes` (
    60. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品ID',
    61. `name` varchar(100) NOT NULL COMMENT '菜品名称',
    62. `description` text COMMENT '菜品描述',
    63. `price` decimal(10,2) NOT NULL COMMENT '菜品价格',
    64. `category_id` int(11) NOT NULL DEFAULT '1' COMMENT '所属分类ID',
    65. `restaurant_id` int(11) NOT NULL DEFAULT '1' COMMENT '所属餐厅ID',
    66. `stock_quantity` varchar(255) NOT NULL COMMENT '库存数量',
    67. PRIMARY KEY (`id`) USING BTREE,
    68. KEY `category_id` (`category_id`) USING BTREE,
    69. KEY `restaurant_id` (`restaurant_id`) USING BTREE,
    70. CONSTRAINT `dishes_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `dish_categories` (`id`) ON DELETE CASCADE,
    71. CONSTRAINT `dishes_ibfk_2` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`) ON DELETE CASCADE
    72. ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='菜品表';
    73. -- ----------------------------
    74. -- Records of dishes
    75. -- ----------------------------
    76. INSERT INTO `dishes` VALUES ('1', '水煮鱼', '麻辣鲜香,回味无穷', '102.75', '1', '1', '190');
    77. INSERT INTO `dishes` VALUES ('2', '宫保鸡丁', '色泽红亮,口感鲜美', '48.00', '1', '1', '250');
    78. INSERT INTO `dishes` VALUES ('3', '麻婆豆腐', '麻辣可口,下饭佳品', '38.00', '1', '1', '140');
    79. INSERT INTO `dishes` VALUES ('4', '白切鸡', '皮爽肉滑,鲜美无比', '68.00', '2', '2', '300');
    80. INSERT INTO `dishes` VALUES ('5', '清蒸鲈鱼', '鲜嫩可口,营养丰富', '78.00', '2', '2', '420');
    81. INSERT INTO `dishes` VALUES ('6', '菠萝咕噜肉', '酸甜可口,色泽诱人', '52.00', '2', '2', '480');
    82. INSERT INTO `dishes` VALUES ('7', '剁椒鱼头', '香辣可口,回味无穷', '62.00', '3', '3', '500');
    83. INSERT INTO `dishes` VALUES ('8', '辣椒炒肉', '香辣可口,下饭佳品', '42.00', '3', '3', '350');
    84. INSERT INTO `dishes` VALUES ('9', '红烧肉', '肥而不腻,入口即化', '55.00', '3', '3', '250');
    85. INSERT INTO `dishes` VALUES ('16', '大鱼头', null, '55.00', '1', '1', '100');
    86. -- ----------------------------
    87. -- Table structure for `dish_categories`
    88. -- ----------------------------
    89. DROP TABLE IF EXISTS `dish_categories`;
    90. CREATE TABLE `dish_categories` (
    91. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '菜品分类ID',
    92. `name` varchar(50) NOT NULL COMMENT '分类名称',
    93. `restaurant_id` int(11) NOT NULL COMMENT '所属餐厅ID',
    94. PRIMARY KEY (`id`) USING BTREE,
    95. KEY `restaurant_id` (`restaurant_id`) USING BTREE,
    96. CONSTRAINT `dish_categories_ibfk_1` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`) ON DELETE CASCADE
    97. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='菜品分类表';
    98. -- ----------------------------
    99. -- Records of dish_categories
    100. -- ----------------------------
    101. INSERT INTO `dish_categories` VALUES ('1', '川菜', '1');
    102. INSERT INTO `dish_categories` VALUES ('2', '粤菜', '2');
    103. INSERT INTO `dish_categories` VALUES ('3', '湘菜', '3');
    104. INSERT INTO `dish_categories` VALUES ('4', '鲁菜', '1');
    105. INSERT INTO `dish_categories` VALUES ('5', '苏菜', '2');
    106. INSERT INTO `dish_categories` VALUES ('6', '浙菜', '3');
    107. -- ----------------------------
    108. -- Table structure for `orders`
    109. -- ----------------------------
    110. DROP TABLE IF EXISTS `orders`;
    111. CREATE TABLE `orders` (
    112. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单ID',
    113. `user_id` int(11) NOT NULL COMMENT '用户ID',
    114. `restaurant_id` int(11) DEFAULT NULL COMMENT '餐厅ID',
    115. `order_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单日期',
    116. `total_price` decimal(10,2) NOT NULL DEFAULT '1.00' COMMENT '订单总价',
    117. `status` enum('待支付','已支付','已取消','已完成') NOT NULL DEFAULT '待支付' COMMENT '订单状态',
    118. PRIMARY KEY (`id`) USING BTREE,
    119. KEY `user_id` (`user_id`) USING BTREE,
    120. KEY `restaurant_id` (`restaurant_id`) USING BTREE,
    121. CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
    122. CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`) ON DELETE CASCADE
    123. ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单表';
    124. -- ----------------------------
    125. -- Records of orders
    126. -- ----------------------------
    127. INSERT INTO `orders` VALUES ('2', '3', '2', '2024-05-26 00:00:00', '95.00', '待支付');
    128. INSERT INTO `orders` VALUES ('3', '2', '1', '2024-05-27 00:00:00', '75.08', '已完成');
    129. INSERT INTO `orders` VALUES ('4', '5', '3', '2024-06-27 13:34:14', '100.00', '已支付');
    130. INSERT INTO `orders` VALUES ('6', '4', '4', '2024-06-25 08:35:20', '74.00', '已完成');
    131. INSERT INTO `orders` VALUES ('8', '1', '1', '2024-06-27 21:50:29', '116.00', '待支付');
    132. INSERT INTO `orders` VALUES ('9', '1', '1', '2024-06-27 21:50:50', '116.00', '待支付');
    133. INSERT INTO `orders` VALUES ('10', '1', '1', '2024-06-28 00:30:15', '205.50', '待支付');
    134. INSERT INTO `orders` VALUES ('11', '1', '1', '2024-06-28 00:32:01', '205.50', '待支付');
    135. INSERT INTO `orders` VALUES ('12', '2', null, '2024-06-28 00:37:55', '76.00', '待支付');
    136. INSERT INTO `orders` VALUES ('13', '2', null, '2024-06-28 00:38:30', '76.00', '待支付');
    137. INSERT INTO `orders` VALUES ('14', '2', null, '2024-06-28 00:39:34', '76.00', '待支付');
    138. INSERT INTO `orders` VALUES ('15', '2', null, '2024-06-28 00:39:47', '76.00', '待支付');
    139. INSERT INTO `orders` VALUES ('16', '2', null, '2024-06-28 00:47:01', '76.00', '待支付');
    140. -- ----------------------------
    141. -- Table structure for `order_items`
    142. -- ----------------------------
    143. DROP TABLE IF EXISTS `order_items`;
    144. CREATE TABLE `order_items` (
    145. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '订单项ID',
    146. `order_id` int(11) NOT NULL COMMENT '订单ID',
    147. `dish_id` int(11) NOT NULL COMMENT '菜品ID',
    148. `quantity` int(11) NOT NULL COMMENT '数量',
    149. `price_per_item` decimal(10,2) NOT NULL COMMENT '单价',
    150. PRIMARY KEY (`id`) USING BTREE,
    151. KEY `order_id` (`order_id`) USING BTREE,
    152. KEY `dish_id` (`dish_id`) USING BTREE,
    153. CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE,
    154. CONSTRAINT `order_items_ibfk_2` FOREIGN KEY (`dish_id`) REFERENCES `dishes` (`id`) ON DELETE CASCADE
    155. ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='订单项表';
    156. -- ----------------------------
    157. -- Records of order_items
    158. -- ----------------------------
    159. INSERT INTO `order_items` VALUES ('1', '2', '2', '2', '25.00');
    160. INSERT INTO `order_items` VALUES ('3', '3', '6', '1', '35.00');
    161. INSERT INTO `order_items` VALUES ('5', '4', '3', '5', '55.00');
    162. INSERT INTO `order_items` VALUES ('6', '6', '1', '6', '43.00');
    163. INSERT INTO `order_items` VALUES ('8', '8', '1', '2', '58.00');
    164. INSERT INTO `order_items` VALUES ('9', '9', '1', '2', '58.00');
    165. INSERT INTO `order_items` VALUES ('13', '10', '1', '2', '102.75');
    166. INSERT INTO `order_items` VALUES ('14', '11', '1', '2', '102.75');
    167. INSERT INTO `order_items` VALUES ('15', '12', '3', '2', '38.00');
    168. INSERT INTO `order_items` VALUES ('16', '13', '3', '2', '38.00');
    169. INSERT INTO `order_items` VALUES ('17', '14', '3', '2', '38.00');
    170. INSERT INTO `order_items` VALUES ('18', '15', '3', '2', '38.00');
    171. INSERT INTO `order_items` VALUES ('19', '16', '3', '2', '38.00');
    172. -- ----------------------------
    173. -- Table structure for `restaurants`
    174. -- ----------------------------
    175. DROP TABLE IF EXISTS `restaurants`;
    176. CREATE TABLE `restaurants` (
    177. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '餐厅ID',
    178. `name` varchar(100) NOT NULL COMMENT '餐厅名称',
    179. `address` varchar(255) NOT NULL COMMENT '餐厅地址',
    180. `opening_hours` varchar(50) DEFAULT NULL COMMENT '营业时间',
    181. `contact_number` varchar(20) DEFAULT NULL COMMENT '联系电话',
    182. PRIMARY KEY (`id`) USING BTREE
    183. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='餐厅表';
    184. -- ----------------------------
    185. -- Records of restaurants
    186. -- ----------------------------
    187. INSERT INTO `restaurants` VALUES ('1', '江湖酒楼', '京城大街1号', '09:00-22:00', '12345678');
    188. INSERT INTO `restaurants` VALUES ('2', '美味轩', '长安路88号', '10:00-21:30', '87654321');
    189. INSERT INTO `restaurants` VALUES ('3', '清风阁', '西湖路123号', '11:00-23:00', '98765432');
    190. INSERT INTO `restaurants` VALUES ('4', '美食街', '西梅路45号', '14:00-18:00', '22651231');
    191. INSERT INTO `restaurants` VALUES ('5', '饺子馆', '建设街23号', '8:00-11:30', '25641584');
    192. INSERT INTO `restaurants` VALUES ('6', '河间驴肉火烧', '教育路47号', '13:00-19:00', '27512975');
    193. -- ----------------------------
    194. -- Table structure for `users`
    195. -- ----------------------------
    196. DROP TABLE IF EXISTS `users`;
    197. CREATE TABLE `users` (
    198. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
    199. `username` varchar(50) NOT NULL COMMENT '用户名',
    200. `gender` enum('男','女') NOT NULL COMMENT '性别',
    201. `phone` varchar(20) DEFAULT NULL COMMENT '电话号码',
    202. `balance` varchar(255) DEFAULT '钱包余额',
    203. PRIMARY KEY (`id`) USING BTREE,
    204. UNIQUE KEY `username` (`username`) USING BTREE
    205. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='用户表';
    206. -- ----------------------------
    207. -- Records of users
    208. -- ----------------------------
    209. INSERT INTO `users` VALUES ('1', '赵一', '男', '154786482', '600');
    210. INSERT INTO `users` VALUES ('2', '钱二', '男', '154961482', '650');
    211. INSERT INTO `users` VALUES ('3', '张三', '女', '121876879', '450');
    212. INSERT INTO `users` VALUES ('4', '李四', '男', '187597326', '470');
    213. INSERT INTO `users` VALUES ('5', '王五', '女', '145745468', '550');
    214. -- ----------------------------
    215. -- Procedure structure for `create_order_infos`
    216. -- ----------------------------
    217. DROP PROCEDURE IF EXISTS `create_order_infos`;
    218. DELIMITER ;;
    219. CREATE DEFINER=`root`@`localhost` PROCEDURE `create_order_infos`(
    220. IN in_user_id INT,
    221. IN in_dish_id INT,
    222. IN in_quantity INT
    223. )
    224. BEGIN
    225. -- 注释:声明变量用于存储相关信息
    226. DECLARE dish_price DECIMAL(10, 2);
    227. DECLARE total_price DECIMAL(10, 2);
    228. DECLARE order_id INT;
    229. DECLARE restaurant_id INT;
    230. -- 注释:获取菜品价格
    231. SELECT price INTO dish_price FROM dishes WHERE id = in_dish_id;
    232. -- 注释:计算订单总价
    233. SET total_price = dish_price * in_quantity;
    234. -- 注释:获取菜品所属餐厅 ID
    235. SELECT restaurant_id INTO restaurant_id FROM dishes WHERE id = in_dish_id;
    236. -- 注释:插入订单信息到订单表
    237. INSERT INTO orders (user_id, restaurant_id, order_date, total_price, status)
    238. VALUES (in_user_id, restaurant_id, NOW(), total_price, '待支付');
    239. -- 注释:获取生成的订单 ID
    240. SET order_id = LAST_INSERT_ID();
    241. -- 注释:插入订单详情信息到订单详情表
    242. INSERT INTO order_items (order_id, dish_id, quantity, price_per_item)
    243. VALUES (order_id, in_dish_id, in_quantity, dish_price);
    244. -- 注释:更新商品表的库存数量
    245. UPDATE dishes SET stock_quantity = stock_quantity - in_quantity WHERE id = in_dish_id;
    246. END
    247. ;;
    248. DELIMITER ;
    249. DROP TRIGGER IF EXISTS `trg_check_dish_price_update`;
    250. DELIMITER ;;
    251. CREATE TRIGGER `trg_check_dish_price_update` BEFORE UPDATE ON `dishes` FOR EACH ROW BEGIN
    252. DECLARE original_price DECIMAL(10,2);
    253. DECLARE new_price DECIMAL(10,2);
    254. DECLARE diff DECIMAL(10,2);
    255. SET original_price = OLD.price;
    256. SET new_price = NEW.price;
    257. -- 计算价格差是否超过10%
    258. SET diff = ABS(new_price - original_price) / original_price;
    259. IF diff > 0.1 THEN
    260. SIGNAL SQLSTATE '45000'
    261. SET MESSAGE_TEXT = '商品售价上下浮动超过10%的限制!';
    262. END IF;
    263. END
    264. ;;
    265. DELIMITER ;
    266. DROP TRIGGER IF EXISTS `trg_delete_order_items_before_order`;
    267. DELIMITER ;;
    268. CREATE TRIGGER `trg_delete_order_items_before_order` BEFORE DELETE ON `orders` FOR EACH ROW BEGIN
    269. DELETE FROM order_items WHERE order_id = OLD.id;
    270. END
    271. ;;
    272. DELIMITER ;

     查询

    1、查询用户信息,仅显示用户的姓名与手机号,用中文显示列名。中文显示姓名列与手机号列。

    SELECT username as '姓名', phone as '手机号' from users;

     

    ​​2、根据商品名称进行模糊查询,模糊查询需要可以走索引,需要给出explain语句。使用explain测试给出的查询语句,需要显示走了索引查询。 

    explain select * from dishes where restaurant_id like 'e%';

     

    3、统计用户订单信息,查询所有用户的下单数量,并进行倒序排列。使用聚合函数查询处所有用户的订单数量,倒序排列结果。 

    1. SELECT user_id,sum(restaurant_id) as '订单数量'
    2. from `orders`
    3. GROUP BY user_id
    4. ORDER BY `订单数量` DESC;

     

    1、查询用户的基本信息,钱包信息。正确显示用户信息,正确显示用户钱包信息,正确进行多表联合查询. 

    1. select
    2. user_id,
    3. username,
    4. phone,
    5. order_date,
    6. total_price
    7. FROM
    8. users
    9. JOIN
    10. orders on user_id = user_id;

     

     2、查看订单中下单最多的产品对应的类别。正确使用聚合函数,正确使用子查询,正确显示结果.

    1. SELECT `name`
    2. from (
    3. SELECT `name`,count(*) as id
    4. from dishes
    5. GROUP BY `name`
    6. ) as subquery
    7. ORDER BY id DESC
    8. LIMIT 1;

     

    3、查询下单总金额最多的用户,并查询用户的全部信息与当前钱包余额。正确使用聚合函数,正确使用子查询,正确进行多表联合查询. 

     

    1. SELECT
    2. u.*,
    3. balance
    4. FROM
    5. users u
    6. JOIN
    7. (
    8. SELECT
    9. o.user_id,
    10. SUM(o.total_price) AS total_spent
    11. FROM
    12. orders o
    13. GROUP BY
    14. o.user_id
    15. ORDER BY
    16. total_spent DESC
    17. LIMIT 1
    18. ) AS top_spender
    19. ON
    20. u.id = top_spender.user_id
    21. LEFT JOIN
    22. orders w ON u.id = w.user_id;

    触发器和存储过程

    1.触发器

    1. DELIMITER $$
    2. CREATE TRIGGER trg_check_price_change
    3. BEFORE UPDATE ON dishes
    4. FOR EACH ROW
    5. BEGIN
    6. DECLARE old_price DECIMAL(10,2);
    7. DECLARE new_price DECIMAL(10,2);
    8. DECLARE price_change DECIMAL(10,2);
    9. -- 获取新旧价格
    10. SET old_price = OLD.price;
    11. SET new_price = NEW.price;
    12. -- 计算价格变化百分比
    13. IF (new_price <> 0) THEN
    14. SET price_change = (new_price - old_price) / old_price * 100;
    15. ELSE
    16. -- 如果新价格为0,则不允许更新
    17. SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '新价格不能为0。';
    18. END IF;
    19. -- 检查价格变动是否超过10%
    20. IF (price_change > 10 OR price_change < -10) THEN
    21. SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '商品售价上下浮动不能超过10%。';
    22. END IF;
    23. END;
    24. $$
    25. DELIMITER ;
    26. DELIMITER //
    27. CREATE TRIGGER trg_cascade_delete_order_items
    28. AFTER DELETE ON orders
    29. FOR EACH ROW
    30. BEGIN
    31. DELETE FROM order_items WHERE order_id = OLD.id;
    32. END;
    33. //
    34. DELIMITER ;
    35. DELETE FROM `order` WHERE `order_id` = 刚才添加的id号;
    36. -- 插入测试数据:
    37. insert into dishes(`name`,price,stock_quantity) VALUES ( '大鱼头',55.00,100);
    38. -- 正常更新(未超10%)
    39. UPDATE dishes set price =8.00 WHERE `name`='大鱼头'; -- 触发错误

     

    2.存储过程

    1. DELIMITER //
    2. CREATE PROCEDURE create_order_infos(
    3. IN in_user_id INT,
    4. IN in_dish_id INT,
    5. IN in_quantity INT
    6. )
    7. BEGIN
    8. -- 注释:声明变量用于存储相关信息
    9. DECLARE dish_price DECIMAL(10, 2);
    10. DECLARE total_price DECIMAL(10, 2);
    11. DECLARE order_id INT;
    12. DECLARE restaurant_id INT;
    13. -- 注释:获取菜品价格
    14. SELECT price INTO dish_price FROM dishes WHERE id = in_dish_id;
    15. -- 注释:计算订单总价
    16. SET total_price = dish_price * in_quantity;
    17. -- 注释:获取菜品所属餐厅 ID
    18. SELECT restaurant_id INTO restaurant_id FROM dishes WHERE id = in_dish_id;
    19. -- 注释:插入订单信息到订单表
    20. INSERT INTO orders (user_id, restaurant_id, order_date, total_price, status)
    21. VALUES (in_user_id, restaurant_id, NOW(), total_price, '待支付');
    22. -- 注释:获取生成的订单 ID
    23. SET order_id = LAST_INSERT_ID();
    24. -- 注释:插入订单详情信息到订单详情表
    25. INSERT INTO order_items (order_id, dish_id, quantity, price_per_item)
    26. VALUES (order_id, in_dish_id, in_quantity, dish_price);
    27. -- 注释:更新商品表的库存数量
    28. UPDATE dishes SET stock_quantity = stock_quantity - in_quantity WHERE id = in_dish_id;
    29. END //
    30. DELIMITER ;
    31. CALL create_order_infos(2, 3, 2);

     

    总结:

    餐厅点餐系统的MySQL数据库数据管理与维护至关重要。这包括定期备份数据库,确保数据安全性;优化查询性能,提高系统响应速度;监控数据库运行状态,及时发现并解决潜在问题。同时,需要定期清理过期数据,保持数据库的整洁和高效。此外,还要定期更新和维护数据库表结构,以适应业务发展和变化。通过有效的数据管理与维护,可以确保餐厅点餐系统的稳定运行和数据的准确性。 

  • 相关阅读:
    互联网Java工程师面试题·Redis 篇·第二弹
    【推荐系统】特征处理
    汉纳西点:100天成功打造大连行业最大单体店,创造一个商业传奇
    Endotoxin Substrate:Boc-LGR-pNA,CAS号: 68223-96-1
    如何做好建筑翻译呢
    Windows Server2012 R2修复SSL/TLS漏洞(CVE-2016-2183)
    【精讲】vue框架 利用脚手架实现购物车(含添加、删除、存储、清空数据、全选or单选、tap栏切换)内含详细注释
    SpringBoot 请求参数解析全过程
    2011年03月16日 Go生态洞察:Go朝着更高稳定性迈进
    Java中的super关键字
  • 原文地址:https://blog.csdn.net/2301_80388658/article/details/140043631