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