SQLite composite key (2 foreign keys) Link table

前端 未结 4 1125
再見小時候
再見小時候 2020-12-28 18:45

I\'ve read the rather cool styled BNF grammar for the SQLite create table statement

found here: http://www.sqlite.org/lang_createtable.html

I was wo

4条回答
  •  执念已碎
    2020-12-28 19:04

    Either of these should work for your association table:

    create table house_items (
        house_id integer not null,
        item_id  integer not null,
        foreign key (house_id) references houses(id),
        foreign key (item_id) references electrical_items(id),
        primary key (house_id, item_id)
    )
    
    create table house_items (
        house_id integer not null references houses(id),
        item_id  integer not null references electrical_items(id),
        primary key (house_id, item_id)
    )
    

    You'll probably want separate (single column) indexes on house_items.house_id and house_items.item_id as well.

提交回复
热议问题