简体   繁体   English

仅当第二列是特定值时,两列的 MySQL 唯一约束

[英]MySQL unique constraint of two columns only if second column is a specific value

Hi I have a MysQL table in which I save the clients嗨,我有一个 Mysql 表,我在其中保存了客户端

In one column I save the IP Address,在一栏中,我保存了 IP 地址,

In other column I set if the client is Active or Not Also I save the name, mail, etc...在另一列中,我设置了客户端是否处于活动状态我还保存了名称、邮件等...

If one client left, we just set the Active column to 0, (to keep Payments history and so), when new client arrives we can reassign the same IP如果有一个客户离开,我们只需将 Active 列设置为 0,(以保留付款历史等),当新客户到达时,我们可以重新分配相同的 IP

Is there a way to create a UNIQUE constraint to allow just one IP that is ACTIVE ?有没有办法创建一个 UNIQUE 约束来只允许一个 ACTIVE 的 IP?

example例子

IP    |   ACTIVE  |   DATE
---------------------------------
102   |      1    |    2017-10-12
103   |      1    |    2017-10-12
103   |      0    |    2017-10-13
103   |      0    |    2017-10-09

So It can be multiples rows with the IP and 0 but only one with the IP and 1所以它可以是带有IP和0的多行,但只有带有IP和1的一行

I'm thinking about a trigger before INSERT, UPDATE that do a SELECT and verify if there is no other row with that IP and 1 in ACTIVE我正在考虑 INSERT、UPDATE 之前的触发器,它执行 SELECT 并验证是否没有其他行具有该 IP 和 ACTIVE 中的 1

Suggestions?建议?

Alternative 1备选方案 1

Instead of using 0 to indicate not active, why don't you use a negative number?为什么不使用0表示不活动,而是使用负数? That way, you could maintain uniqueness on the IP / Active pair of columns, and only the record with Active = 1 would be considered active.I am not sure what your logic is in terms of in-activating, but essentially, you would just pick a negative number one smaller than the existing one (or -1 if it is the first one) to update the record with.这样,您可以在 IP / Active 对列上保持唯一性,并且只有Active = 1的记录才会被视为活动。我不确定您的逻辑是什么,但基本上,您只会选择一个比现有数字小的负数(如果是第一个,则为-1 )以更新记录。

Alternative 2备选方案 2

Dispense with the Active column altogether, and create another table to hold IP history.完全取消Active列,并创建另一个表来保存 IP 历史记录。 So... in your main table, the uniqueness constraint is on the IP address.所以......在你的主表中,唯一性约束是在 IP 地址上。 In your history table, you can have a uniqueness constraint on the IP address and the date it became historical.在您的历史表中,您可以对 IP 地址及其成为历史的日期设置唯一性约束。

EDIT编辑

Based on the comment by @MichelFeldheim, I would suggest that you include the user email address as part of the uniqueness constraint.根据@MichelFeldheim 的评论,我建议您将用户电子邮件地址作为唯一性约束的一部分。 Basically meaning that each unique user would be allowed one active IP address.基本上意味着每个唯一用户将被允许使用一个活动 IP 地址。 It would also allow more than one active user per IP address (or organization, eg).它还允许每个 IP 地址(或组织,例如)有一个以上的活动用户。

Use the values 1 and NULL instead of 1 and 0 .使用值1NULL而不是10

You can then add UNIQUE KEY (ip, active) to the table and only one row per IP combination with a value of 1 will be permitted, but infinite rows with NULL will not violate the unique constraint.然后,您可以将UNIQUE KEY (ip, active)到表中,并且每个 IP 组合仅允许一行值为 1,但具有NULL无限行不会违反唯一约束。

Remember that NULL = NULL is not true, because NULL is not a value.请记住NULL = NULL不是真的,因为NULL不是一个值。 It is a marker signifying the absence of a value.它是一个标记,表示没有值。 Two absent values cannot be said to be identical, so multiple rows with NULL for the same value of ip will not be considered duplicates, so they can't violate a unique constraint.两个不存在的值不能说是相同的,因此对于相同的ip值具有NULL多行不会被视为重复,因此它们不能违反唯一约束。

Aside: Noting that NULL = NULL is not true, as mentioned above, it is just as important to note that, it is not false, either.旁白:注意NULL = NULL不是真的,如上所述,同样重要的是要注意,它也不是假的。 Similarly, NULL != NULL is also neither true nor false, because -- by definition -- a lack of a value is not something that can properly be compared to anything, including another lack of a value.类似地, NULL != NULL也不是真也不是假,因为——根据定义——值的缺失不能与任何东西进行比较,包括另一个值的缺失。

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

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