HSQL Database
HSQL Database
HSQLDB is a database written in Java that can be embedded in your application or run in clientserver mode. HSQLDB is small and fast, faster than Derby. It is included in Grails, Play
Framework, and many open-source applications.
Install HSQLDB
1. Download HSQLDB from www.hsqldb.org. It is distributed as a ZIP file.
2. Unzip the file to a directory where you store software libraries, such as /opt (Linux) or
URL
jdbc:hsqldb:file:/path/to/database
Derby Database
-1-
In memory
jdbc:hsqldb:mem:databasename
Resource (read-only)
jdbc:hsqldb:res:org.me.app.resource
jdbc:hsqldb:hsql://hostname/database
jdbc:hsqldb:hsqls://hostname/database
Database settings
database.script
database.log
database.data
table data
database.backup
database.lobs
large objects
Derby Database
-2-
Data in the .log file is incorporated into the database (and the log file removed) at normal
SHUTDOWN (using a shutdown command). While the database is open, HSQLDB also creates a
lock file:
database.lck
Closing a Database
HSQLDB has a SHUTDOWN command to close the database and changes written to the data file.
Any uncommitted transactions are rolled back.
The command SHUTDOWN COMPACT closes the database and rewrites the data file to compact
it. This command should be used when lots of inserts, updates, and deletes have been performed,
and used periodically.
The database is not automatically closed when the last JDBC connection is closed. To specify that
the database be closed when last connection is closed add the stutdown=true property to the
connection URL, e.g.:
Connection conn = DriverManager.getConnection(
"jdbc>hsqldb:file:/data/mydatabase/world;stutdown=true", "SA", "");
You can issue SHUTDOWN [COMPACT] in the interactive GUI tool or using the execute
method of an SQL Statement object.
Statement statement = conn.createStatement( );
statement.execute( "SHUTDOWN" );
-3-
Using Indices
Indices speed up queries and JOINs. An index is created by default for primary keys, foreign keys,
and constraints. Don't explicitly create an index for these. For cases where you plan to use a JOIN,
you are advised to create an index for the JOIN column of the second table.
If you plan to use:
SELECT .... FROM Table1
JOIN Table2
ON Table1.column1 = Table2.column2
then HSQLDB recommends you create an index for column2 of Table2 using:
CREATE INDEX idx_column2 ON Table2(column2);
-4-
7. Execute the country-data.sql script to insert data into the Country and
CountryLanguage tables.
8. Verify that the Country table contains 239 records and CountryLanguage contains approximately
986 records (answer may differ because of ongoing corrections to CountryLanguage data).
9. Execute a SHUTDOWN COMPACT command to save the data to disk.
Resources
1. HSQLDB User's Guide included with the HSQLDB distribution is easy to read and the only
documentation you need to use HyperSQL.
2. For SQL, HSQLDB recommends the "PostreSQL: Introduction and Concepts" manual on the
PostgreSQL web (http://www.postgresql.org/files/documentation/bools/aw_pgsql).
Derby Database
-5-