Hab
Hab

Reputation:

Foreign key definition in sqlite

Is there any way to define relationship among tables and give foreign keys among them while using sqlite in Objective c

Upvotes: 2

Views: 2058

Answers (6)

Luc Wollants
Luc Wollants

Reputation: 890

You can easily create a foreign key by adding a FOREIGN KEY statement to the SQL CREATE command.

For example when having a person and address entity:

  • Create the person table: CREATE TABLE IF NOT EXISTS PERSON (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT)

  • Create the address table: CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY AUTOINCREMENT, STREETNAME TEXT, STREETNUMBER INT, PERSONID INT, FOREIGN KEY(PERSONID) REFERENCES PERSON(ID))

This will mark the PERSONID column of the ADDRESS table as a foreign key pointing to the ID column of the PERSON table.

You can also find a full tutorial at: http://www.apptite.be/tutorial_ios_sqlite.php

Upvotes: 1

Sunil Targe
Sunil Targe

Reputation: 7459

You can set one flag in SQLite for foreign key relationship.

Step 1: Go to tool menu in SQLiteManager.

Step 2: Open On-Connect SQL tab.

Step 3: Set "PRAGMA foreign_keys=ON;" and save it.

You can use database as normal PK and FK relationship.

Thanks.

Upvotes: 0

cms
cms

Reputation: 5982

If you use the Cocoa CoreData framework, and define a managed object model, using SQLite as the persistent store - you can specify the relations between your model, and specify deletion rules ( such as cascade or deny ) and these will be performed and validated as you make changes to your entities from Objective-C, and committed back to the database when you save.

The relationships and rules are very similar to database foreign key rules, but are performed by the CoreData framework inside the objective-C runtime. The SQLite database is just used as a persistence store for your managed object graph.

Here is the CoreData programming guide at the Apple Developer Site:

NB This framework is Cocoa-specific, and your question doesn't explicitly mention using Cocoa, just Objective-C

Upvotes: 0

sebasgo
sebasgo

Reputation: 3851

you can define foreign key relations in sqlite like in any other sql database system but to actually enforce them you need additional triggers. these can be compiled automatically from the database scheme with a tool shipped with the official sqlite distribution.

the big advantage of this solution is that it is programming language agnostic. once setup you don't have to care about the triggers in your source code anymore. see http://www.sqlite.org/omitted.html for more information.

Upvotes: 0

Clement Herreman
Clement Herreman

Reputation: 10536

SQLite isn't a "real" relationnal-database, you can have fields that link to other tables primary key, but you have to control all from your code. Same for deleting, no CASCADE or other integrity controls.

Upvotes: 1

codymanix
codymanix

Reputation: 29490

you can use foreign keys in sqlite the same way as in other sql-datebase systems but be aware that foreign key constraints in sqlite are not checked/enforced!

Upvotes: 2

Related Questions