news 2026/3/21 18:16:29

关系型数据库大王Mysql——SQL编程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
关系型数据库大王Mysql——SQL编程

SQL编程

触发器

什么是触发器?

​ 当某个表的数据发生某件事(insert, delete, update), 然后自动触发预先编译好的若干条sql

触发器

1.特点:触发的操作和触发器的sql语句是一个事务操作,具备原子性,要么都执行,要么都不执行

2.作用:保证数据的完整性,起到约束的作用

示例1

mysql> create table emp_count( -> emp_count_id int primary key auto_increment, -> total int); Query OK, 0 rows affected (0.07 sec) mysql> show tables; +--------------------+ | Tables_in_dml_test | +--------------------+ | department | | emp_count | | employee | | user | +--------------------+ 4 rows in set (0.00 sec) mysql> insert into emp_count values(1,0); Query OK, 1 row affected (0.01 sec) mysql> select * from emp_count; +--------------+-------+ | emp_count_id | total | +--------------+-------+ | 1 | 0 | +--------------+-------+ 1 row in set (0.00 sec) mysql> update emp_count set total = 18 where total = 0; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from emp_count; +--------------+-------+ | emp_count_id | total | +--------------+-------+ | 1 | 18 | +--------------+-------+ 1 row in set (0.00 sec) #临时修改终止符,以免与触发器语句发生冲突 mysql> \d $ #delimiter $ mysql> create trigger emp_count_p after insert -> on employee for each row -> begin -> update emp_count set total = total + 1 where emp_count_id = 1; -> end -> $ Query OK, 0 rows affected (0.01 sec) mysql> show triggers\G *************************** 1. row *************************** Trigger: emp_count_p Event: INSERT Table: employee Statement: begin update emp_count set total = total + 1 where emp_count_id = 1; end Timing: AFTER Created: 2025-11-18 20:10:20.59 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Definer: root@% character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci 1 row in set (0.01 sec) mysql> insert into employee(number,name) values("23123213132",'来俊希')$ Query OK, 1 row affected (0.01 sec) mysql> select * from emp_count$ +--------------+-------+ | emp_count_id | total | +--------------+-------+ | 1 | 19 | +--------------+-------+ 1 row in set (0.00 sec)

示例2

mysql> create table bank( -> b_id int primary key auto_increment, -> value int)$ Query OK, 0 rows affected (0.03 sec) mysql> create table u( -> u_id int primary key auto_increment, -> value int)$ Query OK, 0 rows affected (0.04 sec) #创建触发器 mysql> create trigger u_bank_t after insert -> on u for each row -> begin -> update bank set value = value + 500 where b_id = 1; -> end -> $ Query OK, 0 rows affected (0.02 sec) #查看创建的触发器 mysql> show triggers\G *************************** 1. row *************************** Trigger: emp_count_p Event: INSERT Table: employee Statement: begin update emp_count set total = total + 1 where emp_count_id = 1; end Timing: AFTER Created: 2025-11-18 20:10:20.59 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Definer: root@% character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci *************************** 2. row *************************** Trigger: u_bank_t Event: INSERT Table: u Statement: begin update bank set value = value + 500 where b_id = 1; end Timing: AFTER Created: 2025-11-18 20:24:50.28 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Definer: root@% character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci 2 rows in set (0.00 sec) mysql> insert into bank(b_id,value) values(1,0); Query OK, 1 row affected (0.01 sec) mysql> select * from bank; +------+-------+ | b_id | value | +------+-------+ | 1 | 0 | +------+-------+ 1 row in set (0.00 sec) mysql> insert into u(u_id,value) values(1,500); Query OK, 1 row affected (0.01 sec) mysql> select * from bank; +------+-------+ | b_id | value | +------+-------+ | 1 | 500 | +------+-------+ 1 row in set (0.00 sec) mysql> select * from u; +------+-------+ | u_id | value | +------+-------+ | 1 | 500 | +------+-------+ 1 row in set (0.00 sec) mysql> select value from u where u_id = 1; +-------+ | value | +-------+ | 500 | +-------+ 1 row in set (0.00 sec) mysql> select value from u group by u_id having max(value); +-------+ | value | +-------+ | 500 | +-------+ 1 row in set (0.00 sec) mysql> insert into bank(value) select value from u group by u_id having max(value); Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from bank; +------+-------+ | b_id | value | +------+-------+ | 1 | 500 | | 2 | 500 | +------+-------+ 2 rows in set (0.00 sec)

存储过程

什么是存储过程

事先经过编译并存储在数据库中的一段sql语句集合

示例1

mysql> create procedure emp_count() -> begin -> select count(emp_count_id) from emp_count; -> end -> $ Query OK, 0 rows affected (0.01 sec) #查看创建的存储过程 mysql> show create procedure emp_count\G$ *************************** 1. row *************************** Procedure: emp_count sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Create Procedure: CREATE DEFINER=`root`@`%` PROCEDURE `emp_count`() begin select count(emp_count_id) from emp_count; end character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci 1 row in set (0.00 sec) #调用存储过程 mysql> call emp_count(); -> $ +---------------------+ | count(emp_count_id) | +---------------------+ | 1 | +---------------------+ 1 row in set (0.00 sec)

示例2

mysql> create table t1( -> id int primary key auto_increment, -> password varchar(255)); -> $ Query OK, 0 rows affected (0.03 sec) mysql> create procedure insert_many_date(in total_row) -> begin -> declare i int default 1; -> while (i < rows) do -> insert into t1 values(i,md5(i)); -> set i = i + 1; -> end while; -> end -> $ mysql> create procedure user_password(in pwd varchar(255)) -> begin -> select * from user where password = pwd; -> end$ Query OK, 0 rows affected (0.01 sec) mysql> call user_password("111")$ Empty set (0.01 sec)

示例3

mysql> select * from user; -> $ +---------+----------+----------+------+------------+ | user_id | username | password | name | hire_date | +---------+----------+----------+------+------------+ | 1 | ljx | ljxbbfjw | cj | 2006-06-06 | | 2 | ljx | ljxbbfjw | cj | 2006-06-06 | | 3 | lisi | 123 | ls | 2007-07-07 | | 4 | wangwu | 123 | ww | 2008-08-08 | | 5 | cj | 123 | cj | 2009-09-09 | +---------+----------+----------+------+------------+ 5 rows in set (0.00 sec) mysql> create procedure user_password(in pwd varchar(255)) -> begin -> select * from user where password = pwd; -> end$ Query OK, 0 rows affected (0.01 sec) mysql> call user_password("123")$ +---------+----------+----------+------+------------+ | user_id | username | password | name | hire_date | +---------+----------+----------+------+------------+ | 3 | lisi | 123 | ls | 2007-07-07 | | 4 | wangwu | 123 | ww | 2008-08-08 | | 5 | cj | 123 | cj | 2009-09-09 | +---------+----------+----------+------+------------+ 3 rows in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)

示例4

mysql> select @a; -> $ +------------+ | @a | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> set @a = 1; -> $ Query OK, 0 rows affected (0.01 sec) mysql> select @a; -> $ +------+ | @a | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql> \d $ mysql> create procedure user_count_a(out number int) -> begin -> select count(1) into number from user; -> end$ Query OK, 0 rows affected (0.01 sec) mysql> show create procedure user_count_a\G$ *************************** 1. row *************************** Procedure: user_count_a sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Create Procedure: CREATE DEFINER=`root`@`%` PROCEDURE `user_count_a`(out number int) begin select count(1) into number from user; end character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci 1 row in set (0.00 sec) mysql> call user_count_a(@a); -> $ Query OK, 1 row affected (0.01 sec) mysql> select @a -> $ +------+ | @a | +------+ | 5 | +------+ 1 row in set (0.00 sec) mysql> \d ; mysql> select count(1) from user; +----------+ | count(1) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)

示例5

mysql> \d $ mysql> create procedure count_emp_name(in dep_name varchar(255),out count_emp int) -> begin -> select count(*) into count_emp from employee where department_NO = (select number from department where name = dep_name); -> end$ Query OK, 0 rows affected (0.02 sec) mysql> \d ; mysql> show create procedure count_emp_name\G; *************************** 1. row *************************** Procedure: count_emp_name sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Create Procedure: CREATE DEFINER=`root`@`%` PROCEDURE `count_emp_name`(in dep_name varchar(255),out count_emp int) begin select count(*) into count_emp from employee where department_NO = (select number from department where name = dep_name); end character_set_client: utf8mb4 collation_connection: utf8mb4_0900_ai_ci Database Collation: utf8mb4_0900_ai_ci 1 row in set (0.00 sec) mysql> call count_emp_name("上海中心",@a); Query OK, 1 row affected (0.00 sec) mysql> select @a -> ; +------+ | @a | +------+ | 2 | +------+ 1 row in set (0.00 sec) mysql> select count(*) from employee where department_NO = (select number from department where name = "上海中心"); +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.00 sec)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/13 7:18:07

ssm仓库管理信息系统whkb8--程序+源码+数据库+调试部署+开发环境

本系统&#xff08;程序源码数据库调试部署开发环境&#xff09;带论文文档1万字以上&#xff0c;文末可获取&#xff0c;系统界面在最后面。 系统程序文件列表 开题报告内容 一、项目背景与意义 随着企业规模的不断扩大&#xff0c;仓库管理变得越来越复杂&#xff0c;传统…

作者头像 李华
网站建设 2026/3/13 22:56:35

S选择器这锅老汤:从清汤寡水到浓油赤酱的30味秘料

S选择器这锅老汤&#xff1a;从清汤寡水到浓油赤酱的30味秘料CSS选择器这锅老汤&#xff1a;从清汤寡水到浓油赤酱的30味秘料 说实话&#xff0c;我刚学前端那会儿&#xff0c;写CSS就是纯靠体力活。打开DevTools&#xff0c;选中元素&#xff0c;复制那个长得离谱的class名字…

作者头像 李华
网站建设 2026/3/19 6:29:17

当 FPGA 遇上 Python:Glasgow 如何玩转数字接口(开源硬件 软件)

在嵌入式开发、接口调试、硬件逆向或数字存档工作中&#xff0c;经常会遇到各种异构、老旧或自定义数字接口协议。为了让工程师更轻松地接入、探测、控制和抓取这些接口的数据&#xff0c;Glasgow Embedded 社区推出了一个开源工具——Glasgow Interface Explorer。什么是 Glas…

作者头像 李华
网站建设 2026/3/20 7:06:25

**AI漫剧软件2025推荐,解锁沉浸式二次元内容创作新体验

AI漫剧软件2025推荐&#xff0c;解锁沉浸式二次元内容创作新体验据《2025年中国数字内容产业白皮书》显示&#xff0c;2025年AI生成视频内容的市场规模预计突破800亿元&#xff0c;其中二次元风格的AI漫剧因其独特的视觉表现力和高效的产能&#xff0c;成为增长最快的细分赛道之…

作者头像 李华
网站建设 2026/3/21 13:29:17

CSS3笔记

1. CSS 引入方式 行内式&#xff1a;直接写在标签 style"" 内&#xff08;优先级高&#xff0c;不推荐大量使用&#xff09;嵌入式&#xff1a;在 HTML 的 <head> 里写 <style>...</style>外部式&#xff1a;通过 <link rel"stylesheet&q…

作者头像 李华
网站建设 2026/3/21 17:44:46

模型炼金术:机器学习全流程中的超参数调优与评估陷阱

模型炼金术&#xff1a;机器学习全流程中的超参数调优与评估陷阱 1. 超参数调优的本质与价值 在机器学习项目中&#xff0c;超参数调优往往被视为"模型炼金术"——它既需要科学方法论指导&#xff0c;又依赖实践经验的微妙平衡。与模型参数不同&#xff0c;超参数是…

作者头像 李华