mysql bidirectional composite primary key code example

Example 1: mysql bidirectional composite primary key

CREATE TRIGGER uinsert BEFORE INSERT ON tbl_challenger
 FOR EACH ROW SET NEW.u0 = LEAST(NEW.host,NEW.challenger),
  NEW.u1 = GREATEST(NEW.host,NEW.challenger);
#same for update
CREATE UNIQUE INDEX uniqueness ON tbl_challenger(u0,u1);

Example 2: mysql bidirectional composite primary key

create trigger bi_foo before insert on foo
for each row
begin
  if exists(select 1 from foo where bar1 = NEW.bar2 and bar2 = NEW.bar1)
  then
    signal sqlstate '50000' set message_text="Oops";
  end if;
end

Tags:

Misc Example