如何在设计表格时实现一对一、一对多和多对多的关系?有人能解释如何在设计表格时实现一对一、一对多和多对多的关系吗?
3 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
一对一:
student: student_id, first_name, last_name, address_id address: address_id, address, city, zipcode, student_id # you can have a # "link back" if you need
addess.student_id
address
student
).
一对多
teachers: teacher_id, first_name, last_name # the "one" side classes: class_id, class_name, teacher_id # the "many" side
多对多
student: student_id, first_name, last_name classes: class_id, name, teacher_id student_classes: class_id, student_id # the junction table
-- Getting all students for a class: SELECT s.student_id, last_name FROM student_classes sc INNER JOIN students s ON s.student_id = sc.student_id WHERE sc.class_id = X -- Getting all classes for a student: SELECT c.class_id, name FROM student_classes sc INNER JOIN classes c ON c.class_id = sc.class_id WHERE sc.student_id = Y
添加回答
举报
0/150
提交
取消