"Create table if not exists" - how to check the schema, too?

Joonas Pulakka picture Joonas Pulakka · Mar 19, 2010 · Viewed 16.2k times · Source

Is there a (more or less) standard way to check not only whether a table named mytable exists, but also whether its schema is similar to what it should be? I'm experimenting with H2 database, and

CREATE TABLE IF NOT EXISTS mytable (....)

statements apparently only check for the table´s name. I would expect to get an exception if there's a table with the given name, but different schema.

Answer

isapir picture isapir · Sep 4, 2012
SELECT  *
FROM    INFORMATION_SCHEMA.TABLES
WHERE   TABLE_NAME      = 'TableName'
    AND TABLE_SCHEMA    = 'public'