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.
0 comments:
Post a Comment