Foreign Key Android DATABASE - android

I'm confused about the correct usage and implementation of the "Foreign KEY" in the SQLite Database in Android.
I created a DB with several relations, as following:
CREATE TABLE "food" (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` TEXT NOT NULL,
`description` TEXT,
`category_id` INTEGER,
FOREIGN KEY(`category_id`) REFERENCES food(_id) )
CREATE TABLE `category` (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`cagory_name` TEXT NOT NULL)
CREATE TABLE "favourites" (
`_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`id_favourite` INTEGER NOT NULL,
FOREIGN KEY(`id_favourite`) REFERENCES food ( _id ))
As you can see, there are 3 tables and the _id of "food" is the foreign key connecting the table "category" and "favourites".
Now, in Android I don't see happening ANYTHING that proves that I created such a relation among tables. Do I miss some declaration to make it effective?
How does it work in Android?

The category_id foreign key reference seems odd.
Is there a reason it's:
FOREIGN KEY(`category_id`) REFERENCES food(_id)
and not
FOREIGN KEY(`category_id`) REFERENCES category(_id)
To make sure foreign key constraints are enforced, in SQLite run
PRAGMA foreign_keys = ON;
See http://www.sqlite.org/foreignkeys.html for documentation.

Related

what is missing in my sqlite syntax for foreign key?

I created two sqlite tables on android
phone table with primary key "id"
CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT UNIQUE,KEY_IS_BLOCKED BIT )
comment table with foreign key "id"
CREATE TABLE COMMENTS_TABLE ( id INTEGER, KEY_COMMENT_TEXT TEXT, FOREIGN KEY(id) REFERENCES BLOCKED_PHONES_TABLE(id))
why does the comment table don't refer id as a foreign key?
otherwise it won't have ids that are missing in the phone table.
how can I know my sqlite version?
For backwards compatibility, foreign key checking is disabled by default.
You need to call setForeignKeyConstraintsEnabled in onConfigure.

Foreign key mismatch

This is my SQLite table structure
CREATE TABLE tbl_district (
districtId INTEGER PRIMARY KEY not null,
districtName TEXT unique NOT NULL
);
CREATE TABLE tbl_route (
routeId INTEGER,
districtId INTEGER NOT NULL REFERENCES tbl_district (districtId) ON DELETE CASCADE ON UPDATE CASCADE,
routeName TEXT NOT NULL,
primary key (routeId , districtId)
);
CREATE TABLE tbl_city (
cityId INTEGER PRIMARY KEY,
routeId INTEGER NOT NULL REFERENCES tbl_route (routeId) ON DELETE CASCADE ON UPDATE CASCADE,
cityName TEXT NOT NULL
);
If i execute
insert into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It works as it should
But if i execute
replace into tbl_district ( districtId, districtName) values (1,'Sri Lanka')
It gives following error
Error while executing query: foreign key mismatch - "tbl_city" referencing "tbl_route"
Any idea to resolve this?
Thanks in advance!
The documentation says that
the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.
The routeId column is the parent key of the foreign key in tbl_city, but has no UNIQUE constraint.
(Your REPLACE statement is not directly related to this; it's just when the database happend to notice the error.)

syntax error while using CASCADE in sqlite

when i am trying to run following query in sqlite in android i am getting error
android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1):
follwing is the table creating query , i am not be able to figure out where things are going wrong
CREATE TABLE IF NOT EXISTS template_info
( _id integer autoincrement ,
temp_contact_key integer ,
temp_text text ,
FOREIGN KEY ( temp_contact_key ) REFERENCES contact_info ( _id )
ON UPDATE CASCADE ON DELETE CASCADE ,
PRIMARY KEY ( _id , temp_contact_key )
);
SQL syntax problems:
If you want to use autoincrement, it should be primary key. Either make the _id integer primary key autoincrement and remove the last line composite PRIMARY KEY (and the , comma before it), or remove the autoincrement keyword. If you need to enforce that (_id,temp_contact_key) pairs are unique, add UNIQUE(_id,temp_contact_key) constraint.
Remove the , before ON UPDATE CASCADE ON DELETE CASCADE.

Using different IDs for multiple table in SQLITE

I am using SQLite to work on an android application. What I have learned so far is that a table made in SQLite requires a column called _id. So, for instance, to create a table, I use the following SQL Statement:
CREATE TABLE IF NOT EXISTS Semesters(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_name TEXT,
is_current TEXT
GPA REAL);
However, now if I want to create a seperate table in the same database, what would I name the primary key column? For instance, this is the table I want to create:
CREATE TABLE IF NOT EXISTS Classes(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
course_name TEXT);
Now, both the semesters table and the courses table have a column called _id, and if I want to make a foreign key reference to the _id column in the semesters table, I will have to call it by the table name. Is there any way to make this simpler by using different names for ids?
Thanks.
For example, you make a third table and want to use both primary keys from your tables as foreign keys:
CREATE TABLE ClassSemester(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
semester_id INTEGER,
class_id INTEGER,
FOREIGN KEY(semester_id) REFERENCES Semesters(_id),
FOREIGN KEY(class_id) REFERENCES Classes(_id)
);
More details in here:
http://www.sqlite.org/foreignkeys.html

syntax error with FOREIGN KEY in CREATE TABLE

I am getting following output on debugger. I am not sure what syntax is missing.
The SQL code is:
CREATE TABLE weeks(Week_Id INTEGER PRIMARY KEY,
Day TEXT,
Start_Time Text,
End_Time Text,
Break_Time Text );
CREATE TABLE projects(Project_Id INTEGER PRIMARY KEY,
Name TEXT,
Description Text,
Client_Name Text,
Location Text );
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER,
FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
Week_Id INTEGER,
FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id));
The error boils down to:
12-09 12:34:20.782: E/SQLiteLog(6490): (1) near "Week_Id": syntax error
Try moving your FOREIGN KEY lists to after your variables are created.
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER,
Week_Id INTEGER,
FOREIGN KEY (Project_Id) REFERENCES projects (Project_Id),
FOREIGN KEY (Week_Id) REFERENCES weeks (Week_Id));
According to the SQLite syntax (http://www.sqlite.org/lang_createtable.html) you can also write something like this:
CREATE TABLE timesheets(Timesheet_Id INTEGER PRIMARY KEY,
Project_Id INTEGER REFERENCES projects (Project_Id),
Week_Id INTEGER REFERENCES weeks (Week_Id));
This merges declarations and foreign keys.

Categories

Resources