sql - How do i write a trigger in mysql to check for duplicate records before inserting -


users(id,company,name)

schema table. how should write trigger check duplicates before inserting new row. need check incoming data not duplicated incoming record. if incoming record existing record should not inserted table. if not duplicating record hould inserted!

below trigger tried gives error @ 'last end' line

delimiter $$ create trigger userduplicate  before insert on `users` each row  begin     declare recordid varchar(999);      set recordid = (select id users company= new.companyand name = new.name);      if recordid not null             insert users (company,name) values (new.name,new.company);     end if; end; $$ delimiter; 

according accepted answer here: how abort insert operation in mysql trigger? can not have mysql trigger cancel insert. it's easy on postgresql (before insert trigger returns null), mysql can not handle that.

the thing can do, though, insert ignore:

insert ignore users (company, name)  values ('abc','def'); 

Comments