Can I use the same foreign key constraint in two different tables?

前端 未结 2 1270
挽巷
挽巷 2021-01-13 15:28

I am trying to create a database for work. I have two different types of users: internal and external. Each type has different properties so I just created two separate tabl

相关标签:
2条回答
  • 2021-01-13 16:00

    There are columns and there are foreign keys (FKs) and there are constraints.

    You can have a column name in a table regardless of other tables.

    A FK is a referencing table and column set and a referenced table and column set. All the names together identify a FK. It's a conceptual thing about a database.

    A FK constraint, being a constraint, is a thing whose name must be unique over the database. It has and enforces an associated FK, namely the one described in its declaration. You can have multiple FK constraints in a table enforcing the same FK.

    The DBMS has automatic unique names for FK constraints. Eg a name part plus a number part where the constraint is the numberth FK constraint of the table with that name. You can actually have the same nameless FK constraint definition text multiple times in a table and in multiple tables, each for a different FK constraint. (The ones inside a given table enforce the same FK.)

    You should have a unique naming scheme for when you want to name them. Referencing and referenced table names should be involved, and when necessary distinguishing column names.

    Confusingly, we say FK when we mean FK constraint.

    When you say "It's the same foreign key in two different tables," that misuses terms. There are two different FKs involved, and corresponding FK constraints. You mean maybe "it's the same referencing columns and referenced table and columns in both FK constraints" or "it's the same text re referencing in both table declarations' FK constraint declarations". But the FK constraint names must be unique.

    When you say "In both cases it refers to an internal user," you are confirming that the type and/or table of the referenced column are the same for both FK constraints. But they are different FK constraints for different FKs.

    0 讨论(0)
  • 2021-01-13 16:07

    Names of FKs have to be unique. You need to use two different FK names. I would further suggest that you should include information about the tables being linked in the key name to make it more descriptive.

    0 讨论(0)
提交回复
热议问题