mysql 触发器 进程
mysql 触发器 过程 1、触发器: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt 其中trigger_name标识触发器名称,用户自行指定; trigger_time标识触发时机,用before和after替换; trigger_event标识触发
mysql 触发器 过程1、触发器:
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
其中trigger_name标识触发器名称,用户自行指定;trigger_time标识触发时机,用before和after替换;trigger_event标识触发事件,用insert,updat
e和delete替换;bl_name标识建立触发器的表名,即在哪张表上建立触发器;trigger_stmt是触发器程序体;触发器程序可以使用begin和end作为开
始和结束,中间包含多条语句;
有几个状态对像和几张伟表:
insert : NEW
update : NEW OLD
delete : OLD
status : 表示是否有新数据和老数据
2、过程中
-
mysql>
DELIMITER // -
mysql>
CREATE PROCEDURE proc1( OUTs int) -
-> -
-> COUNT(*) INTO s FROMuser; -
-> -
-> // -
mysql>
DELIMITER ;
MySQL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:
CREATE PROCEDURE([[IN |OUT |INOUT ] 参数名 数据类形...])
IN 输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值
OUT 输出参数:该值可在存储过程内部被改变,并可返回
INOUT 输入输出参数:调用时指定,并且可被改变和返回
Ⅰ. IN参数例子
创建:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE demo_in_parameter( INp_in int) -
->
BEGIN -
->
SELECT p_in; -
->
SET p_in=2; -
->
SELECT p_in; -
->
END; -
->
// -
mysql
> DELIMITER ;
执行结果:
-
mysql
> SET@p_in=1; -
mysql
> CALL demo_in_parameter(@p_in); -
+------+
-
|
p_in | -
+------+
-
|
1 | -
+------+
-
-
+------+
-
|
p_in | -
+------+
-
|
2 | -
+------+
-
-
mysql>
SELECT @p_in; -
+-------+
-
|
@p_in | -
+-------+
-
|
1 | -
+-------+
以上可以看出,p_in虽然在存储过程中被修改,但并不影响@p_id的值
Ⅱ.OUT参数例子
创建:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE demo_out_parameter( OUTp_out int) -
->
BEGIN -
->
SELECT p_out; -
->
SET p_out=2; -
->
SELECT p_out; -
->
END; -
->
// -
mysql
> DELIMITER ;
执行结果:
-
mysql
> SET@p_out=1; -
mysql
> CALL sp_demo_out_parameter(@p_out); -
+-------+
-
|
p_out | -
+-------+
-
|
NULL | -
+-------+
-
-
+-------+
-
|
p_out | -
+-------+
-
|
2 | -
+-------+
-
-
mysql>
SELECT @p_out; -
+-------+
-
|
p_out | -
+-------+
-
|
2 | -
+-------+
Ⅲ.
INOUT参数例子
创建:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE demo_inout_parameter(INOUT int)p_inout -
->
BEGIN -
->
SELECT p_inout; -
->
SET p_inout=2; -
->
SELECT p_inout; -
->
END; -
->
// -
mysql
> DELIMITER ;
-
mysql
> SET p_inout=1;@ -
mysql
> CALL demo_inout_parameter(@p_inout) ; -
+---------+
-
|
p_inout | -
+---------+
-
|
1 | -
+---------+
-
-
+---------+
-
|
p_inout | -
+---------+
-
|
2 | -
+---------+
-
-
mysql
> SELECT @p_inout; -
+----------+
-
|
@p_inout | -
+----------+
-
|
2 | -
+----------+
(4). 变量
Ⅰ. 变量定义
DECLARE variable_name [,variable_name...] datatype [DEFAULT value];
其中,datatype为MySQL的数据类型,如:int, float, date, varchar(length)
例如:
-
DECLARE
l_int intunsigned default4000000; -
DECLARE
l_numeric DEFAULTnumber(8,2) 9.95; -
DECLARE
l_date dateDEFAULT '1999-12-31'; -
DECLARE
l_datetime DEFAULTdatetime '1999-12-31 23:59:59' ; -
DECLARE
l_varchar varchar(255)DEFAULT 'This will ;not be padded'
Ⅲ. 用户变量
ⅰ. 在MySQL客户端使用用户变量
-
mysql
> SELECT'Hello World' into @x; -
mysql
> SELECT@x; -
+-------------+
-
|
@x | -
+-------------+
-
|
Hello World | -
+-------------+
-
mysql
> SET@y= 'GoodbyeCruel ;World' -
mysql
> SELECT@y; -
+---------------------+
-
|
@y | -
+---------------------+
-
|
Goodbye Cruel World | -
+---------------------+
-
-
mysql
> SET@z=1+2+3; -
mysql
> SELECT@z; -
+------+
-
|
@z | -
+------+
-
|
6 | -
+------+
ⅱ. 在存储过程中使用用户变量
-
mysql
> CREATEPROCEDURE GreetWorld( SELECT) CONCAT(@greeting, 'World' ); -
mysql
> SET@greeting= 'Hello'; -
mysql
> CALL GreetWorld( ); -
+----------------------------+
-
|
CONCAT(@greeting, 'World' )| -
+----------------------------+
-
|
Hello World | -
+----------------------------+
-
mysql>
CREATE PROCEDURE p1() SET@last_procedure= 'p1'; -
mysql>
CREATE PROCEDURE p2() SELECTCONCAT( 'Lastprocedure ,@last_proc);was ' -
mysql>
CALL p1( ); -
mysql>
CALL p2( ); -
+-----------------------------------------------+
-
|
CONCAT( 'Lastprocedure ,@last_procwas ' | -
+-----------------------------------------------+
-
|
Last procedure was p1 | -
+-----------------------------------------------+
①用户变量名一般以@开头
②滥用用户变量会导致程序难以理解及管理
(5). 注释
MySQL存储过程可使用两种风格的注释
双模杠:--
该风格一般用于单行注释
c风格: 一般用于多行注释
例如:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc1 --name存储过程名 -
->
( INparameter1 INTEGER) -
->
BEGIN -
->
DECLARE variable1 CHAR(10); -
->
IF THENparameter1 = 17 -
->
SET variable1 'birds';= -
->
ELSE -
->
SET variable1 'beasts';= -
->
END IF; -
->
INSERT INTO table1 VALUES(variable1); -
->
END -
->
// -
mysql
> DELIMITER ;
4.
用call和你过程名以及一个括号,括号里面根据需要,加入参数,参数包括输入参数、输出参数、输入输出参数。具体的调用方法可以参看上面的例子。
5.
我们像知道一个数据库下面有那些表,我们一般采用show tables;进行查看。那么我们要查看某个数据库下面的存储过程,是否也可以采用呢?答案是,我们可以查看某个数据库下面的存储过程,但是是令一钟方式。
我们可以用
select name from mysql.proc where db=’数据库名’;
或者
select routine_name from information_schema.routines where routine_schema='数据库名';
或者
show procedure status where db='数据库名';
进行查询。
如果我们想知道,某个存储过程的详细,那我们又该怎么做呢?是不是也可以像操作表一样用describe 表名进行查看呢?
答案是:我们可以查看存储过程的详细,但是需要用另一种方法:
SHOW CREATE PROCEDURE 数据库.存储过程名;
就可以查看当前存储过程的详细。
6.
ALTER PROCEDURE
更改用CREATE PROCEDURE 建立的预先指定的存储过程,其不会影响相关存储过程或存储功能。
7.
删除一个存储过程比较简单,和删除表一样:
DROP PROCEDURE
从MySQL的表格中删除一个或多个存储过程。
8.
(1). 变量作用域
内部的变量在其作用域范围内享有更高的优先权,当执行到end。变量时,内部变量消失,此时已经在其作用域外,变量不再可见了,应为在存储
过程外再也不能找到这个申明的变量,但是你可以通过out参数或者将其值指派
给会话变量来保存其值。
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc3() -
-> -
-> x1 varchar(5)default 'outer'; -
-> -
-> x1 varchar(5)default 'inner'; -
-> x1; -
-> -
-> x1; -
-> -
-> // -
mysql
> DELIMITER ;
Ⅰ. if-then -else语句
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc2( INparameter int) -
-> -
-> var int; -
-> var=parameter+1; -
-> if var=0 -
-> into t values(17); -
-> if; -
-> if parameter=0 -
-> t sets1=s1+1; -
-> -
-> t sets1=s1+2; -
-> if; -
-> -
-> // -
mysql
> DELIMITER ;
Ⅱ. case语句:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc3 in( parameter int) -
-> -
-> var int; -
-> var=parameter+1; -
-> var -
-> 0 then -
-> into t values(17); -
-> 1 then -
-> into t values(18); -
-> -
-> into t values(19); -
-> case; -
-> -
-> // -
mysql
> DELIMITER ;
(3). 循环语句
Ⅰ. while ···· end while:
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc4() -
-> -
-> var int; -
-> var=0; -
-> while vardo -
-> into t values(var); -
-> var=var+1; -
-> while; -
-> -
-> // -
mysql
> DELIMITER ;
Ⅱ. repeat···· end repeat:
它在执行操作后检查结果,而while则是执行前进行检查。
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc5 () -
-> -
-> v int; -
-> v=0; -
-> repeat -
-> into t values(v); -
-> v=v+1; -
-> until v>=5 -
-> repeat; -
-> -
-> // -
mysql
> DELIMITER ;
Ⅲ. loop ·····end
loop:
loop循环不需要初始条件,这点和while 循环相似,同时和repeat循环一样不需要结束条件,
leave语句的意义是离开循环。
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc6 () -
-> -
-> v int; -
-> v=0; -
-> LOOP_LABLE:loop -
-> into t values(v); -
-> v=v+1; -
-> if v >=5 -
-> leave LOOP_LABLE; -
-> if; -
-> loop; -
-> -
-> // -
mysql
> DELIMITER ;
Ⅳ. LABLES 标号:
标号可以用在begin repeat while 或者loop 语句前,语句标号只能在合法的语句前面使用。可以跳出循环,使运行指令达到复合语句的最后一步。
(4). ITERATE迭代
Ⅰ. ITERATE:
通过引用复合语句的标号,来从新开始复合语句
-
mysql
> DELIMITER // -
mysql
> CREATEPROCEDURE proc10 () -
-> -
-> v int; -
-> v=0; -
-> LOOP_LABLE:loop -
-> if v=3 -
-> v=v+1; -
-> ITERATE LOOP_LABLE; -
-> if; -
-> into t values(v); -
-> v=v+1; -
-> if v>=5 -
-> leave LOOP_LABLE; -
-> if; -
-> loop; -
-> -
-> // -
mysql
> DELIMITER ;
9.
(1).字符串类
CHARSET(str) //返回字串字符集
CONCAT (string2 [,... ]) //连接字串
INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0
LCASE (string2 ) //转换成小写
LEFT (string2 ,length ) //从string2中的左边起取length个字符
LENGTH (string ) //string长度
LOAD_FILE (file_name ) //从文件读取内容
LOCATE (substring , string [,start_position ] ) 同INSTR,但可指定开始位置
LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length
LTRIM (string2 ) //去除前端空格
REPEAT (string2 ,count ) //重复count次
REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str
RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length
RTRIM (string2 ) //去除后端空格
STRCMP (string1 ,string2 ) //逐字符比较两字串大小,
SUBSTRING (str , position [,length ]) //从str的position开始,取length个字符,
注:mysql中处理字符串时,默认第一个字符下标为1,即参数position必须大于等于1
-
mysql>
select substring('abcd',0,2); -
+-----------------------+
-
|
substring('abcd',0,2) | -
+-----------------------+
-
|
| -
+-----------------------+
-
1
row inset (0.00 sec) -
-
mysql>
select substring('abcd',1,2); -
+-----------------------+
-
|
substring('abcd',1,2) | -
+-----------------------+
-
|
ab | -
+-----------------------+
-
1
row inset (0.02 sec)
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2)
//去除指定位置的指定字符
UCASE (string2 ) //转换成大写
RIGHT(string2,length) //取string2最后length个字符
SPACE(count) //生成count个空格
(2).数学类
ABS (number2 ) //绝对值
BIN (decimal_number ) //十进制转二进制
CEILING (number2 ) //向上取整
CONV(number2,from_base,to_base) //进制转换
FLOOR (number2 ) //向下取整
FORMAT (number,decimal_places ) //保留小数位数
HEX (DecimalNumber ) //转十六进制
注:HEX()中可传入字符串,则返回其ASC-11码,如HEX('DEF')返回4142143
也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19
LEAST (number , number2 [,..]) //求最小值
MOD (numerator ,denominator ) //求余
POWER (number ,power ) //求指数
RAND([seed]) //随机数
ROUND (number [,decimals ]) //四舍五入,decimals为小数位数]
注:返回类型并非均为整数,如:
(1)默认变为整形值
-
mysql>
select round(1.23); -
+-------------+
-
|
round(1.23) | -
+-------------+
-
|
1 | -
+-------------+
-
1
row inset (0.00 sec) -
-
mysql>
select round(1.56); -
+-------------+
-
|
round(1.56) | -
+-------------+
-
|
2 | -
+-------------+
-
1
row inset (0.00 sec)
(2)可以设定小数位数,返回浮点型数据
-
mysql>
select round(1.567,2); -
+----------------+
-
|
round(1.567,2) | -
+----------------+
-
|
1.57 | -
+----------------+
-
1
row inset (0.00 sec)
SIGN (number2 ) //
(3).日期时间类
ADDTIME (date2 ,time_interval ) //将time_interval加到date2CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE ( ) //当前日期
CURRENT_TIME ( ) //当前时间
CURRENT_TIMESTAMP ( ) //当前时间戳
DATE (datetime ) //返回datetime的日期部分
DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间
DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间
DATEDIFF (date1 ,date2 ) //两个日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1为星期天
DAYOFYEAR (date ) //一年中的第几天
EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分
MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串
MAKETIME (hour ,minute ,second ) //生成时间串
MONTHNAME (date ) //英文月份名
NOW ( ) //当前时间
SEC_TO_TIME (seconds ) //秒数转成时间
STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示
TIMEDIFF (datetime1 ,datetime2 ) //两个时间差
TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第几天
HOUR(datetime) //小时
LAST_DAY(date) //date的月的最后日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分返回符号,正负或0
SQRT(number2) //开平方

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
