views:

140

answers:

3

I have two tables 'Elements' and 'Lists' Lists has a primary key and a list name. Elements has data pertaining to an individual entry in the list.

Elements needs a column that holds which list the element is in.

I've read about SQL's foreign key constraint and figure that is the best way to link the tables, but I'm using SQLite which doesn't enforce the foerign key constraint.

Is there a point to declaring the foreign key constraint if there is no enforcement?

+1  A: 

Nowadays sqlite enforces foreign keys, download the new release.

tuinstoel
What's the latest version? I have to use sqlite3, does that support foreign keys?
CodeFusionMobile
Read here, sqlite 3.6.19 enforces foreign keys without the use of triggers: http://www.sqlite.org/foreignkeys.html
tuinstoel
A: 

A foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.

It only enforces the "business rule". If you require this from the business side, then yes, it is required.

Indexing will not be affected.

You can still create indexes as requred.

Have a look at Foreign Key

and

Wikipedia Foreign key

astander
+2  A: 

It's always good to do, even if your database doesn't enforce the constraint (old MySQL, for instance). The reasoning for this, is that someday, someone will try reading your schema (perhaps even yourself).

If you can't use the new version, you can still declare the constraint and enforce it with triggers. In either case, I wouldn't omit the notation. It's far too helpful.

Pestilence