table_exists

NAME

table_exists(): Check if specified table exists

TYPE

Function

DESCRIPTION

table_exists() provides with a quick and efficient boolean check for existence of a table or a view.

It requires both input parameters to identify existing object (no wildcards, NULLs or empty strings allowed), and uses INFORMATION_SCHEMA optimizations to look them up. In particular, no directories are scanned and no tables are opened by the execution of this function.

The function makes no distinction between a table and a view. Any such distinction would require opening the table definition file.

SYNOPSIS

table_exists(lookup_table_schema varchar(64) charset utf8, lookup_table_name varchar(64) charset utf8) 
  RETURNS TINYINT UNSIGNED

Input:

  • lookup_table_schema: name of schema (database)
  • lookup_table_name: name of table to look for within said schema

Output: boolean: 1 is indicated table/view exists, 0 otherwise.

EXAMPLES

Qualify a GRANTEE:

mysql> SELECT table_exists('sakila', 'rental') AS does_it_exist;
+---------------+
| does_it_exist |
+---------------+
|             1 |
+---------------+

mysql> SELECT table_exists('sakila', 'zzzztttt') AS does_it_exist;
+---------------+
| does_it_exist |
+---------------+
|             0 |
+---------------+

ENVIRONMENT

MySQL 5.1 or newer

SEE ALSO

Schema analysis, table_rotate

AUTHOR

Shlomi Noach
 
common_schema documentation