Q:

MYSQL Insert...on duplicate value insert again with increment

I have 5 columns a, b, c, d and e.
I want to insert values into first 4 columns and have kept default value as 1 in last column. 
I have kept the first 4 columns as primary keys. 
If duplicate values are inserted in the first 4 columns, then I want to insert the same values again but with increment in the 5th column. 
Eg: if 1,2,3,4 are values for the first 4 columns.  then when 1,2,3,4 comes again they need to be inserted as 1,2,3,4 but with fifth column as 2. 

A:

First remove the primary key and add a unique key instead. 
ALTER TABLE test DROP PRIMARY KEY, ADD UNIQUE KEY 'test_uIdx_a_b_c_d_e' ( a,b,c,d,e );

Assuming your table now looks like this
CREATE TABLE test (
    a int NOT NULL DEFAULT '0',
    b int NOT NULL DEFAULT '0',
    c int NOT NULL DEFAULT '0',
    d int NOT NULL DEFAULT '0',
    e int NOT NULL DEFAULT '0',
    UNIQUE KEY 'test_uIdx_a_b_c_d_e' ( a,b,c,d,e )
)

Then you can create a trigger on before insert
DELIMITER ;;
CREATE TRIGGER before_test_insert 
BEFORE INSERT ON test
FOR EACH ROW
BEGIN
    SET NEW.e := ( SELECT COUNT(*)+1 FROM test WHERE a = NEW.a AND b = NEW.b AND c = NEW.c AND d = NEW.d;
END;;

DELIMITER ;

This will set field e to the number of occurrences of a,b,c,d
One of the comments above offered a simpler alternative
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

