简体   繁体   English

跨多个表的唯一键

[英]Unique key across multiple tables

Rewrote the question: 改写问题:

I have three tables, objects, owners and objectOwnerRelation 我有三个表,对象,所有者和objectOwnerRelation

objects has three columns: id, name, type 对象具有三列:id,名称,类型

owners has two columns: id, owner 所有者有两列:id,所有者

objectOwnerRelation has three columns: id, objectId, ownerId objectOwnerRelation具有三列:id,objectId,ownerId

objectId = id in objects objectId =对象中的id

ownerId = id in owners ownerId =所有者中的ID

Two objects can have the same name and type but only if the owner is different. 两个对象可以具有相同的名称和类型,但前提是所有者不同。 That means that two objects with the same name and type can't appear in the objectOwnerRelation with the same ownerId 这意味着具有相同名称和类型的两个对象不能出现在具有相同ownerId的objectOwnerRelation中

An object can have multiple owners and a owner can have multiple objects. 一个对象可以有多个所有者,一个所有者可以有多个对象。

There is no way to enforce the constraint: 无法强制执行约束:

Two objects can have the same name and type but only if the owner is different. 两个对象可以具有相同的名称和类型,但前提是所有者不同。

using only declarative referential integrity (DRI). 仅使用声明性参照完整性(DRI)。 Using just DRI you could create a table that contains all of the columns that you need to build a unique key. 仅使用DRI,您可以创建一个表,其中包含构建唯一键所需的所有列。 You could denormalize object.name and object.type to your objectOwnerRelation table. 您可以将object.nameobject.type规范化为objectOwnerRelation表。 This would ensure that there are no records in objectOwnerRelation with the same name and type and owner but only in the intersection table itself. 这将确保objectOwnerRelation中没有名称类型所有者相同的记录,而仅在交集表本身中。

At the end of the day, DRI does not allow a child table to impose a constraint on a parent. 到最后, DRI不允许子表对父表施加约束。

The constraint that you are looking for needs to be imposed procedurally, using a trigger or some other code. 您正在寻找的约束需要使用触发器或其他一些代码以程序方式施加。

You have to change the structure of objectOwnerRelation 您必须更改objectOwnerRelation的结构

ownerid, objectid and PK on both 两者上的ownerid,objectid和PK

You can leave the structure as it is and add a UNIQUE INDEX on ownerid and objectid but each row can be identified through ownerid and objectid 您可以保留结构,并在ownerid和objectid上添加UNIQUE INDEX,但是每一行都可以通过ownerid和objectid进行标识

That means that two objects with the same name and type can't appear in the objectOwnerRelation with the same ownerId. 这意味着具有相同名称和类型的两个对象不能出现在具有相同ownerId的objectOwnerRelation中。


An object can have multiple owners and a owner can have multiple objects. 一个对象可以有多个所有者,一个所有者可以有多个对象。


These two sentences make it quite simple actually, the main problem is the sentence that somehow implies that objects can change names depending on the owner -- hence Joel's answer. 这两个句子实际上使它变得非常简单,主要的问题是该句子某种程度上意味着对象可以根据所有者更改名称-因此是乔尔的答案。

So here is the simple solution 所以这是简单的解决方案

在此处输入图片说明


Now the troublesome sentence 现在麻烦的句子

Two objects can have the same name and type but only if the owner is different. 两个对象可以具有相同的名称和类型,但前提是所有者不同。

Does this mean that objects can be renamed? 这是否意味着可以重命名对象? That their types can be renamed/changed by an owner? 所有者可以重命名/更改其类型吗?

So -- as you see -- the main problem here is that your question can be (mis)interpreted in many ways. 所以-正如您所看到的-这里的主要问题是您的问题可以用多种方式(错误)解释。

<rant>

Technically, this design encountered a problem on the conceptual level, way before it reached entities (logical level) and tables (physical level). 从技术上讲,此设计在概念级别上遇到了问题,即到达实体(逻辑级别)和表(物理级别)之前。 Evan small changes on the conceptual level of design can create huge differences on logical and physical levels -- as obvious from submitted answers and comments to your post. 在设计的概念层次上进行微小的变更会在逻辑和物理层次上产生巨大差异-从提交的答案和评论到您的帖子中显而易见。

</rant>

In you question it looks like combination of table name and type are needs to be unique per objectownerid. 在您的问题中,看起来表名和类型的组合对于每个objectownerid而言都是唯一的。

My recommendation is to add ownerID to objects and create unique index on ([name],[type],[ownerid]), this way you can enforce you constraint. 我的建议是将ownerID添加到对象,并在([name],[type],[ownerid])上创建唯一索引,这样您就可以强制执行约束。

Another solution which will allow to keep current structure is to create trigger on the table that constraint should be enforced on, do the checks and rollback in case of constraint violation. 允许保持当前结构的另一种解决方案是在应强制执行约束的表上创建触发器,在发生约束违例时进行检查和回滚。

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

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