简体   繁体   English

SQL中的表能否将多列作为仅引用另一表的一个主键的外键?

[英]Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table?

For instance, table Companies has columns company_name, first_contact, second_contact and table contacts have columns id(PK), name, phone. 例如,表Companies的列有company_name,first_contact,second_contact,表联系人的列有id(PK),名称,电话。 Can a table (companies) in SQL have multiple columns as foreign keys that refer only to one primary key of another table (contacts)? SQL中的表(公司)是否可以有多列作为外键,它们仅引用另一张表(联系人)的一个主键?

Yes, here's an example: 是的,这里有一个例子:

mysql> CREATE TABLE Contacts (id INT PRIMARY KEY);
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE Companies (id INT PRIMARY KEY, company_name TEXT,
    -> first_contact INT, second_contact INT,
    -> FOREIGN KEY (first_contact) REFERENCES Contacts(id),
    -> FOREIGN KEY (second_contact) REFERENCES Contacts(id)
    -> );
Query OK, 0 rows affected (0.03 sec)

But it would be more common to design the database another way, with a third table instead of the two foreign keys in Companies: 但是用另一种方法设计数据库会更常见,用第三张表而不是公司中的两个外键:

mysql> CREATE TABLE CompanyContacts (
    -> contact_id INT NOT NULL,
    -> company_id INT NOT NULL,
    -> is_primary BOOL NOT NULL,
    -> PRIMARY KEY (contact_id, company_id),
    -> FOREIGN KEY (contact_id) REFERENCES Contacts(id),
    -> FOREIGN KEY (company_id) REFERENCES Companies(id)
    -> );
Query OK, 0 rows affected (0.04 sec)

Some advantages: 一些优点:

  • You aren't limited to two contacts per company. 每个公司不限于两个联系人。
  • You can search for a contact more simply — instead of searching if a contact occurs as either the first_contact or second_contact, you just search for it in CompanyContacts.contact_id. 您可以更简单地搜索联系人-只需在CompanyContacts.contact_id中进行搜索,而不是搜索联系人是first_contact还是second_contact。 It's easier to optimize that query with an index. 使用索引优化查询更为容易。

Some disadvantages: 一些缺点:

  • No way to make a constraint to make at least one contact mandatory. 没有办法使至少一个联系人成为必需的约束。 You can do this by declaring first_contact as NOT NULL in your design, but there's no SQL constraint that requires a row to exist in the third table for each company. 您可以通过在设计中将first_contact声明为NOT NULL来做到这一点,但是没有SQL约束要求每个公司的第三张表中都存在一行。
  • If you're put off by JOIN queries, this might not be attractive. 如果您因JOIN查询而推迟,这可能不会很有吸引力。 But I recommend you get comfortable with doing JOINs when you have many-to-many relationships. 但是,我建议您在拥有多对多关系的情况下熟悉JOIN。

Sure! 当然! You'd essentially have several one-to-one relationships from Companies to Contacts. 从公司到联系人,您实际上具有几种一对一的关系。

When querying the data, you'd have to join in the Contacts table multiple times (once per column that is a foreign key) 查询数据时,您必须多次加入Contacts表(每列一次为外键)

select *
from Companies c
join Contacts contact1 on c.first_contact=contact1.id
join Contacts contact2 on c.second_contact=contact2.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复合主键的每个列都引用另一个表的主键。 该表的外键应该是什么? - Composite Primary Key's columns each refer to a Primary Key of another table. What should be the Foreign Keys for this table? 复合主键可以让外键引用父表吗 - is it ok for composite primary key to have foreign key refer to parent table 外键如何引用多个主键 - How a foreign key refer multiple primary keys 外键是否可以引用自己的表的主键? - Can a foreign key refer to the primary key of its own table? 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table? 选择另一个表中没有外键的主键 - Selecting primary keys that do not have foreign keys in another table Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys 如何在作为主键和候选键的表中引用多个外键? - How to reference multiple foreign keys in a table that is a primary key and candidate keys? 如何将主键作为外键引用到各种表 - How to refer primary key as Foreign to various table 更新一个表中的主键,这是另一表中的外键 - Update primary key in one table which is foreign key in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM