简体   繁体   English

在表中的两列复合上处理具有持久UNIQUE约束的重复项?

[英]handling duplicates with a persistent UNIQUE constraint on a composite of two columns in a table?

I have a number of records that I would like to insert into a MYSQL table. 我有许多记录要插入到MYSQL表中。 I am using PHP. 我正在使用PHP。 Some of them are duplicates. 其中一些是重复的。 I am using the redbeanPHP orm ( http://redbeanphp.com/ ) 我正在使用redbeanPHP orm( http://redbeanphp.com/

My strategy is to use a composite UNIQUE constraint made of 2 columns( FIELD A and DUPLICATENUMBER ). 我的策略是使用由2列(FIELD A和DUPLICATENUMBER)组成的复合UNIQUE约束。 When each record is created DUPLICATENUMBER = 0. 创建每条记录时DUPLICATENUMBER = 0。

Then try to insert the record as follows: 然后尝试按如下方式插入记录:

try {             
                 $id = R::store($record);
} catch (Exception $exc) {
---------------

If a duplicate tries to be inserted an error will be generated. 如果尝试插入重复项,则会生成错误。 I then want to increment DUPLICATENUMBER by 1, and reinsert. 然后我想将DUPLICATENUMBER增加1,然后重新插入。 This time it should work if both FIELD A and DUPLICATENUMBER have to be the same for the unique constraint to be intact, because incrementing DUPLICATENUMBER , should break the uniqueness. 这一次,如果FIELD A和DUPLICATENUMBER必须相同,唯一约束必须完整,因为递增DUPLICATENUMBER应该打破唯一性。 What MYSQL command should I issue to set up this combined Unique constraint? 我应该发出什么MYSQL命令来设置这个组合的唯一约束?

Thanks in advance, 提前致谢,

Bill 法案

Thanks in advance, 提前致谢,

您可以选择具有匹配的FIELD A的记录数,并将其设置为DUPLICATENUMBER。

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

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