Rechercher dans le manuel MySQL

27.4.4.26 The table_exists() Procedure

Tests whether a given table exists as a regular table, a TEMPORARY table, or a view. The procedure returns the table type in an OUT parameter. If both a temporary and a permanent table exist with the given name, TEMPORARY is returned.

Parameters
  • in_db VARCHAR(64): The name of the database in which to check for table existance.

  • in_table VARCHAR(64): The name of the table to check the existance of.

  • out_exists ENUM('', 'BASE TABLE', 'VIEW', 'TEMPORARY'): The return value. This is an OUT parameter, so it must be a variable into which the table type can be stored. When the procedure returns, the variable has one of the following values to indicate whether the table exists:

    • '': The table name does not exist as a base table, TEMPORARY table, or view.

    • BASE TABLE: The table name exists as a base (permanent) table.

    • VIEW: The table name exists as a view.

    • TEMPORARY: The table name exists as a TEMPORARY table.

Contents Haut

Example
  1. mysql> CREATE DATABASE db1;
  2. Query OK, 1 row affected (0.01 sec)
  3.  
  4. mysql> USE db1;
  5. Database changed
  6. mysql> CREATE TABLE t1 (id INT PRIMARY KEY);
  7. Query OK, 0 rows affected (0.03 sec)
  8.  
  9. mysql> CREATE TABLE t2 (id INT PRIMARY KEY);
  10. Query OK, 0 rows affected (0.20 sec)
  11.  
  12. mysql> CREATE view v_t1 AS SELECT * FROM t1;
  13. Query OK, 0 rows affected (0.02 sec)
  14.  
  15. Query OK, 0 rows affected (0.00 sec)
  16.  
  17. mysql> CALL sys.table_exists('db1', 't1', @exists); SELECT @exists;
  18. Query OK, 0 rows affected (0.01 sec)
  19.  
  20. +-----------+
  21. | @exists   |
  22. +-----------+
  23. +-----------+
  24. 1 row in set (0.00 sec)
  25.  
  26. mysql> CALL sys.table_exists('db1', 't2', @exists); SELECT @exists;
  27. Query OK, 0 rows affected (0.02 sec)
  28.  
  29. +------------+
  30. | @exists    |
  31. +------------+
  32. | BASE TABLE |
  33. +------------+
  34. 1 row in set (0.00 sec)
  35.  
  36. mysql> CALL sys.table_exists('db1', 'v_t1', @exists); SELECT @exists;
  37. Query OK, 0 rows affected (0.02 sec)
  38.  
  39. +---------+
  40. | @exists |
  41. +---------+
  42. | VIEW    |
  43. +---------+
  44. 1 row in set (0.00 sec)
  45.  
  46. mysql> CALL sys.table_exists('db1', 't3', @exists); SELECT @exists;
  47. Query OK, 0 rows affected (0.00 sec)
  48.  
  49. +---------+
  50. | @exists |
  51. +---------+
  52. |         |
  53. +---------+
  54. 1 row in set (0.00 sec)

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-sys-table-exists.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut