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

StackOverflow https://stackoverflow.com/questions/15790832

  •  01-04-2022
  •  | 
  •  

Question

I have a number of records that I would like to insert into a MYSQL table. I am using PHP. Some of them are duplicates. I am using the redbeanPHP orm (http://redbeanphp.com/)

My strategy is to use a composite UNIQUE constraint made of 2 columns( FIELD A and DUPLICATENUMBER ). When each record is created 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. 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. What MYSQL command should I issue to set up this combined Unique constraint?

Thanks in advance,

Bill

Thanks in advance,

Was it helpful?

Solution

You could select the number of records with a matching FIELD A, and set this as the DUPLICATENUMBER.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top