Am 09.08.2010 16:57, schrieb Oliver Peters:
> [...]
>
> To my mind the simplified question is:
>
> why returns the "INSERT INTO a" not an error while the "INSERT INTO b" does? 
> How
> corresponds this behaviour to the concept of FOREIGN KEYS?
>
> I wrote the code into file.sql (encoding=utf8, if this matters) and started
>       sqlite3 -bail test.db3<  file.sql
>
>
> PRAGMA foreign_keys = ON;
>
> /*
> **success INSERT = yes
> */
>
> CREATE TABLE a(
>                  id                                INTEGER     PRIMARY KEY
> AUTOINCREMENT,
>                  id_staff_editor                   INTEGER     NOT NULL,
>                  FOREIGN KEY(id_staff_editor)      REFERENCES a(id)
> );
>
> INSERT INTO a(id_staff_editor) VALUES(1);
This insert succeeds, because the relusting record has the value 1 in 
it's field id. Id is autoincrement, therefore the first record will habe 
id == 1, which satisfies your foreign key, because you insert 1 in the 
column id_staff_editor as well.
> /*
> **success INSERT = no
> */
>
> CREATE TABLE b(
>                  id                                INTEGER     PRIMARY KEY
> AUTOINCREMENT,
>                  id_staff_editor                   INTEGER     NOT NULL,
>                  FOREIGN KEY(id_staff_editor)      REFERENCES b(id)
> );
>
> INSERT INTO b(id_staff_editor) VALUES(2);
>
This insert FAILS, because the relusting record has the value 1 in it's 
field id. Id is autoincrement, therefore the first record will habe id 
== 1, which DOES NOT satisfiy your foreign key, because you insert *2* 
in the column id_staff_editor .

> Oliver
>
>
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to