UNIQUE 约束,仅当字段包含特定值时

发布于 2024-10-16 18:37:33 字数 484 浏览 10 评论 0原文

我正在尝试为两列创建 UNIQUE INDEX 约束,但前提是另一列包含值 1。例如,column_1column_2 仅当 <代码>活动= 1 。任何包含 active = 0 的行都可以与另一行共享 column_1column_2 的值,无论另一行的 值是什么活跃 是。但 active = 1 的行无法与 active = 1 的另一行共享 column_1column_2 的值。

我所说的“共享”是指同一列中具有相同值的两行。示例:row1.a = row2.a AND row1.b = row2.b。仅当 row1 中的两列与 row2 中的其他两列匹配时,才会共享值。

我希望我说清楚了。 :\

I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1. For example, column_1 and column_2 should be UNIQUE only when active = 1. Any rows that contain active = 0 can share values for column_1 and column_2 with another row, regardless of what the other row's value for active is. But rows where active = 1 cannot share values of column_1 or column_2 with another row that has active = 1.

What I mean by "share" is two rows having the same value(s) in the same column(s). Example: row1.a = row2.a AND row1.b = row2.b. Values would be shared only if both columns in row1 matched the other two columns in row2.

I hope I made myself clear. :\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦笑流年记忆 2024-10-23 18:37:33

可以尝试用column_1、column_2和active做多列UNIQUE索引,然后对不需要唯一性的行设置active=NULL。或者,您可以使用触发器(请参阅MySQL触发器语法)
并检查每个插入/更新的行是否这些值已经在表中 - 但我认为这会相当慢。

You can try to make multi-column UNIQUE index with column_1, column_2 and active, and then set active=NULL for the rows where uniqueness not required. Alternatively, you can use triggers (see MySQL trigger syntax)
and check for each inserted/updated row if such values are already in the table - but I think it would be rather slow.

冷弦 2024-10-23 18:37:33

我不确定 MySQL 语法,但它应该与 SQL Server 具有几乎相同的内容:

CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
  ON dbo.[MyTable]([column1,column2)
  WHERE   ([active] = 1);

此索引将确保如果 active=1,则列 1 和列 2 组合在整个表中是唯一的。

I am not sure about MySQL syntax, but it should have pretty much the same thing that SQL Server has:

CREATE UNIQUE INDEX [UNQ_Column1Column2OnActive_MyTable]
  ON dbo.[MyTable]([column1,column2)
  WHERE   ([active] = 1);

This index will make sure if active=1 then column1 and column2 combination is unique across the table.

甲如呢乙后呢 2024-10-23 18:37:33

我正在尝试为两列创建 UNIQUE INDEX 约束,但前提是另一列包含值 1

您可以将“另一列”的值设置为不等于 1 的唯一值。例如记录。

然后,可以将唯一索引约束应用于包括“另一列”在内的所有三列。我们将“另一列”称为“columnX”。
如果要将唯一约束应用于记录,请将columnX 的值设置为1。如果您不想应用唯一约束,请将 columnX 的值设置为唯一值。

然后不需要额外的工作/触发。所有三列的唯一索引可以解决您的问题。

I'm trying to create a UNIQUE INDEX constraint for two columns, but only when another column contains the value 1

You can set the value of "another column" to a unique value that does not equal to 1. for example the id of a record.

Then the unique index constraint could be applied to all three columns including the "another column". Let's call the "another column" columnX.
Set the value of columnX to 1 if you want to apply the unique constraint to a record. Set the value of columnX to a unique value if you don't want to apply the unique constraint.

Then no extra work/triggers needed. The unique index to all three columns could solve your problem.

盛夏尉蓝 2024-10-23 18:37:33

在 SQL Server 中,这可以通过检查约束来完成,但是我不知道 MySQL 是否支持类似的东西。

适用于任何数据库的是,您可以将表分成两部分。如果 active =0 的记录只是历史记录,并且永远不会再次激活,您可以将它们移动到另一个表,并在原始表上设置一个简单的唯一约束。

In SQL Server this could be accomplished with check constraints, however I do not know if MySQL supports anything similar.

What will work on any database, is that you can split the table in two. If the records where active =0 are just history records, and will never become active again, you could just move them to another table, and set a simple unique constraint on the original table.

丘比特射中我 2024-10-23 18:37:33

我不确定我是否 100% 理解您,但假设您有一个带有状态列的表,并且您想确保只有一个原始状态为“A”(活动)。您可以接受许多状态为“I”或“Z”或其他任何状态的行。只允许一行的状态为“A”。

这样就可以解决问题了。

  CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
  ON [dbo].[EvalHeader]([Hdr Status])
  WHERE [Hdr Status] = 'A';

I am not sure I understand you 100% but lets say you have a table that has a status column and you want to make sure there is only one raw with a status of 'A' (Active). You are OK with many rows with statuses of 'I' or 'Z' or anything else. Only one row is allowed with status of 'A'.

This will do the trick.

  CREATE UNIQUE INDEX [Idx_EvalHeaderOnlyOneActive]
  ON [dbo].[EvalHeader]([Hdr Status])
  WHERE [Hdr Status] = 'A';
止于盛夏 2024-10-23 18:37:33

指数与外部影响无关。这种约束必须在数据库外部实现。

indexes are agnostic of external influences. This kind of constraint would have to be implemented outside your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文