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