資料庫表中一對多關係怎麼設計?

馬克-to-win發表於2018-12-06

Database Design(資料庫設計)(視訊下載) (全部書籍)

馬克-to-win:

(一對多:one-to-many)

1) teacher and student.

(teacher表:兩列id(主鍵),name。
pupil表: 三列:id(主鍵),name,tid(外來鍵))

 

舉例: Teacher “qixy” has two students: liyaohua,fuwenlong. Teacher “huanglaosh” has two students: mashuai,jiaxiaohu.

create table pupil(id int not null,name char(10),tid int);

create table teacher(id int not null,name char(10));

INSERT INTO pupil (id,name,tid) VALUES(1,`liyaohua`,1);
INSERT INTO pupil (id,name,tid) VALUES(2,`fuwenlong`,1);
INSERT INTO pupil (id,name,tid) VALUES(3,`mashuai`,2);
INSERT INTO pupil (id,name,tid) VALUES(4,`jiaxiaohu`,2);

詳情請看:http://www.mark-to-win.com/index.html?content=Mydb/DBUrl.html&chapter=Mydb/DBIntroduction_web.html#DesignOneMany

相關文章