數據庫表中一對多關系怎么設計?


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);

INSERT INTO teacher (id,name) VALUES(1,'qixy');
INSERT INTO teacher (id,name) VALUES(2,'huanglaosh');

select * from pupil,teacher where tid=teacher.id and teacher.name='qixy';

+----+-----------+------+----+------+
| id | name      | tid  | id | name |
+----+-----------+------+----+------+
|  1 | liyaohua  |    1 |  1 | qixy |
|  2 | fuwenlong |    1 |  1 | qixy |
+----+-----------+------+----+------+
 

更多內容請見原文,文章轉載自:https://blog.csdn.net/qq_44591615/article/details/109205809


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM