[英]MySQL - Relational tables - How to create multiple relationships?
我的數據庫中有兩個表。
Table 1 : book
book_id (Primary & Auto Increment)
book_name
writer_id (Foreign Key from writer table, selected as index)
Table 2 : writer
writer_id (Primary & Auto Increment)
writer_name
我只能在書中添加一位作家,但是一本書可以由兩個或更多作家來寫。 我該如何實現?
我以為可以創建兩個名為writer_2和writer_3的表(因為大多數書是由1、2或3位作家編寫的),並將它們作為外鍵添加到表中,但是我可以選擇其他解決方案。
我正在使用phpmyadmin,我的表存儲引擎是InnoDB。
您需要一個將作者分配到書籍的聯結表。 就像這樣:
create table BookWriter (
Book_Id int not null,
Writer_Id int not null,
foreign key (Book_id) references book(book_id),
foreign key (Writer_Id) references writer(writer_id)
);
請注意,此表中可能包含其他信息,例如別名:
create table BookWriter (
Book_Id int not null,
Writer_Id int not null,
Alias varchar(255),
foreign key (Book_id) references book(book_id),
foreign key (Writer_Id) references writer(writer_id)
);
例如,寫了一本名為《半殼維納斯》的書的“基爾古特·鱒魚”實際上就是科特·馮內古特。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.