Michael Sheaver
Michael Sheaver

Reputation: 2119

Test for Existence of a Table

In my SQL script, how do I test for the existence of a table in the database? I would like to do something like the following:

IF EXISTS test4 THEN 
  GRANT DROP ON test4 FOR 'root'@'localhost;
  DROP test4;
ENDIF

Upvotes: 0

Views: 63

Answers (2)

Anup Shah
Anup Shah

Reputation: 1254

if you really need to add check you can use system catalog

select table_name from information_schema.tables 
where table_name='TABLENAME' and table_schema='DBNAME'

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

There's no need to check for a table's existence for grants -- assuming you even need them.

GRANT DROP ON test4 FOR ?
DROP test4 IF EXISTS

Upvotes: 1

Related Questions