mysql-prepare用法

一、基本用法

# 准备可以引用的语句,?代表参数:
# 准备好的语句特定于创建它的会话。 如果您终止会话而不释放先前准备的语句,则服务器会自动释放它。

# 准备好的语句对于会话也是全局的。 如果在存储过程中创建准备好的语句,则在存储过程结束时不会释放它。
PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';

# 设置变量
SET @a = 3;
SET @b = 4;

#调用准备好的语句
EXECUTE stmt1 USING @a, @b;

# 释放准备好的语句
{DEALLOCATE | DROP} PREPARE stmt_name
# DEALLOCATE PREPARE stmt1
# drop PREPARE stmt1

二、相关参数

# 最多允许的准备语句数量
max_prepared_stmt_count

三、prepare支持的sql语句

ALTER TABLE
ALTER USER
ANALYZE TABLE
CACHE INDEX
CALL
CHANGE MASTER
CHECKSUM {TABLE | TABLES}
COMMIT
{CREATE | DROP} INDEX
{CREATE | RENAME | DROP} DATABASE
{CREATE | DROP} TABLE
{CREATE | RENAME | DROP} USER
{CREATE | DROP} VIEW
DELETE
DO
FLUSH {TABLE | TABLES | TABLES WITH READ LOCK | HOSTS | PRIVILEGES
  | LOGS | STATUS | MASTER | SLAVE | DES_KEY_FILE | USER_RESOURCES}
GRANT
INSERT
INSTALL PLUGIN
KILL
LOAD INDEX INTO CACHE
OPTIMIZE TABLE
RENAME TABLE
REPAIR TABLE
REPLACE
RESET {MASTER | SLAVE | QUERY CACHE}
REVOKE
SELECT
SET
SHOW BINLOG EVENTS
SHOW CREATE {PROCEDURE | FUNCTION | EVENT | TABLE | VIEW}
SHOW {MASTER | BINARY} LOGS
SHOW {MASTER | SLAVE} STATUS
SLAVE {START | STOP}
TRUNCATE TABLE
UNINSTALL PLUGIN
UPDATE

官网链接: https://dev.mysql.com/doc/refman/5.7/en/sql-prepared-statements.html

你可能感兴趣的:(mysql,TIDB,数据库)