Categories
SQL / MySQL Tips and Tutorials

MySQL: Check if a table exists

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.

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

One reply on “MySQL: Check if a table exists”

Comments are closed.