Hello I have a MYSQL Table where I want to prevent same data in 3 columns, it goes like:

Viewed 38

Essentially it is used for product barcode and subbarcode. Let say I have 3 columns, priBarcode, subBarcode1, subBarcode2, what I want is, for this 3 columns to never contain duplicate data to each others.

For example:

|priBarcode|subBarcode1|subBarcode2|

|a1 | a11 |a111|

|b2 | b22 |b222|

They must all be unique given they are in different columns. Any way I can achieve this?

Thanks!

1 Answers

You need just to create Trigger before insert and later just put your conditions inside it .

Here is example what you are looking for.

drop table if exists testtable;
create table testtable (
  priBarcode varchar(40),    -- this is the natural key to be unique indexed
  subBarcode1 varchar(40),
  subBarcode2 varchar(40));      

alter table testtable add unique index testtable_ux (priBarcode);



drop trigger if exists testtable_uid;
delimiter //
create trigger testtable_uid before insert on testtable for each row
begin


     IF NEW.priBarcode in (
            select A.priBarcode
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on priBarcode`;
        END IF;
        
        IF NEW.priBarcode in (
            select A.subBarcode1
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on priBarcode -subBarcode1`;
        END IF;
        
            IF NEW.priBarcode in (
            select A.subBarcode2
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on priBarcode -subBarcode2`;
        END IF;
        
    
         IF NEW.subBarcode1 in (
            select A.subBarcode1
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode1`;
        END IF;
        
               IF NEW.subBarcode1 in (
            select A.priBarcode
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode1 -priBarcode`;
        END IF;
        
               IF NEW.subBarcode1 in (
            select A.subBarcode2
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode1 -subBarcode2`;
        END IF;
        
             IF NEW.subBarcode2 in (
            select A.subBarcode2
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode2`;
        END IF;
        
        
                   IF NEW.subBarcode2 in (
            select A.subBarcode1
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode2 -subBarcode1`;
        END IF;
        
                   IF NEW.subBarcode2 in (
            select A.priBarcode
            From testtable A  -- CHANGED THE ALIAS TO A
        ) THEN -- MISSING THEN
           CALL `Insert not allowed duplicate on subBarcode2 -priBarcode`;
        END IF;
        
        
        
end //
delimiter ;

# First insert
insert into testtable (priBarcode, subBarcode1, subBarcode2) values ('a1', 'a11', 'a111'), 
  ('b2', 'b22', 'b222');
select * from testtable;

# Second failed! insert - because of duplicate value a111
insert into testtable (priBarcode, subBarcode1, subBarcode2) values ('a13213', 'a111', 'dasd');
select * from testtable;

# Third failed! insert because of duplicate a11 value
insert into testtable (priBarcode, subBarcode1, subBarcode2) values ('a11', 'b56', 'bhj1');
select * from testtable;

As you can see in code there are 3 insert statemenets. Only first will take place because trigger check if there is duplicate in each column. So finally output will looks like ( and there take place only FIRST insert , because trigger make fails on 2 others cause of duplicates : enter image description here

Related