MySQL: Check if a table exists

by Yang Yang on July 30, 2010

Share This Article:
Subscribe to Kavoir: blog feed

When I was creating an installation script, I needed to check if a table exists to make sure the installation had not been performed yet. How did I do that?

I use this simple query to get whether a table exists in the specified database:

SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_name'
AND table_name = 'table_name'

Just fill in the ‘db_name’ as well as ‘table_name’. If this query returns 1 row, the table db_name.table_name does exist, otherwise it does not.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 1 comment… read it below or add one }

Pradeep September 5, 2010 at 7:06 pm

Nice tip. Keep posting such helpful tricks.

Reply

Leave a Comment

Previous post:

Next post: