mysql - Can I re-insert a row with a newly created auto_increment primary key -


edit:

i want update id newly created auto increment id, rest of row's columns not change.

original:

i've got table in mysql looking like:

id    userid    productid 

all columns ubigint, , id primary key, set auto increment.

so want re-insert row same table, newly created id. , old row should deleted!

i'm using id order (higher == newer).

is possible, because other option adding timestamp column , updating column, won't use timestamp. want prevent @ costs.

let's have following table

create table (     `id` int(11) not null auto_increment,     `num` varchar(20) not null, primary key (`id`),     unique (`num`)  ) engine=innodb default charset=utf8; 

then can use following query insert new row

insert (num) values ("hi") on duplicate key update id=last_insert_id()+1; 

if there "hi"-row query update it's id equal next after last inserted

in case

insert table (userid, productid) values (someuserid, someproductid) on duplicate key update id=last_insert_id()+1; 

also, there replace mysql extension.

replace table (userid, productid) values (someuserid, someproductid); 

as on duplicate key update work if table has unique or primary key constraint. unlike on duplicate key update replace first delete existing row , insert new, while on duplicate key update update it.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -