我有2代表与多对多关系:到许多许多表 - 1个单独字段主键或2存在的Fileds主键(实施例内)
student
(
id int(11) NOT NULL,
name varchar(255),
primary key(id)
);
teacher
(
id int(11) NOT NULL,
name varchar(255),
primary key(id)
);
和我应该做3表 - student_has_teacher
选项添加ID单独字段主键
student_has_teacher ( id int(11)NOT NULL, teacher_id int(11), student_id int(11) primary key(id) );
选项使2场主键
student_has_teacher ( teacher_id INT(11), student_id数据INT(11), 主键(teacher_id,student_id数据), 外键(teacher_id)引用老师(id), 外键(student_id)引用学生(id) );
什么是更好的选择,为什么?
谢谢
id是id字段的最差名称。请学习使用studentid,teacherid等。当您遇到复杂的查询和报告时,它会产生巨大的差异。 – HLGEM 2010-08-17 15:20:09
重复http://stackoverflow.com/questions/963809/should-i-use-composite-primary-keys-or-not,http://stackoverflow.com/questions/159087/composite-primary-keys-versus -unique-object-id-field,http://stackoverflow.com/questions/1383062/composite-primary-key-to-use-or-not-to-use? – 2010-08-17 17:01:58