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.
You should also read:
- MySQL: Get the exact size of a database by SQL query
- Using SQL to Find records existing in one table but not in another
- MySQL: INSERT INTO … SELECT … FROM … Selectively on Table Columns / Fields to Combine Multiple Tables into One
- SQL: CREATE TABLE … SELECT … to generate new table from existing tables
- MySQL, PHP: Display MySQL table fields and data


Facebook
Twitter
Google Plus
{ 1 comment… read it below or add one }
Nice tip. Keep posting such helpful tricks.