ALTER TRIGGER TR_TMP

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER TRIGGER TR_TMP
ON TMP
FOR UPDATE
AS
UPDATE TMP SET LASTTIME=GETDATE() WHERE ID IN (SELECT ID FROM inserted)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

[root@yfw ~]# cd /www/wwwroot/szrengjing.com [root@yfw szrengjing.com]# mysql -u szrengjing_com -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 93707 Server version: 5.7.42-log Source distribution Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> USE szrengjing_com; Database changed mysql> CREATE TABLE IF NOT EXISTS ecs_user_credit ( -> id INT AUTO_INCREMENT PRIMARY KEY, -> user_id INT NOT NULL UNIQUE, -> score INT DEFAULT 80, -> level VARCHAR(20) AS ( -> CASE -> WHEN score >= 95 THEN '★★★★★' -> WHEN score >= 85 THEN '★★★★☆' -> WHEN score >= 75 THEN '★★★☆☆' -> WHEN score >= 60 THEN '★★☆☆☆' -> ELSE '★☆☆☆☆' -> END -> ) STORED, -> updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> CREATE TABLE IF NOT EXISTS ecs_user_credit_log ( -> id BIGINT AUTO_INCREMENT PRIMARY KEY, -> user_id INT NOT NULL, -> old_score INT, -> new_score INT, -> change_value INT, -> reason VARCHAR(100), -> created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DROP TRIGGER IF EXISTS tr_after_update_credit; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> DROP PROCEDURE IF EXISTS sp_add_credit; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DELIMITER ;; mysql> CREATE TRIGGER tr_after_update_credit -> AFTER UPDATE ON ecs_user_credit -> FOR EACH ROW -> BEGIN -> INSERT INTO ecs_user_credit_log (user_id, old_score, new_score, change_value, reason) -> VALUES (NEW.user_id, OLD.score, NEW.score, NEW.score - OLD.score, '系统调整'); -> END;; Query OK, 0 rows affected (0.01 sec) mysql> CREATE PROCEDURE sp_add_credit( -> IN p_user_id INT, -> IN p_change INT, -> IN p_reason VARCHAR(100) -> ) -> BEGIN -> DECLARE current_score INT DEFAULT 80; -> -- 查询当前分数并加锁防止并发 -> SELECT score INTO current_score FROM ecs_user_credit WHERE user_id = p_user_id FOR UPDATE; -> -> -- 插入或更新分数 -> INSERT INTO ecs_user_credit (user_id, score) -> VALUES (p_user_id, current_score + p_change) -> ON DUPLICATE KEY UPDATE score = score + p_change; -> END;; Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> SHOW TABLES LIKE 'ecs_user_credit%'; +---------------------------------------------+ | Tables_in_szrengjing_com (ecs_user_credit%) | +---------------------------------------------+ | ecs_user_credit | | ecs_user_credit_log | +---------------------------------------------+ 2 rows in set (0.00 sec) mysql> SHOW TRIGGERS LIKE 'ecs_user_credit'\G *************************** 1. row *************************** Trigger: tr_update_credit_level Event: UPDATE Table: ecs_user_credit Statement: BEGIN IF NEW.score >= 95 THEN SET NEW.level = '★★★★★'; ELSEIF NEW.score >= 85 THEN SET NEW.level = '★★★★☆'; ELSEIF NEW.score >= 75 THEN SET NEW.level = '★★★☆☆'; ELSEIF NEW.score >= 60 THEN SET NEW.level = '★★☆☆☆'; ELSE SET NEW.level = '★☆☆☆☆'; END IF; END Timing: BEFORE Created: 2025-11-09 05:25:20.26 sql_mode: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Definer: szrengjing_com@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8mb4_general_ci *************************** 2. row *************************** Trigger: tr_after_update_credit Event: UPDATE Table: ecs_user_credit Statement: BEGIN INSERT INTO ecs_user_credit_log (user_id, old_score, new_score, change_value, reason) VALUES (NEW.user_id, OLD.score, NEW.score, NEW.score - OLD.score, '系统调整'); END Timing: AFTER Created: 2025-11-09 06:19:33.39 sql_mode: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION Definer: szrengjing_com@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8mb4_general_ci 2 rows in set (0.00 sec) mysql> SHOW PROCEDURE STATUS WHERE Name = 'sp_add_credit'\G *************************** 1. row *************************** Db: szrengjing_com Name: sp_add_credit Type: PROCEDURE Definer: szrengjing_com@localhost Modified: 2025-11-09 06:19:48 Created: 2025-11-09 06:19:48 Security_type: DEFINER Comment: character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8mb4_general_ci 1 row in set (0.00 sec) mysql> CALL sp_add_credit(1, 5, '手动测试加分'); Query OK, 2 rows affected (0.01 sec) mysql> SELECT * FROM ecs_user_credit_log ORDER BY id DESC LIMIT 1\G *************************** 1. row *************************** id: 4 user_id: 1 old_score: 90 new_score: 95 change_value: 5 reason: 系统调整 created_at: 2025-11-09 06:21:06 1 row in set (0.00 sec) mysql> DROP TRIGGER IF EXISTS tr_update_credit_level; Query OK, 0 rows affected (0.01 sec) mysql> CALL sp_add_credit(2, 10, '新用户注册奖励'); Query OK, 1 row affected (0.00 sec) mysql> CALL sp_add_credit(1, -5, '违规扣分测试'); Query OK, 2 rows affected (0.01 sec) mysql> SELECT * FROM ecs_user_credit_log ORDER BY id DESC LIMIT 5\G *************************** 1. row *************************** id: 5 user_id: 1 old_score: 95 new_score: 90 change_value: -5 reason: 系统调整 created_at: 2025-11-09 06:22:39 *************************** 2. row *************************** id: 4 user_id: 1 old_score: 90 new_score: 95 change_value: 5 reason: 系统调整 created_at: 2025-11-09 06:21:06 *************************** 3. row *************************** id: 3 user_id: 1 old_score: 85 new_score: 90 change_value: 5 reason: 测试加分 created_at: 2025-11-09 05:55:47 *************************** 4. row *************************** id: 2 user_id: 1 old_score: 95 new_score: 85 change_value: -10 reason: 评论被举报 created_at: 2025-11-09 05:38:14 *************************** 5. row *************************** id: 1 user_id: 1 old_score: 80 new_score: 95 change_value: 15 reason: 完善个人资料 created_at: 2025-11-09 05:38:14 5 rows in set (0.00 sec) mysql> SELECT user_id, score, level, updated_at FROM ecs_user_credit; +---------+-------+-----------------+---------------------+ | user_id | score | level | updated_at | +---------+-------+-----------------+---------------------+ | 1 | 90 | ★★★★★ | 2025-11-09 06:22:39 | | 2 | 90 | ★★★★☆ | 2025-11-09 06:22:39 | +---------+-------+-----------------+---------------------+ 2 rows in set (0.00 sec) mysql> CALL sp_add_credit(3, 10, '邀请好友'); Query OK, 1 row affected (0.00 sec) mysql> DELIMITER ;; mysql> DROP PROCEDURE IF EXISTS sp_add_credit;; Query OK, 0 rows affected (0.00 sec) mysql> CREATE PROCEDURE sp_add_credit( -> IN p_user_id INT, -> IN p_change INT, -> IN p_reason VARCHAR(100) -> ) -> BEGIN -> DECLARE current_score INT DEFAULT 80; -> DECLARE CONTINUE HANDLER FOR NOT FOUND SET current_score = 80; -> -> -- 尝试获取当前分数(如果存在) -> SELECT score INTO current_score -> FROM ecs_user_credit -> WHERE user_id = p_user_id -> FOR UPDATE; -> -> -- 插入或更新:不存在则从 80+p_change 开始 -> INSERT INTO ecs_user_credit (user_id, score) -> VALUES (p_user_id, current_score + p_change) -> ON DUPLICATE KEY UPDATE score = score + p_change; -> END;; Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> DELIMITER ;; mysql> DROP PROCEDURE IF EXISTS sp_add_credit;; Query OK, 0 rows affected (0.00 sec) mysql> CREATE PROCEDURE sp_add_credit( -> IN p_user_id INT, -> IN p_change INT, -> IN p_reason VARCHAR(100) -> ) -> BEGIN -> DECLARE current_score INT DEFAULT 80; -> DECLARE new_score INT; -> DECLARE CONTINUE HANDLER FOR NOT FOUND SET current_score = 80; -> -> -- 获取当前分数(加锁) -> SELECT score INTO current_score -> FROM ecs_user_credit -> WHERE user_id = p_user_id -> FOR UPDATE; -> -> -- 计算新分数 -> SET new_score = current_score + p_change; -> -> -- 限制范围 -> IF new_score < 0 THEN -> SET new_score = 0; -> ELSEIF new_score > 100 THEN -> SET new_score = 100; -> END IF; -> -> -- 写入数据库 -> INSERT INTO ecs_user_credit (user_id, score) -> VALUES (p_user_id, new_score) -> ON DUPLICATE KEY UPDATE score = new_score; -> END;; Query OK, 0 rows affected (0.01 sec) mysql> DELIMITER ; mysql> CREATE OR REPLACE VIEW vw_user_credit_summary AS -> SELECT -> u.user_id, -> u.score, -> u.level, -> u.updated_at, -> ( -> SELECT GROUP_CONCAT(reason SEPARATOR '; ') -> FROM ecs_user_credit_log l -> WHERE l.user_id = u.user_id -> ORDER BY l.created_at DESC -> LIMIT 3 -> ) AS recent_actions -> FROM ecs_user_credit u -> ORDER BY u.score DESC; Query OK, 0 rows affected (0.01 sec) mysql> SELECT * FROM vw_user_credit_summary\G *************************** 1. row *************************** user_id: 1 score: 90 level: ★★★★★ updated_at: 2025-11-09 06:22:39 recent_actions: 完善个人资料; 评论被举报; 测试加分; 系统调整; 系统调整 *************************** 2. row *************************** user_id: 2 score: 90 level: ★★★★☆ updated_at: 2025-11-09 06:22:39 recent_actions: NULL *************************** 3. row *************************** user_id: 3 score: 90 level: ★★★★☆ updated_at: 2025-11-09 06:24:47 recent_actions: NULL 3 rows in set (0.00 sec) mysql> -- 日志表按用户和时间查询频繁 mysql> ALTER TABLE ecs_user_credit_log ADD INDEX idx_user_time (user_id, created_at); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> -- 主表按分数排序常用 mysql> ALTER TABLE ecs_user_credit ADD INDEX idx_score (score); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> CREATE PROCEDURE sp_add_credit(...) -> BEGIN -> DECLARE current_score INT DEFAULT 80; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '...) BEGIN DECLARE current_score INT DEFAULT 80' at line 1 mysql> DECLARE new_score INT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE new_score INT' at line 1 mysql> DECLARE CONTINUE HANDLER FOR NOT FOUND SET current_score = 80; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE CONTINUE HANDLER FOR NOT FOUND SET current_score = 80' at line 1 mysql> mysql> SELECT score INTO current_score FROM ecs_user_credit WHERE user_id = p_user_id FOR UPDATE; ERROR 1327 (42000): Undeclared variable: current_score mysql> mysql> SET new_score = current_score + p_change; ERROR 1193 (HY000): Unknown system variable 'new_score' mysql> IF new_score < 0 THEN SET new_score = 0; END IF; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF new_score < 0 THEN SET new_score = 0' at line 1 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF' at line 1 mysql> IF new_score > 100 THEN SET new_score = 100; END IF; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF new_score > 100 THEN SET new_score = 100' at line 1 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF' at line 1 mysql> mysql> INSERT INTO ... ON DUPLICATE KEY UPDATE score = new_score; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.. ON DUPLICATE KEY UPDATE score = new_score' at line 1 mysql> END;; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1 ERROR: No query specified mysql> SELECT * FROM vw_user_credit_summary\G *************************** 1. row *************************** user_id: 1 score: 90 level: ★★★★★ updated_at: 2025-11-09 06:22:39 recent_actions: 完善个人资料; 评论被举报; 测试加分; 系统调整; 系统调整 *************************** 2. row *************************** user_id: 2 score: 90 level: ★★★★☆ updated_at: 2025-11-09 06:22:39 recent_actions: NULL *************************** 3. row *************************** user_id: 3 score: 90 level: ★★★★☆ updated_at: 2025-11-09 06:24:47 recent_actions: NULL 3 rows in set (0.00 sec) mysql> CREATE OR REPLACE VIEW vw_user_credit_summary AS -> SELECT -> u.user_id, -> u.score, -> u.level, -> u.updated_at, -> ( -> SELECT GROUP_CONCAT(reason SEPARATOR '; ') -> FROM ( -> SELECT reason -> FROM ecs_user_credit_log l -> WHERE l.user_id = u.user_id -> ORDER BY l.created_at DESC -> LIMIT 3 -> ) AS tmp -> ) AS recent_actions -> FROM ecs_user_credit u -> ORDER BY u.score DESC; ERROR 1054 (42S22): Unknown column 'u.user_id' in 'where clause' mysql> SET SESSION group_concat_max_len = 1024; Query OK, 0 rows affected (0.00 sec) mysql> ALTER TABLE ecs_user_credit_log ADD INDEX idx_user_time (user_id, created_at); ERROR 1061 (42000): Duplicate key name 'idx_user_time' mysql> ALTER TABLE ecs_user_credit ADD INDEX idx_score (score); ERROR 1061 (42000): Duplicate key name 'idx_score' mysql>
11-10
内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值