亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在設計表格時實現一對一、一對多和多對多的關系?

如何在設計表格時實現一對一、一對多和多對多的關系?

幕布斯7119047 2019-07-05 18:38:00
如何在設計表格時實現一對一、一對多和多對多的關系?有人能解釋如何在設計表格時實現一對一、一對多和多對多的關系嗎?
查看完整描述

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


查看完整回答
反對 回復 2019-07-05
  • 3 回答
  • 0 關注
  • 1794 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號