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);