Search code examples
jdbcdatabase-schemahsqldb

HSQLDB equivalent to "Use <databasename>;"


I am trying to figure out how you tell HSQLDB which database to work with. With MySQL for example, I'd enter:

  use testbd;

Toe work only tables from "testdb".

When you start a HSQLDB session with the "HSQL Database Manager" shows all the schemas (databases). It then allows you to select a database to 'use'.

How can I just specify the database Testdb so my queries can look like:

  SELECT * FROM NAMES;

instead of long-winded:

  SELECT * FROM TESTDB.NAMES;

command? Also is there a different method to do that programaticly via JDBC, ODBC or Java?


Solution

  • The database is selected as part of the JDBC URL. You can not change that at runtime without creating a new connection.

    But you are mixing up terminology anyway

    SELECT * FROM TESTDB.NAMES 
    

    selects from a schema called testdb not from a "database" (MySQL doesn't distinguish between a database and a schema, that's probably why you mixed up the terminology as well)

    The SQL standard defines two namespace levels: catalog and schema.

    A fully qualified name is database_name.schema_name.object_name. You can only leave out parts from the beginning. So foo.bar references a table bar in the schema foo. As HSQLDB does not support more than one catalog per database (or actually: the current database is the "catalog") you can always leave out the catalog name when referencing a table.

    See the manual for details:
    http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_names_references

    To avoid having to specify the table's schema, you can change the current schema using the set schema statement:

    SET SCHEMA testdb;
    

    This is also documented in the manual:
    http://hsqldb.org/doc/2.0/guide/sessions-chapt.html#N10BBF