Using foreign key constraints

If you create foreign keys for your database, you can enforce the use of them by setting the foreign_key_constraints option in the DatabaseOptions class.

You can set this option when you create the database. For the foreign key constraints to be in effect, you must set the option each time you open the database.

You can check whether foreign key constraints are on with the DatabaseOptions.foreignKeyConstraintsOn method.

Code sample

The following code sample shows how to enforce foreign key constraints before opening or creating a database. The code sample creates the DatabaseOptions object with foreign_key_constraints set to on.

DatabaseOptions dbo = new DatabaseOptions();
dbo.set("foreign_key_constraints","on");
Database d = DatabaseFactory.openOrCreate("test.db", dbo);

Was this information helpful? Send us your comments.