CALL
CREATE
DROP
CALL
;
BEGIN ...
END
SET
REPEAT
SET
CREATE PROCEDURE dorepeat(p1 INT)BEGIN SET @x = 0; REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;END;
delimiter
//
;
mysql>delimiter //
mysql>CREATE PROCEDURE dorepeat(p1 INT)
->BEGIN
->SET @x = 0;
->REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
->END
->//
Query OK, 0 rows affected (0.00 sec) mysql>delimiter ;
mysql>CALL dorepeat(1000);
Query OK, 0 rows affected (0.00 sec) mysql>SELECT @x;
+------+ | @x | +------+ | 1001 | +------+ 1 row in set (0.00 sec)
//
delimiter
mysql>CREATE FUNCTION hello (s CHAR(20))
mysql>RETURNS CHAR(50) DETERMINISTIC
->RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec) mysql>SELECT hello('world');
+----------------+ | hello('world') | +----------------+ | Hello, world! | +----------------+ 1 row in set (0.00 sec)
CREATE
PROCEDURE
CREATE
FUNCTION
CALL
DROP
PROCEDURE
DROP
FUNCTION
ALTER PROCEDURE
ALTER FUNCTION
USE
db_name
USE
p
test
test.f()
max_sp_recursion_depth
max_sp_recursion_depth
thread_stack
SELECT
SELECT
mysql_real_connect()
CREATE ROUTINE
ALTER ROUTINE
EXECUTE
DEFINER
automatic_sp_privileges
EXECUTE
ALTER ROUTINE
CREATE
ROUTINES
SHOW CREATE PROCEDURE
SHOW CREATE FUNCTION
SHOW PROCEDURE STATUS
SHOW FUNCTION STATUS
LAST_INSERT_ID()
LAST_INSERT_ID()
LAST_INSERT_ID()
INSERT
LOAD
DATA
NDB
INFORMATION_SCHEMA
CREATE TRIGGER
DROP TRIGGER
INSERT
MySQL的> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
查询行,0行受影响(0.03秒)MySQL > CREATE TRIGGER ins_sum BEFORE INSERT ON account
FOR EACH ROW SET @sum = @sum + NEW.amount;
查询行,0行受影响(0.01秒)
CREATE TRIGGER
account
INSERT
MySQL的> SET @sum = 0;
MySQL的> INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
MySQL的> SELECT @sum AS 'Total amount inserted';
----------------------- |总量插入| ----------------------- | 1852.48 | -----------------------
@sum
INSERT
1852.48
DROP
TRIGGER
MySQL的> DROP TRIGGER test.ins_sum;
BEFORE UPDATE
FOLLOWS
FOLLOWS
BEFORE INSERT
mysql>CREATE TRIGGER ins_transaction BEFORE INSERT ON account
FOR EACH ROW PRECEDES ins_sum
SET
@deposits = @deposits + IF(NEW.amount>0,NEW.amount,0),
@withdrawals = @withdrawals + IF(NEW.amount<0,-NEW.amount,0);
Query OK, 0 rows affected (0.01 sec)
ins_transaction
PRECEDES
ins_sum
OLD
OLD
INSERT
col_name
OLD.
col_name
OLD.
col_name
col_name
OLD
SELECT
SELECT
SET NEW.
col_name
=
value
UPDATE
AFTER
BEFORE
AUTO_INCREMENT
BEGIN ...
END
;
BEFORE
MySQL的> delimiter //
MySQL的> CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END;//
MySQL的> delimiter ;
CALL
CALL
INOUT
START
TRANSACTION
COMMIT
ROLLBACK
ROLLBACK to
SAVEPOINT
BEFORE
BEFORE
AFTER
BEFORE
testref
CREATE TABLE test1(a1 INT);CREATE TABLE test2(a2 INT);CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);CREATE TABLE test4( a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY, b4 INT DEFAULT 0);delimiter |CREATE TRIGGER testref BEFORE INSERT ON test1 FOR EACH ROW BEGIN INSERT INTO test2 SET a2 = NEW.a1; DELETE FROM test3 WHERE a3 = NEW.a1; UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1; END;|delimiter ;INSERT INTO test3 (a3) VALUES (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL), (NULL);INSERT INTO test4 (a4) VALUES (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);
test1
MySQL的> INSERT INTO test1 VALUES
(1), (3), (1), (7), (1), (8), (4), (4);
查询行,8行受影响(0.01秒)记录:8份:0警告:0
mysql>SELECT * FROM test1;
+------+ | a1 | +------+ | 1 | | 3 | | 1 | | 7 | | 1 | | 8 | | 4 | | 4 | +------+ 8 rows in set (0.00 sec) mysql>SELECT * FROM test2;
+------+ | a2 | +------+ | 1 | | 3 | | 1 | | 7 | | 1 | | 8 | | 4 | | 4 | +------+ 8 rows in set (0.00 sec) mysql>SELECT * FROM test3;
+----+ | a3 | +----+ | 2 | | 5 | | 6 | | 9 | | 10 | +----+ 5 rows in set (0.00 sec) mysql>SELECT * FROM test4;
+----+------+ | a4 | b4 | +----+------+ | 1 | 3 | | 2 | 0 | | 3 | 1 | | 4 | 2 | | 5 | 0 | | 6 | 0 | | 7 | 1 | | 8 | 1 | | 9 | 0 | | 10 | 0 | +----+------+ 10 rows in set (0.00 sec)
events
crontab
BEGIN ...
END
GET_LOCK()
ALTER EVENT
EVENT
PROCESS
SHOW PROCESSLIST
event_scheduler
ON
ON
SHOW PROCESSLIST
MySQL的> SHOW PROCESSLIST\G
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××1。××××××××××××××××××××××××一×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××:1根用户ID:本地主机:nullcommand DB查询时间:0::::processlist状态信息显示为×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××2。××××××××××××××××××××××××一×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××身份证号码:2:事件调度的用户:本地主机:_ DB:时间:3 nullcommand守护进程的状态:等待下一行信息:null2在激活集(0秒)
event_scheduler
OFF
SHOW PROCESSLIST
event_scheduler
event_scheduler
DISABLED
SHOW PROCESSLIST
DISABLED
event_scheduler
OFF
SET
OFF
ON
SET GLOBAL event_scheduler = ON; SET @@global.event_scheduler = ON; SET GLOBAL event_scheduler = 1; SET @@global.event_scheduler = 1;
SET GLOBAL event_scheduler = OFF; SET @@global.event_scheduler = OFF; SET GLOBAL event_scheduler = 0; SET @@global.event_scheduler = 0;
ON
event_scheduler
SELECT
SHOW
VARIABLES
ON
DISABLED
OFF
0
event_scheduler
mysql<SET @@event_scheduler = OFF;
错误1229(hy000):变“event_scheduler '是一个全局变量,应集全球
DISABLED
event_scheduler
OFF
DISABLED
event_scheduler
--event-scheduler=DISABLED
my.cnf
[mysqld]
event_scheduler=DISABLED
--event-scheduler=DISABLED
event-scheduler=DISABLED
1
0
event_scheduler
ON
--skip-grant-tables
event_scheduler
my.cnf
EVENTS
CREATE
EVENT
ALTER EVENT
DROP EVENT
ON
COMPLETION
EVENT
EVENTS
time_zone
time_zone
CREATE EVENT
ALTER EVENT
mysql.event
starts
last_executed
mysql.event
INFORMATION_SCHEMA.EVENTS
SHOW EVENTS
mysql.event | INFORMATION_SCHEMA.EVENTS | SHOW EVENTS | |
---|---|---|---|
Events status: LLA = Last Locked At LUA = Last Unlocked At WOC = Waiting On Condition DL = Data Locked Event scheduler status: State : INITIALIZED Thread id : 0 LLA : init_scheduler:313 LUA : init_scheduler:318 WOC : NO Workers : 0 Executed : 0 Data locked: NO Event queue status: Element count : 1 Data locked : NO Attempting lock : NO LLA : init_queue:148 LUA : init_queue:168 WOC : NO Next activation : 0000-00-00 00:00:00
SELECT ... INTO
var_list
Result consisted of more than one row
LIMIT
1
event_scheduler
SYSTEM_VARIABLES_ADMIN
SUPER
EVENT
GRANT
GRANT
EVENT
jon@ghidora
格兰特事件对myschema。*乔恩@姬度拉;
EVENT
格兰特事件我们*。* ghidora乔恩”;
EVENT
MySQL的> GRANT EVENT ON myschema.mytable TO jon@ghidora;
错误1144(42000):非法授予/撤销命令;请咨询手册看到这特权可以用
jon@ghidora
EVENT
SELECT
jon@ghidora
创建事件e_store_ts进度每10秒做插入myschema.mytable值(unix_timestamp());
SELECT * FROM mytable;
INSERT
hostname
.err
2013-09-24t12:41: 31.261992z 25 [错误]事件调度器:[乔恩@姬度拉] [菜谱。e_store_ts ]插入命令拒绝user'jon”@ 'ghidora”表“表'2013-09-24t12:41: 31.262022z 25 [注]事件调度器:[乔恩@姬度拉]。[ myschema。e_store_ts ]事件执行失败。2013-09-24t12:41: 41.271796z 26 [错误]事件调度器:[乔恩@姬度拉] [菜谱。e_store_ts ]插入命令拒绝user'jon”@ 'ghidora”表“表'2013-09-24t12:41: 41.272761z 26 [注]事件调度器:[乔恩@姬度拉]。[ myschema e_store_ts ]活动执行失败。
mysql> INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP());
ERROR 1142 (42000): INSERT command denied to user
'jon'@'ghidora' for table 'mytable'
INFORMATION_SCHEMA.EVENTS
LAST_EXECUTED
mysql>SELECT * FROM INFORMATION_SCHEMA.EVENTS
>WHERE EVENT_NAME='e_store_ts'
>AND EVENT_SCHEMA='myschema'\G
*************************** 1. row *************************** EVENT_CATALOG: NULL EVENT_SCHEMA: myschema EVENT_NAME: e_store_ts DEFINER: jon@ghidora EVENT_BODY: SQL EVENT_DEFINITION: INSERT INTO myschema.mytable VALUES (UNIX_TIMESTAMP()) EVENT_TYPE: RECURRING EXECUTE_AT: NULL INTERVAL_VALUE: 5 INTERVAL_FIELD: SECOND SQL_MODE: NULL STARTS: 0000-00-00 00:00:00 ENDS: 0000-00-00 00:00:00 STATUS: ENABLED ON_COMPLETION: NOT PRESERVE CREATED: 2006-02-09 22:36:06 LAST_ALTERED: 2006-02-09 22:36:06 LAST_EXECUTED: NULL EVENT_COMMENT: 1 row in set (0.00 sec)
EVENT
REVOKE
EVENT
jon@ghidora
撤销事件myschema。*乔恩@姬度拉;
jon@ghidora
EVENT
INSERT
CREATE EVENT e_insert ON SCHEDULE EVERY 7 SECOND DO INSERT INTO myschema.mytable;
root
EVENT
e_insert
root
DROP USER jon@ghidora;
RENAME USER jon@ghidora TO
someotherguy@ghidora;
mysql.event
INFORMATION_SCHEMA.EVENTS
DROP USER
RENAME USER
mysql.event
e_insert
DELETE FROM mysql.event WHERE db = 'myschema' AND definer = 'jon@ghidora' AND name = 'e_insert';
mysql.event
EVENT
mysql.user
Y
N
Y
EVENT
EVENT
GRANT
Db
Event_priv
GRANT
EVENT
Com_create_event
CREATE EVENT
Com_alter_event
ALTER EVENT
Com_drop_event
DROP EVENT
Com_show_create_event
SHOW CREATE EVENT
Com_show_events
SHOW EVENTS
SHOW STATUS LIKE
'%event%';
CREATE VIEW
ALTER VIEW
DROP
VIEW
SELECT
UNION
SELECT
MySQL的> CREATE TABLE t (qty INT, price INT);
MySQL的> INSERT INTO t VALUES(3, 50), (5, 60);
MySQL的> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
MySQL的> SELECT * FROM v;
------ ------- ------- |数量|价格|价值| ------ ------- ------- |三|五十| 150 | |五|六十| 300 | ------ ------- ------- MySQL > SELECT * FROM v WHERE qty = 5;
------ ------- ------- |数量|价格|价值| ------ ------- ------- |五|六十| 300 | ------ ------- -------
ALGORITHM
CREATE VIEW
ALTER VIEW
MERGE
UNDEFINED
MERGE
TEMPTABLE
UNDEFINED
TEMPTABLE
ALGORITHM
optimizer_switch
TEMPTABLE
UNDEFINED
ALGORITHM
CREATE VIEW
CREATE VIEW
ALGORITHM = UNDEFINED
ALGORITHM = MERGE
MERGE
v_merge
CREATE ALGORITHM = MERGE VIEW v_merge (vc1, vc2) ASSELECT c1, c2 FROM t WHERE c3 > 100;
SELECT * FROM v_merge;
v_merge
*
c1, c2
WHERE
SELECT c1, c2 FROM t WHERE c3 > 100;
SELECT * FROM v_merge WHERE vc1 < 100;
vc1 < 100
c1 < 100
WHERE
AND
SELECT c1, c2 FROM t WHERE (c3 > 100) AND (c1 < 100);
WHERE
在(选择)和(视图)
MERGE
LIMIT
UPDATE
DELETE
INSERT
UPDATE
DELETE
DEFAULT
MERGE
UNION
UNION ALL
INSERT
3.14159
col1 + 3
UPPER(col2)
col3 / col4
(subquery
)
CREATE VIEW
UPDATE
DELETE
IS_UPDATABLE
INFORMATION_SCHEMA.VIEWS
UPDATE
DELETE
INSERT
updatable_views_with_limit
CREATE TABLE t1 (x INTEGER); CREATE TABLE t2 (c INTEGER); CREATE VIEW vmat AS SELECT SUM(x) AS s FROM t1; CREATE VIEW vup AS SELECT * FROM t2; CREATE VIEW vjoin AS SELECT * FROM vmat JOIN vup ON vmat.s=vup.c;
INSERT
INSERT
INSERT
INSERT INTO vjoin (c) VALUES (1);
INSERT INTO vup (c) VALUES (1);
UPDATE
UPDATE
INSERT
UPDATE
c
UPDATE vjoin SET c=c+1;
x
UPDATE vjoin SET x=x+1;
UPDATE
UPDATE vup JOIN (SELECT SUM(x) AS s FROM t1) AS dt ON ... SET c=c+1;
UPDATE vup JOIN (SELECT SUM(x) AS s FROM t1) AS dt ON ... SET s=s+1;
DELETE
DELETE
INSERT
UPDATE
DELETE vjoin WHERE ...;
DELETE vup WHERE ...;
DELETE vup FROM vup JOIN (SELECT SUM(x) AS s FROM t1) AS dt ON ...;
CREATE VIEW v AS SELECT col1, 1 AS col2 FROM t;
col2
UPDATE v SET col1 = 0;
UPDATE v SET col2 = 0;
AUTO_INCREMENT
LAST_INSERT_ID()
WITH CHECK OPTION
select_statement
WITH CHECK OPTION
CASCADED
WITH CHECK OPTION
LOCAL
CASCADED
WITH CASCADED
CHECK OPTION
WHERE
CREATE TABLE t1 (a INT); CREATE VIEW v1 AS SELECT * FROM t1 WHERE a < 2 WITH CHECK OPTION; CREATE VIEW v2 AS SELECT * FROM v1 WHERE a > 0 WITH LOCAL CHECK OPTION; CREATE VIEW v3 AS SELECT * FROM v1 WHERE a > 0 WITH CASCADED CHECK OPTION;
v2
v1
v2
v1
v3
MySQL的> INSERT INTO v2 VALUES (2);
错误1369年(hy000):检查选项失败的试验。v2'mysql > INSERT INTO v3 VALUES (2);
错误1369(hy000):检查选项失败的试验。V3”
DEFINER
DEFINER
SQL SECURITY
INVOKER
SQL SECURITY
CREATE DEFINER = 'admin'@'localhost' PROCEDURE p1() SQL SECURITY DEFINER BEGIN UPDATE t1 SET counter = counter + 1; END;
EXECUTE
CALL
'admin'@'localhost'
EXECUTE
UPDATE
p1
INVOKER
CREATE DEFINER = 'admin'@'localhost' PROCEDURE p2()SQL SECURITY INVOKERBEGIN UPDATE t1 SET counter = counter + 1;END;
p2
DEFINER
p2
EXECUTE
UPDATE
DEFINER
DEFINER
SET_USER_ID
SUPER
SET_USER_ID
SUPER
CURRENT_USER
SQL SECURITY
INVOKER
SET_USER_ID
SUPER
DEFINER
DEFINER
SET_USER_ID
SUPER
DEFINER
SET_USER_ID
SUPER
TRIGGER
CALL
binlog_format=MIXED
SET_USER_ID
SUPER
CREATE ROUTINE
ALTER ROUTINE
SET_USER_ID
SUPER
CREATE
FUNCTION
NO SQL
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
CREATE FUNCTION f1(i INT) RETURNS INT DETERMINISTIC READS SQL DATA BEGIN RETURN i; END;
UUID()
创建功能f2()返回char(36)字符集utf8begin返回uuid();结束;
CREATE FUNCTION f3(p_id INT) RETURNS INT BEGIN UPDATE t SET modtime = NOW() WHERE id = p_id; RETURN ROW_COUNT(); END;
DETERMINISTIC
binlog_format=STATEMENT
log_bin_trust_function_creators=1
DETERMINISTIC
DETERMINISTIC
binlog_format=STATEMENT
SUPER
log_bin_trust_function_creators
MySQL的> SET GLOBAL log_bin_trust_function_creators = 1;
--log-bin-trust-function-creators=1
log_bin_trust_function_creators
SUPER
CREATE TRIGGER
UUID()
CREATE
TRIGGER
CREATE
CREATE EVENT
CREATE PROCEDURE
CREATE FUNCTION
ALTER EVENT
ALTER PROCEDURE
ALTER FUNCTION
DROP EVENT
DROP PROCEDURE
DROP FUNCTION
SELECT
SELECT
SELECT
func_name
CREATE FUNCTION f1(a INT) RETURNS INTBEGIN IF (a < 3) THEN INSERT INTO t2 VALUES (a); END IF; RETURN 0;END;CREATE TABLE t1 (a INT);INSERT INTO t1 VALUES (1),(2),(3);SELECT f1(a) FROM t1;
SELECT
SELECT
选择F1(1);(2)选择F1;
SELECT
SELECT
CREATE ROUTINE
mysql>delimiter //
mysql>CREATE FUNCTION unsafe_func () RETURNS INT
->BEGIN
->IF @@server_id=2 THEN
->dangerous_statement
; END IF;RETURN 1;
->END;
->//
mysql>delimiter ;
mysql>INSERT INTO t VALUES(unsafe_func());
CREATE FUNCTION
INSERT
SUPER
CREATE ROUTINE
ALTER FUNCTION
SUPER
ALTER ROUTINE
SUPER
错误1419(hy000):你没有超级特权和启用了日志记录(你可能想使用更少的safelog_bin_trust_function_creators变量)
SUPER
CREATE
ROUTINE
log_bin_trust_function_creators
--log-bin-trust-function-creators=1
log_bin_trust_function_creators
SUPER
DETERMINISTIC
NOT DETERMINISTIC
CONTAINS SQL
READS SQL DATA
NO SQL
CONTAINS
SQL
CREATE
FUNCTION
NO SQL
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
log_bin_trust_function_creators
CALL
CALL
NAME_CONST(var_name
,var_value
)
var_name
var_value
NAME_CONST()
var_value
var_name
MySQL的> SELECT NAME_CONST('myname', 14);
“……………………”
NAME_CONST()
NAME_CONST()
CREATE TABLE
... SELECT
NAME_CONST()
CREATE TABLE t1 SELECT myvar;
CREATE TABLE t1 SELECT NAME_CONST(myvar, 1);
CREATE TABLE t1 SELECT myvar AS myvar;
CREATE TABLE t1 SELECT NAME_CONST(myvar, 1) AS myvar;
SET
value
SET @my_var = value
;
BEGIN
COMMIT
ROLLBACK
BEGIN
COMMIT
DO