Igor Tandetnik <itandet...@...> writes:

> 
> Oliver Peters <oliver....@...> wrote:
> > This sql code works in the two cases but AFAIK  it shouldn't in the second
> > 
> > PRAGMA foreign_keys = ON;
> > 
> > CREATE TABLE staff_02(
> > id                                INTEGER     PRIMARY KEY AUTOINCREMENT,
> > id_staff_editor                   INTEGER     NOT NULL,
> > code                              CHAR(2)     NOT NULL,
> > UNIQUE(code)
> > FOREIGN KEY(id_staff_editor)      REFERENCES staff_02(id)
> > );
> > 
> > INSERT INTO staff_02(id_staff_editor,code) VALUES(1,'CB');
> 
> The first record gets inserted with an id of 1, which just happens to match
the value of id_staff_editor. See
> what this would do:
> 
> INSERT INTO staff_02(id_staff_editor,code) VALUES(1000,'CB');
> 

So I think I misunderstood the concept of Foreign Keys - I thought at first it
is checked if the PK exists and if it is not existing it is rejected to
INSERT/UPDATE the FK

Could you please explain why it is o.k that this works?


PRAGMA foreign_keys = ON;

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);
INSERT INTO a(id_staff_editor) VALUES(2);
INSERT INTO a(id_staff_editor) VALUES(3);


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

Reply via email to