On delete cascade android 2.2 - android

I have 2 tables:
Table 1- create table table1(_id integer primary key, content text not null );
Table 2- create table table2(_id integer primary key, id_parent integer, content text not null , CONSTRAINT [FK_table2_id_parent] FOREIGN KEY (id_parent) REFERENCES table1 (_id) ON DELETE CASCADE );
But the DELETE CASCADE doesn't do anything... Anyone know what I'm doing wrong?
[EDIT:
When I try to delete rows from table1 I can, but when I look for table2 the records are all there ... even those who have the id_parent that no longer exists from Table1.]
I'm using android 2.2.

First make sure your used create table commands are syntactically and logically right.
If yes then this is due to following:
Declaring and defining foreign key does not apply them really to act.
You need to explicitly on the foreign key constraint in your database.
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}

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.

Change column name in sqlite when other tables have foreign keys to it

I have a table named groups, and I want to rename one of its columns. Is was ok, so far. I know sqlite doesn't support renaming columns, so I did:
ALTER TABLE groups RENAME to tmp_groups;
CREATE TABLE groups(
_ID integer primary key autoincrement,
new_column_name integer
);
INSERT INTO groups(_ID, new_column_name) SELECT _ID, old_column_name FROM tmp_groups;
DROP TABLE tmp_groups;
But, when I drop the table tmp_groups, the table members, that had a foreign key with ON DELETE CASCADE has its records deleted as well, so I had to do the following:
Create a table tmp_members with the same columns as members, and without the foreign key;
Insert the records from members in tmp_members;
Drop the table members;
Run the code from the first part (with the groups table);
Re-create the table members with its foreign key;
Insert in members data from tmp_members;
Man, that was tiring! Its too much code to simply rename a column;
Is there any simpler way to handle this constraint problem, or is this the "sqlite way"?
For historical reasons, SQLite allows to disable foreign key constraints (and this is even the default currently).
Just run PRAGMA foreign_keys = off before doing the groups table stuff.
It would also be possible to rename a column by using PRAGMA writable_schema, but you should do this only if you know what you're doing.

android sqlite deleting record being lined as foreign key with another record

I have the following tables in my sqlite android database:
CREATE TABLE MAIN (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
NAME TEXT,
.....
CURRENT_CARD INTEGER
FOREIGN KEY (CURRENT_CARD) REFERENCES CARD (_ID));
CREATE TABLE CARD (
_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT,
.....
Now I want to delete records from my CARD table in the following way:
DELETE FROM CARD WHERE _ID IN (10,11,12,...);
Everything works ok until I have the record in Card table being linked to Main table, ex. let's admit that Card with id 11 has the corresponding record in Main table. When deleting Card 11 I would like to set the corresponding CURRENT_CARD field for record ex. 1 in MAIN as null (I don't want to delete the whole record in MAIN, just to null one column).
How to do that in the easiest way? If possible as one sql statement...
Use ON DELETE SET NULL action with the FOREIGN KEY.
Remember to enable foreign key support with pragma foreign_keys='ON'.

How to stop parent delete if child present

I want to use constraints in SQLite.
Scenario: Stop parent delete if child is present.
How can I achieve that?
Corrected: SQLite supports foreign key constraints, but they're off by default. You have to enable them at run time with this statement. (That means you can't set this once, and expect it to stick. If you forget to set it just once, you can delete rows from the parent and leave orphans in the child table.)
sqlite> PRAGMA foreign_keys = ON;
Details are at Enabling Foreign Key Support
SQLite can be compiled with foreign key support entirely disabled. That's something to keep in mind.
Lets say you have table A (ID, NAME) and table B (ID, A_ID, NICK_NAME)
When you create table A you'd do
create table A (
ID INTEGER PRIMARY KEY,
NAME TEXT NOT NULL UNIQUE
);
create table B (
ID INTEGER PRIMARY KEY,
A_ID INTEGER NOT NULL,
NICK_NAME TEXT,
CONSTRAINT A_FK FOREIGN KEY (A_ID) REFERENCES A (ID) ON DELETE RESTRICT
);
That should cause an error if you try to delete content from table A that is referenced by column B.
EDIT: as noted in the original poster's comment and my own following it, this apparently does not work. Anyone else shed light on this?

Problem with inserting into android sqlite3 table that has composite primary key

I have a table with a composite primary key and I am having trouble inserting. The code used to create the table is:
CREATE TABLE ClassEvent (
EventName varchar(10) NOT NULL,
CourseId varchar(10) NOT NULL,
EventType varchar(20),
EventWeight number(3),
DueDate DATE NOT NULL,
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId),
PRIMARY KEY (CourseId, EventName));
The problem I am having is when I want to insert records that have values that may not be unique for the columns CourseId or EventName, but are a unique combination of the 2.
for example, if I try to run the following 2 inserts:
INSERT INTO ClassEvent VALUES('Assignment 1','60-415','Assignment',10,'12/10/2010');
INSERT INTO ClassEvent VALUES('Project 1','60-415','Project',15,'5/12/2010');
I get the following error:
Error: columns CourseId, EventName are not unique.
and the second insert does not make it into the DB. Why does this error out? I thought that a composite primary key requires that the combination of both values are unique. In my above inserts, the values for the EventName column are different even though the values for CourseId are the same. Shouldn't this be seen as 2 unique combinations and thus 2 different primary keys?
My table needs to be able to hold several different events for each CourseId, but each Event must be unique for each Course. I need to be able to insert values into the table like:
EventName CourseId
Assignment 1 60-415
Project 1 60-415
Assignment2 60-415
Project 2 60-415
Assignment 1 60-367
Project 1 60-367
and so on. Can anyone tell me how I can get this to work? Why are these composite PK's not being seen as unique entries? Any help would be much appreciated.
Here is the java function I am using for the insert:
public void addNewClassEvent(ContentValues values) {
SQLiteDatabase db = openConnection();
db.insert("ClassEvent", null, values);
db.close();
}
Could this be causing the problem?
You can have a composite primary key in SQLite, but you
have to create the key when you create the table:
CREATE TABLE example1(
field1 FLOAT,
field2 TEXT,
PRIMARY KEY(field1, field2)
);
You cannot create the primary key after the fact using ALTER TABLE.
On the other hand, you can create a UNIQUE INDEX after the fact
which has essentially the same effect as a PRIMARY KEY:
CREATE UNIQUE INDEX pk_index ON "table1"("field1","field2");
I am not sure how you have created, the tables, and if you have added the primary index later, but grab the database to your desktop, and check out how works in a desktop environment.
You can't make combinations like that, but you don't need them. What is stopping you from just having a truly id column ?

Categories

Resources