• <tfoot id='EiauD'></tfoot>

      • <bdo id='EiauD'></bdo><ul id='EiauD'></ul>

        <legend id='EiauD'><style id='EiauD'><dir id='EiauD'><q id='EiauD'></q></dir></style></legend>

        <small id='EiauD'></small><noframes id='EiauD'>

        <i id='EiauD'><tr id='EiauD'><dt id='EiauD'><q id='EiauD'><span id='EiauD'><b id='EiauD'><form id='EiauD'><ins id='EiauD'></ins><ul id='EiauD'></ul><sub id='EiauD'></sub></form><legend id='EiauD'></legend><bdo id='EiauD'><pre id='EiauD'><center id='EiauD'></center></pre></bdo></b><th id='EiauD'></th></span></q></dt></tr></i><div id='EiauD'><tfoot id='EiauD'></tfoot><dl id='EiauD'><fieldset id='EiauD'></fieldset></dl></div>

      1. 设计表格时如何实现一对一、一对多和多对多的关系?

        时间:2023-07-18

            <tbody id='d0ouH'></tbody>

            • <i id='d0ouH'><tr id='d0ouH'><dt id='d0ouH'><q id='d0ouH'><span id='d0ouH'><b id='d0ouH'><form id='d0ouH'><ins id='d0ouH'></ins><ul id='d0ouH'></ul><sub id='d0ouH'></sub></form><legend id='d0ouH'></legend><bdo id='d0ouH'><pre id='d0ouH'><center id='d0ouH'></center></pre></bdo></b><th id='d0ouH'></th></span></q></dt></tr></i><div id='d0ouH'><tfoot id='d0ouH'></tfoot><dl id='d0ouH'><fieldset id='d0ouH'></fieldset></dl></div>
              <legend id='d0ouH'><style id='d0ouH'><dir id='d0ouH'><q id='d0ouH'></q></dir></style></legend>

                <small id='d0ouH'></small><noframes id='d0ouH'>

                  <bdo id='d0ouH'></bdo><ul id='d0ouH'></ul>

                  <tfoot id='d0ouH'></tfoot>
                  本文介绍了设计表格时如何实现一对一、一对多和多对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能用一些例子解释一下如何在设计表格时实现一对一、一对多和多对多的关系?

                  Can anyone explain how to implement one-to-one, one-to-many and many-to-many relationships while designing tables with some examples?

                  推荐答案

                  一对一: 使用引用表的外键:

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

                  You must also put a unique constraint on the foreign key column (addess.student_id) to prevent multiple rows in the child table (address) from relating to the same row in the referenced table (student).

                  一对多:在链接回一"方的关系的多方使用外键:

                  One-to-many: Use a foreign key on the many side of the relationship linking back to the "one" side:

                  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
                  

                  这篇关于设计表格时如何实现一对一、一对多和多对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ANSI JOIN 与非 ANSI JOIN 查询的性能会不同吗? 下一篇:如何在 Oracle 中重置序列?

                  相关文章

                • <tfoot id='nmcEO'></tfoot><legend id='nmcEO'><style id='nmcEO'><dir id='nmcEO'><q id='nmcEO'></q></dir></style></legend>

                    • <bdo id='nmcEO'></bdo><ul id='nmcEO'></ul>
                  1. <small id='nmcEO'></small><noframes id='nmcEO'>

                      <i id='nmcEO'><tr id='nmcEO'><dt id='nmcEO'><q id='nmcEO'><span id='nmcEO'><b id='nmcEO'><form id='nmcEO'><ins id='nmcEO'></ins><ul id='nmcEO'></ul><sub id='nmcEO'></sub></form><legend id='nmcEO'></legend><bdo id='nmcEO'><pre id='nmcEO'><center id='nmcEO'></center></pre></bdo></b><th id='nmcEO'></th></span></q></dt></tr></i><div id='nmcEO'><tfoot id='nmcEO'></tfoot><dl id='nmcEO'><fieldset id='nmcEO'></fieldset></dl></div>