table_exists()过程
测试给定表是否以常规表,TEMPORARY表或视图的形式存在。该过程在OUT参数中返回表类型。如果同时存在具有给定名称的临时表和永久表,TEMPORARY则返回。
参量
in_db VARCHAR(64):要在其中检查表是否存在的数据库的名称。in_table VARCHAR(64):要检查其存在的表的名称。out_exists ENUM('','BASE TABLE','VIEW','TEMPORARY'):返回值。这是一个OUT参数,因此它必须是一个可以将表类型存储到其中的变量。该过程返回时,该变量具有以下值之一以指示该表是否存在:'':表名不作为基本表,TEMPORARY表或视图存在。BASE TABLE:表名作为基础(永久)表存在。VIEW:表名称作为视图存在。TEMPORARY:表名作为TEMPORARY表存在。
例
mysql>CREATE DATABASE db1; Query OK, 1 row affected (0.01 sec) mysql>USE db1;Database changed mysql>CREATE TABLE t1 (id INTPRIMARY KEY ); Query OK, 0 rows affected (0.03 sec) mysql>CREATE TABLE t2 (id INTPRIMARY KEY ); Query OK, 0 rows affected (0.20 sec) mysql>CREATE view v_t1AS SELECT *FROM t1; Query OK, 0 rows affected (0.02 sec) mysql>CREATE TEMPORARY TABLE t1 (id INTPRIMARY KEY ); Query OK, 0 rows affected (0.00 sec) mysql>CALL sys.table_exists('db1', 't1', @exists);SELECT @exists; Query OK, 0 rows affected (0.01 sec) +----------- + | @exists | +----------- + | TEMPORARY | +----------- + 1 row in set (0.00 sec) mysql>CALL sys.table_exists('db1', 't2', @exists);SELECT @exists; Query OK, 0 rows affected (0.02 sec) +------------ + | @exists | +------------ + | BASE TABLE | +------------ + 1 row in set (0.00 sec) mysql>CALL sys.table_exists('db1', 'v_t1', @exists);SELECT @exists; Query OK, 0 rows affected (0.02 sec) +--------- + | @exists | +--------- + | VIEW | +--------- + 1 row in set (0.00 sec) mysql>CALL sys.table_exists('db1', 't3', @exists);SELECT @exists; Query OK, 0 rows affected (0.00 sec) +--------- + | @exists | +--------- + | | +--------- + 1 row in set (0.00 sec)
