USE vn;

CREATE TEMPORARY TABLE tmp.tt
	SELECT MIN(id) id, saleFk, workerFk, stateFk 
		FROM saleTracking 
		GROUP BY saleFk, workerFk, stateFk 
		HAVING COUNT(*) > 1;
	
DELETE st FROM saleTracking st 
		JOIN tmp.tt USING(saleFk, workerFk, stateFk)
	WHERE tt.id <> st.id;

ALTER TABLE saleTracking 
	ADD CONSTRAINT saleTracking_UN 
	UNIQUE KEY (saleFk, workerFk, stateFk);

DROP TEMPORARY TABLE tmp.tt;
