Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

Is there a way to check if a table exists without selecting and checking values from it?

That is, I know I can go SELECT testcol FROM testtable and check the count of fields returned, but it seems there must be a more direct / elegant way to do it.

1 Answer

0 votes
by (40.7k points)

Try this code:

SELECT 1 FROM testtable LIMIT 1;

You don't need to count anything.If there's no error, then the table exists.You can use INFORMATION_SCHEMA, if you want to be correct like this:

SELECT * 

FROM information_schema.tables

WHERE table_schema = 'yourdb' 

    AND table_name = 'testtable'

LIMIT 1;

Otherwise, you can also use SHOW TABLES like this :

SHOW TABLES LIKE 'yourtable';

If there is a row in the resultset, table exists.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...