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.
How to check whether a table (or view) exists, and the current user has access to it? SELECT EXISTS ( SELECT FROM information_schema. tables WHERE table_schema = 'schema_name' AND table_name = 'table_name' ); The information schema is mainly useful to stay portable across major versions and across different RDBMS.
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'TableName'
AND TABLE_SCHEMA = 'public'
CREATE TABLE IF NOT EXISTS ...
is not a standard SQL code.
The thing to do is to check if the table is already in the catalogue.
For instance, in Java you may do something like:
connection.getMetaData().getTables(connection.getCatalog(), null, null, null)
For more info see javadoc java.sql.Connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With