Thursday 15 July 2010

sql - Insert Ignore in MySql when there's primary key that auto increments -


When there is a column that is the primary key , and that auto increment .

I have primary ID on ID (primary key auto increment), and userId and LMCL No preference.

like this:

  id | UserId | LMCOL -------------------- 1 | 1 | Allow 1/2. 1 | Allow 2/3 1 | Allow 3/4 1 | 1 // If it is re-inserted then it is not allowed I have placed it here for just 5 example 2. Allow 1/6. 2 | Allow 2/7 2 | 3 / Allow 8 2 | 1 // If it is inserted then it is not allowed I have put it here for example   

I am using MySQL and MyIsam type tables. I can and insert

INSERT IGNORE To work in your case, you need to create a unique index on (userId, elmCol) .

  ALTER TABLE table_name ADD CONSTRAINT U_userId_elmCol unique (UIIID, LMCOL);   

Here is the demo.

No comments:

Post a Comment