syntax error with FOREIGN KEY in CREATE TABLE - android

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.

Related

Foreign Key Android DATABASE

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.

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.

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

Mycode
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookdir TEXT , lastaddress TEXT,addresname TEXT PRIMARY KEY(bookdir,lastaddress));";
db.execSQL(createtabBook);
Logcat:
03-05 18:29:31.708: I/System.out(17160): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));
i just want to create a table with composite primary key so that it is a combination of bookdir and lastaddress, also i want to make the lid as auto increment.
To get the value of any column autoincrement you need to write it or declare it as primary key.
In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.
EDITED:
As you can not define more than one PRIMARY KEY in a table you have to make the bookdir,lastaddress columns as UNIQUE and define the lid columns as PRIMARY KEY as below:
Try out as below:
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER PRIMARY KEY AUTOINCREMENT,
bookdir TEXT , lastaddress TEXT,addresname TEXT, UNIQUE(bookdir,lastaddress));";
Also add "," after the column addresname TEXT, in your query.

Multi Primary Keys in Android SQLite

In SQLite for Android what is the correct way to add multi primary keys?
Currently I have:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
The alternative method I thought of was:
String Create_table = "CREATE TABLE project (keyID INTEGER, keyName TEXT, PRIMARY KEY(keyID, keyName))";
Are both valid? If so, which is better? Also how do I disallow NULL values?
No it's not possible to create multiple primary keys for a single table. It's basic rule of any SQL. However you can use other constraint like UNIQUE with index to achieve this.
This won't be valid SQL Syntax:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
In other way you can create primary key for multiple column as follows:
Create Table yourTableName (col1, col2, col3, PRIMARY KEY (col1, col2));
How do I disallow NULL values?
you can use NOT NULL Constraint , it will not allow you to enter NULL values.
You may use one primary key , more foreign keys in one table

Sqlite Query Error

I am using sqlite db in android.
In this I need to give Id as autoincrement and second 'Incoming_sms' field as primary key. but it shows me error as below:
detailMessage "near "AUTOINCREMENT": syntax error:
CREATE TABLE TwoWayTable111 (
ID INTEGER AUTOINCREMENT,
INCONMING_MSG TEXT PRIMARY KEY,
OUTGOING_MSG TEXT,
STATUS TEXT )"
Why does this error occur? But when I give id as autoincrement and primary key, it works fine.
This is a FAQ. It works as designed. This statement returns a syntax error.
create table test (id integer autoincrement);
This one runs without error.
create table test (id integer primary key autoincrement);
You should be able to do this.
CREATE TABLE TwoWayTable111 (
ID INTEGER PRIMARY KEY,
INCONMING_MSG TEXT NOT NULL UNIQUE,
OUTGOING_MSG TEXT,
STATUS TEXT );

Categories

Resources