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

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

      <tfoot id='gPNQF'></tfoot>

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

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

        许多表之一的外键?

        时间:2023-06-01

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

          <legend id='mnfEc'><style id='mnfEc'><dir id='mnfEc'><q id='mnfEc'></q></dir></style></legend>
            <tbody id='mnfEc'></tbody>

          • <small id='mnfEc'></small><noframes id='mnfEc'>

                  <bdo id='mnfEc'></bdo><ul id='mnfEc'></ul>
                  <tfoot id='mnfEc'></tfoot>
                • 本文介绍了许多表之一的外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  设置外键约束的常用方法是选择外键将指向哪个表.

                  The usual way of setting a foreign key constraint is to choose which table the foreign key will point to.

                  我在 1 个表和一组表之间存在多态关系.

                  I'm having a polymorphic relation between 1 table and a set of table.

                  这意味着该表将与集合中的其中一个表有关系.

                  That means that this table will have a relation with one of those tables in the set.

                  例如

                  images: person_id, person_type
                  subordinates: id, col1, col2...col9
                  products: id, colA, colB...colZ
                  

                  在上面的例子中,如果person_type是subordinates",那么person_id应该是subordinates.id的外键,产品也是如此.

                  In the above example, if person_type is "subordinates" then person_id should be a foreign key to subordinates.id and the same goes with products.

                  所以我想知道,是否可以有一个外键指向多个表中的一个,或者您是否必须在分配一个表时专门设置它指向哪个表.

                  So I wonder, is it possible to have a foreign key to one of many tables, or do you have to specifically set which table it points to when you assign one.

                  这个问题是针对 MySQL 和 PostgreSQL 的.

                  This question is for both MySQL and PostgreSQL.

                  谢谢

                  推荐答案

                  不,外键约束总是只引用一个父表.

                  No, a foreign key constraint always references exactly one parent table.

                  这个问题经常出现.以下是我过去的一些回答:

                  This question comes up frequently. Here are some of my past answers to it:

                  • 为什么多态关联中不能有外键吗?
                  • 可以为两个可能的表之一设置 MySQL 外键吗?
                  • 在同一列中引用外键
                  • 在 StackOverflow 克隆中,Comments 表应该与 Questions and Answers 有什么关系?
                  • MySQL - 条件外键约束
                  • 如何处理ERD(表格)设计中的OR"关系?
                  • MySQL:两个 n:1 关系,但不能同时使用
                  • Why can you not have a foreign key in a polymorphic association?
                  • Possible to do a MySQL foreign key to one of two possible tables?
                  • Referencing foreign keys in the same column
                  • In a StackOverflow clone, what relationship should a Comments table have to Questions and Answers?
                  • MySQL - Conditional Foreign Key Constraints
                  • How to handle an "OR" relationship in an ERD (table) design?
                  • MySQL: Two n:1 relations, but not both at once

                  有关多态关联的更多信息,请参阅我的演示文稿 Practical Object-SQL 中的面向模型 或我的书 SQL 反模式:避免数据库编程的陷阱.

                  For more on Polymorphic Associations, see my presentation Practical Object-Oriented Models in SQL or my book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming.

                  这篇关于许多表之一的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如果未找到单行,则返回默认值 下一篇:SQL SELECT 获取前 N 个正整数

                  相关文章

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

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