If a is already unique, there is no need for b in the primary key... -----Ursprüngliche Nachricht----- Von: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] Im Auftrag von Simon Slavin Gesendet: Dienstag, 28. Februar 2017 01:41 An: SQLite mailing list <sqlite-users@mailinglists.sqlite.org> Betreff: Re: [sqlite] foreign key cardinality
On 28 Feb 2017, at 12:19am, James K. Lowden <jklow...@schemamania.org> wrote: > sqlite> create table A(a, b, primary key (a,b)); create table C(c > sqlite> references A(a)); The reference column(s) (the column(s) in the 'parent' table) must be UNIQUE otherwise you may have two rows in that table which both look like they may be the parent. So you need a UNIQUE index on the reference column(s). Of course, if they’re the primary key then they already have a UNIQUE index, since the primary key of a table must be unique and SQLite automatically makes an index to enforce that. So try instead create table A(a, b, primary key (a,b)); CREATE UNIQUE INDEX A_a ON A (a); create table C(c references A(a)); A way to have this index automatically created is to put a UNIQUE requirement on that column: create table A(a, b, primary key (a UNIQUE,b)); create table C(c references A(a)); This is documented in section 3 of <https://www.sqlite.org/foreignkeys.html> Simon. _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___________________________________________ Gunter Hick Software Engineer Scientific Games International GmbH FN 157284 a, HG Wien Klitschgasse 2-4, A-1130 Vienna, Austria Tel: +43 1 80100 0 E-Mail: h...@scigames.at This communication (including any attachments) is intended for the use of the intended recipient(s) only and may contain information that is confidential, privileged or legally protected. Any unauthorized use or dissemination of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender by return e-mail message and delete all copies of the original communication. Thank you for your cooperation. _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users