2. Table of Contents
Quickstart............................................................................................................................................................................................... 9
The H2 Console Application................................................................................................................................................................. 9
Step-by-Step................................................................................................................................................................................ 9
Installation............................................................................................................................................................................. 9
Start the Console.................................................................................................................................................................... 9
Login................................................................................................................................................................................... 10
Sample................................................................................................................................................................................ 11
Execute................................................................................................................................................................................ 12
Disconnect........................................................................................................................................................................... 13
End..................................................................................................................................................................................... 13
Installation............................................................................................................................................................................................ 14
Requirements................................................................................................................................................................................... 14
Supported Platforms......................................................................................................................................................................... 14
Installing the Software...................................................................................................................................................................... 14
Directory Structure........................................................................................................................................................................... 14
Tutorial................................................................................................................................................................................................. 15
Starting and Using the H2 Console..................................................................................................................................................... 15
Firewall...................................................................................................................................................................................... 15
Native Version............................................................................................................................................................................ 15
Testing Java............................................................................................................................................................................... 15
Error Message 'Port is in use'....................................................................................................................................................... 16
Using another Port...................................................................................................................................................................... 16
Starting Successfully................................................................................................................................................................... 16
Connecting to the Server using a Browser..................................................................................................................................... 16
Multiple Concurrent Sessions....................................................................................................................................................... 16
Application Properties................................................................................................................................................................. 16
Login......................................................................................................................................................................................... 16
Error Messages........................................................................................................................................................................... 16
Adding Database Drivers............................................................................................................................................................. 16
Using the Application.................................................................................................................................................................. 17
Inserting Table Names or Column Names..................................................................................................................................... 17
Disconnecting and Stopping the Application.................................................................................................................................. 17
Connecting to a Database using JDBC................................................................................................................................................ 17
Creating New Databases................................................................................................................................................................... 17
Using the Server............................................................................................................................................................................... 17
Limitations of the Server............................................................................................................................................................. 17
Starting from Command Line....................................................................................................................................................... 17
Starting within an Application...................................................................................................................................................... 18
Stopping a TCP Server from Another Process................................................................................................................................ 18
Using Hibernate............................................................................................................................................................................... 18
Using Databases in Web Applications................................................................................................................................................. 18
Embedded Mode......................................................................................................................................................................... 18
Server Mode............................................................................................................................................................................... 19
Using a Servlet Listener to Start and Stop a Database.................................................................................................................... 19
CSV (Comma Separated Values) Support............................................................................................................................................ 19
Writing a CSV File from Within a Database.................................................................................................................................... 19
Reading a CSV File from Within a Database.................................................................................................................................. 19
Writing a CSV File from a Java Application.................................................................................................................................... 20
Reading a CSV File from a Java Application................................................................................................................................... 20
Features................................................................................................................................................................................................ 21
Feature List...................................................................................................................................................................................... 21
Main Features............................................................................................................................................................................ 21
Additional Features..................................................................................................................................................................... 21
SQL Support............................................................................................................................................................................... 21
Security Features........................................................................................................................................................................ 22
Other Features and Tools............................................................................................................................................................ 22
Comparison to Other Database Engines.............................................................................................................................................. 22
Products that Work with H2............................................................................................................................................................... 22
Why Java......................................................................................................................................................................................... 23
Connection Modes............................................................................................................................................................................ 23
Database URL Overview.................................................................................................................................................................... 23
Connecting to an Embedded (Local) Database............................................................................................................................... 24
Memory-Only Databases............................................................................................................................................................. 24
Connecting to a Database with File Encryption.................................................................................................................................... 24
Database File Locking....................................................................................................................................................................... 24
Opening a Database Only if it Already Exists....................................................................................................................................... 25
Closing the Database........................................................................................................................................................................ 25
Delayed Database Closing........................................................................................................................................................... 25
Don't Close the Database when the VM Exists............................................................................................................................... 25
Log Index Changes........................................................................................................................................................................... 25
Ignore Unknown Settings............................................................................................................................................................ 25
Changing Other Settings when Opening a Connection.................................................................................................................... 26
2 of 129
3. Multiple Connections......................................................................................................................................................................... 26
Opening Multiple Databases at the Same Time.............................................................................................................................. 26
Multiple Connections to the Same Database: Client/Server............................................................................................................. 26
Multithreading Support................................................................................................................................................................ 26
Locking, Lock-Timeout, Deadlocks................................................................................................................................................ 26
Database File Layout........................................................................................................................................................................ 27
Moving and Renaming Database Files........................................................................................................................................... 27
Backup...................................................................................................................................................................................... 27
Logging and Recovery...................................................................................................................................................................... 27
Compatibility Modes.......................................................................................................................................................................... 28
Using the Trace Options.................................................................................................................................................................... 28
Trace Options............................................................................................................................................................................. 28
Setting the Maximum Size of the Trace File................................................................................................................................... 28
Java Code Generation................................................................................................................................................................. 29
Enabling the Trace Option at Runtime by Manually Creating a File.................................................................................................. 29
Read Only Databases........................................................................................................................................................................ 29
Binary and Text Storage Formats....................................................................................................................................................... 29
Graceful Handling of Low Disk Space Situations.................................................................................................................................. 29
Opening a Corrupted Database.................................................................................................................................................... 30
Computed Columns / Function Based Index........................................................................................................................................ 30
Multi-Dimensional Indexes................................................................................................................................................................ 30
Using Passwords.............................................................................................................................................................................. 30
Using Secure Passwords.............................................................................................................................................................. 30
Passwords: Using Char Arrays instead of Strings........................................................................................................................... 30
Passing the User Name and/or Password in the URL...................................................................................................................... 31
User Defined Functions and Stored Procedures................................................................................................................................... 31
Function Data Type Mapping....................................................................................................................................................... 31
Functions that require a Connection............................................................................................................................................. 31
Functions throwing an Exception.................................................................................................................................................. 31
Functions returning a Result Set.................................................................................................................................................. 32
Using SimpleResultSet................................................................................................................................................................. 32
Using a Function as a Table......................................................................................................................................................... 32
Triggers........................................................................................................................................................................................... 33
Compacting a Database.................................................................................................................................................................... 33
Performance Comparison........................................................................................................................................................................ 34
Embedded....................................................................................................................................................................................... 34
Client-Server.................................................................................................................................................................................... 34
PolePosition Benchmark.................................................................................................................................................................... 34
Benchmark Results and Comments.................................................................................................................................................... 35
H2............................................................................................................................................................................................. 35
HSQLDB..................................................................................................................................................................................... 35
Derby........................................................................................................................................................................................ 35
PostgreSQL................................................................................................................................................................................ 35
MySQL....................................................................................................................................................................................... 35
Firebird...................................................................................................................................................................................... 36
Why Oracle / MS SQL Server / DB2 are Not Listed......................................................................................................................... 36
About this Benchmark....................................................................................................................................................................... 36
Number of Connections............................................................................................................................................................... 36
Real-World Tests........................................................................................................................................................................ 36
Comparing Embedded with Server Databases................................................................................................................................ 36
Test Platform............................................................................................................................................................................. 36
Multiple Runs............................................................................................................................................................................. 36
Memory Usage........................................................................................................................................................................... 36
Delayed Operations.................................................................................................................................................................... 36
Transaction Commit / Durability................................................................................................................................................... 36
Using Prepared Statements......................................................................................................................................................... 36
Currently Not Tested: Startup Time.............................................................................................................................................. 37
Advanced Topics.................................................................................................................................................................................... 38
Performance Tuning......................................................................................................................................................................... 38
Virus Scanners........................................................................................................................................................................... 38
Index Usage............................................................................................................................................................................... 38
Optimizer................................................................................................................................................................................... 38
Expression Optimization.............................................................................................................................................................. 38
COUNT(*) Optimization............................................................................................................................................................... 38
Updating Optimizer Statistics / Column Selectivity.......................................................................................................................... 38
Result Sets...................................................................................................................................................................................... 39
Limiting the Number of Rows....................................................................................................................................................... 39
Large Result Sets and External Sorting......................................................................................................................................... 39
Large Objects................................................................................................................................................................................... 39
Storing and Reading Large Objects.............................................................................................................................................. 39
Linked Tables................................................................................................................................................................................... 39
Transaction Isolation........................................................................................................................................................................ 39
Table Level Locking.................................................................................................................................................................... 39
Lock Timeout............................................................................................................................................................................. 40
Clustering / High Availability.............................................................................................................................................................. 40
Using the CreateCluster Tool....................................................................................................................................................... 40
Two Phase Commit........................................................................................................................................................................... 41
Compatibility.................................................................................................................................................................................... 41
3 of 129
4. Transaction Commit when Autocommit is On................................................................................................................................ 41
Keywords / Reserved Words........................................................................................................................................................ 41
ODBC Driver.................................................................................................................................................................................... 41
ODBC Installation....................................................................................................................................................................... 41
Log Option................................................................................................................................................................................. 42
Security Considerations............................................................................................................................................................... 42
Uninstalling................................................................................................................................................................................ 42
ACID............................................................................................................................................................................................... 42
Atomicity................................................................................................................................................................................... 42
Consistency................................................................................................................................................................................ 42
Isolation.................................................................................................................................................................................... 42
Durability................................................................................................................................................................................... 43
Running the Durability Test......................................................................................................................................................... 43
Using the Recover Tool..................................................................................................................................................................... 43
File Locking Protocols....................................................................................................................................................................... 43
File Locking Method 'File'............................................................................................................................................................. 44
File Locking Method 'Socket'........................................................................................................................................................ 44
Protection against SQL Injection........................................................................................................................................................ 44
What is SQL Injection................................................................................................................................................................. 44
Disabling Literals........................................................................................................................................................................ 45
Using Constants......................................................................................................................................................................... 45
Using the ZERO() Function.......................................................................................................................................................... 45
Security Protocols............................................................................................................................................................................. 45
User Password Encryption........................................................................................................................................................... 45
File Encryption............................................................................................................................................................................ 46
SSL/TLS Connections.................................................................................................................................................................. 46
HTTPS Connections.................................................................................................................................................................... 46
Universally Unique Identifiers (UUID)................................................................................................................................................. 46
Glossary and Links............................................................................................................................................................................ 47
SQL Grammar........................................................................................................................................................................................ 48
Commands (Data Manipulation)......................................................................................................................................................... 48
Commands (Data Definition)............................................................................................................................................................. 48
Commands (Other)........................................................................................................................................................................... 49
Other Grammar................................................................................................................................................................................ 49
SELECT...................................................................................................................................................................................... 50
INSERT...................................................................................................................................................................................... 50
UPDATE..................................................................................................................................................................................... 50
DELETE..................................................................................................................................................................................... 50
CALL......................................................................................................................................................................................... 51
EXPLAIN.................................................................................................................................................................................... 51
MERGE...................................................................................................................................................................................... 51
RUNSCRIPT................................................................................................................................................................................ 51
SCRIPT...................................................................................................................................................................................... 52
ALTER INDEX RENAME................................................................................................................................................................ 52
ALTER SEQUENCE...................................................................................................................................................................... 52
ALTER TABLE ADD...................................................................................................................................................................... 52
ALTER TABLE ADD CONSTRAINT................................................................................................................................................. 52
ALTER TABLE ALTER COLUMN..................................................................................................................................................... 53
ALTER TABLE ALTER COLUMN RENAME........................................................................................................................................ 53
ALTER TABLE ALTER COLUMN RESTART...................................................................................................................................... 53
ALTER TABLE ALTER COLUMN SELECTIVITY................................................................................................................................. 53
ALTER TABLE ALTER COLUMN SET DEFAULT................................................................................................................................ 53
ALTER TABLE ALTER COLUMN SET NOT NULL.............................................................................................................................. 54
ALTER TABLE ALTER COLUMN SET NULL...................................................................................................................................... 54
ALTER TABLE DROP COLUMN...................................................................................................................................................... 54
ALTER TABLE DROP CONSTRAINT............................................................................................................................................... 54
ALTER TABLE RENAME................................................................................................................................................................ 54
ALTER USER ADMIN................................................................................................................................................................... 54
ALTER USER RENAME................................................................................................................................................................. 55
ALTER USER SET PASSWORD...................................................................................................................................................... 55
ALTER VIEW.............................................................................................................................................................................. 55
ANALYZE................................................................................................................................................................................... 55
CREATE ALIAS............................................................................................................................................................................ 55
CREATE DOMAIN........................................................................................................................................................................ 56
CREATE INDEX........................................................................................................................................................................... 56
CREATE LINKED TABLE............................................................................................................................................................... 56
CREATE ROLE............................................................................................................................................................................ 56
CREATE SCHEMA........................................................................................................................................................................ 56
CREATE SEQUENCE.................................................................................................................................................................... 57
CREATE TABLE........................................................................................................................................................................... 57
CREATE TRIGGER....................................................................................................................................................................... 57
CREATE USER............................................................................................................................................................................ 57
CREATE VIEW............................................................................................................................................................................ 58
DROP ALIAS............................................................................................................................................................................... 58
DROP ALL OBJECTS.................................................................................................................................................................... 58
DROP INDEX.............................................................................................................................................................................. 58
DROP ROLE............................................................................................................................................................................... 58
DROP SEQUENCE....................................................................................................................................................................... 58
4 of 129
5. DROP SCHEMA........................................................................................................................................................................... 59
DROP TABLE.............................................................................................................................................................................. 59
DROP TRIGGER.......................................................................................................................................................................... 59
DROP USER............................................................................................................................................................................... 59
DROP VIEW............................................................................................................................................................................... 59
TRUNCATE TABLE...................................................................................................................................................................... 59
COMMIT.................................................................................................................................................................................... 60
COMMIT TRANSACTION.............................................................................................................................................................. 60
CHECKPOINT............................................................................................................................................................................. 60
CHECKPOINT SYNC.................................................................................................................................................................... 60
GRANT RIGHT............................................................................................................................................................................ 60
GRANT ROLE.............................................................................................................................................................................. 60
HELP......................................................................................................................................................................................... 61
PREPARE COMMIT...................................................................................................................................................................... 61
REVOKE RIGHT.......................................................................................................................................................................... 61
REVOKE ROLE............................................................................................................................................................................ 61
ROLLBACK................................................................................................................................................................................. 61
ROLLBACK TRANSACTION........................................................................................................................................................... 61
SAVEPOINT................................................................................................................................................................................ 62
SET ALLOW_LITERALS................................................................................................................................................................ 62
SET ASSERT............................................................................................................................................................................... 62
SET AUTOCOMMIT..................................................................................................................................................................... 62
SET CACHE_SIZE........................................................................................................................................................................ 62
SET CLUSTER............................................................................................................................................................................. 63
SET COLLATION......................................................................................................................................................................... 63
SET COMPRESS_LOB.................................................................................................................................................................. 63
SET DATABASE_EVENT_LISTENER............................................................................................................................................... 63
SET DB_CLOSE_DELAY............................................................................................................................................................... 64
SET DEFAULT_LOCK_TIMEOUT................................................................................................................................................... 64
SET DEFAULT_TABLE_TYPE........................................................................................................................................................ 64
SET IGNORECASE....................................................................................................................................................................... 64
SET LOCK_MODE........................................................................................................................................................................ 65
SET LOCK_TIMEOUT................................................................................................................................................................... 65
SET LOG.................................................................................................................................................................................... 65
SET MAX_LENGTH_INPLACE_LOB................................................................................................................................................ 65
SET MAX_LOG_SIZE................................................................................................................................................................... 66
SET MAX_MEMORY_ROWS.......................................................................................................................................................... 66
SET MAX_MEMORY_UNDO.......................................................................................................................................................... 66
SET MODE................................................................................................................................................................................. 66
SET MULTI_THREADED............................................................................................................................................................... 67
SET PASSWORD......................................................................................................................................................................... 67
SET SALT HASH.......................................................................................................................................................................... 67
SET THROTTLE.......................................................................................................................................................................... 67
SET TRACE_LEVEL...................................................................................................................................................................... 67
SET TRACE_MAX_FILE_SIZE........................................................................................................................................................ 68
SET WRITE_DELAY..................................................................................................................................................................... 68
SHUTDOWN............................................................................................................................................................................... 68
Comment................................................................................................................................................................................... 68
Select Part................................................................................................................................................................................. 68
From Part.................................................................................................................................................................................. 69
Constraint.................................................................................................................................................................................. 69
Referential Constraint................................................................................................................................................................. 69
Table Expression........................................................................................................................................................................ 69
Order........................................................................................................................................................................................ 69
Expression................................................................................................................................................................................. 70
And Condition............................................................................................................................................................................ 70
Condition................................................................................................................................................................................... 70
Condition Right Hand Side........................................................................................................................................................... 70
Compare.................................................................................................................................................................................... 70
Operand.................................................................................................................................................................................... 70
Summand.................................................................................................................................................................................. 71
Factor........................................................................................................................................................................................ 71
Term......................................................................................................................................................................................... 71
Value......................................................................................................................................................................................... 71
Case.......................................................................................................................................................................................... 71
Case When................................................................................................................................................................................ 72
Cipher....................................................................................................................................................................................... 72
Select Expression........................................................................................................................................................................ 72
Data Type.................................................................................................................................................................................. 72
Name........................................................................................................................................................................................ 72
Alias.......................................................................................................................................................................................... 72
Quoted Name............................................................................................................................................................................. 73
String........................................................................................................................................................................................ 73
Int............................................................................................................................................................................................. 73
Long.......................................................................................................................................................................................... 73
Hex Number............................................................................................................................................................................... 73
Decimal..................................................................................................................................................................................... 73
Double....................................................................................................................................................................................... 74
5 of 129