简体   繁体   English

非唯一外键

[英]Non-unique foreign key

Do foreign keys have to be unique?外键必须是唯一的吗?

I'm trying to create a table that stores the foreign key that references to a user and a column 'profileIconId'.我正在尝试创建一个表,该表存储引用用户和列“profileIconId”的外键。 The purpose of the table is to have a list of icons that the user owns.该表的目的是拥有用户拥有的图标列表。 I would like to use cascade delete.我想使用级联删除。

My other choice is to use SELECT FROM WHERE to retrieve the list and use DELETE FROM WHERE to delete all rows that matches the key when the user is removed.我的另一个选择是使用SELECT FROM WHERE检索列表,并使用DELETE FROM WHERE在删除用户时删除与键匹配的所有行。

No, they don't.不,他们没有。 In fact, one of the most common uses of a foreign key is a one-to-many relationship, such as between Customers and Orders , for example.事实上,外键最常见的用途之一是一对多关系,例如在CustomersOrders之间。

No, Foreign Key in a table doesn't have to be unique in said table.不,表中的外键在所述表中不必是唯一的。

BUT, as it is the Primary Key of another table, it must be unique in this table.但是,由于它是另一个表的主键,因此它在该表中必须是唯一的。

No.不。

But the values must exists first on the parent table before you can insert it on the table.但是值必须首先存在于父表中,然后才能将其插入表中。

No, foreign keys do not have to be unique.不,外键不必是唯一的。 Indeed, a lack of uniqueness is requisite for one-to-many or many-to-many relations.事实上,缺乏唯一性是一对多或多对多关系所必需的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM