List:General Discussion« Previous MessageNext Message »
From:Christian Mack Date:June 8 1999 12:02pm
Subject:Re: Table exists ?
View as plain text  
Derek Lavine wrote:
> 
> Hi,
> 
> I would like to check if a table exists before I do a
> 
> create table mytbl
> ( ... );
> 
> or drop table mytbl;
> 
> would someone be so kind as to tell me how.
> 
> I guess I have to query a mysql table to see if the table i am about  to
> create or drop are in it, but I don't know which table or what column of
> that table
> 
> regards
> 
> derek

Hi Derek

You have multiple possibilities.
You can use:
SHOW TABLES
to get a list of all tables in this database.
With this list you can check on the client side, if the choosen table exists.

Or you can use:
SHOW TABLES LIKE 'choosenTable'
to get an empty result set if table choosenTable doesn't exist else you will get one row
with 'choosenTable' in it.

Or you can do:
SELECT count(*) FROM choosenTable
This will give you an error, if 'choosenTable' doesn't exist, else it will give you the
number of rows in it.

If you only want to prevent an error wile creating the table, you can use:
DROP TABLE IF EXISTS choosenTable
before the CREATE statement.
This will not generate an error, if the table 'choosenTable' doesn't exist.
Afterwards table choosenTable will definitely not be there.

Tschau
Christian

Thread
Table exists ?Derek Lavine8 Jun
  • Re: Table exists ?Christian Mack8 Jun
  • Re: Table exists ?Jim Faucette8 Jun
  • RE: Table exists ?Jeff Magnusson8 Jun
  • Re: Table exists ?Derek Lavine9 Jun
    • Re: Table exists ?Paul DuBois9 Jun
    • Re: Table exists ?Michael Widenius9 Jun
  • Re: Table exists ?Sasha Pachev9 Jun
  • Re: Table exists ?Ronald Beck9 Jun
RE: Table exists ?tcobb9 Jun