mysql - Prevent auto increment on duplicated entry? -


i have table called url_info , structure of table is:

url_info:

url_id ( auto_increment, primary key ) url ( unique,varchar(500) ) 

when insert table this:

insert url_info(url) values('tom'); insert url_info(url) values('jerry'); 

the output is:

1 tom 2 jerry 

when insert

insert url_info(url) values('tom'); insert url_info(url) values('tom'); insert url_info(url) values('jerry'); 

the output

1 tom 3 jerry 

the auto-increment id incremented when try insert duplicate entry. have tried insert ignore

how prevent incrementing when try insert duplicate entry?

it's worth creating stored procedure insert want table. but, in stored procedure check items have in table. if these match you're trying insert, query should not attempt insert.

ie. procedure needs contain this:

if not exists(select top 1 url_id url_info url = 'tom') insert url_info(url) values('tom') 

so, in stored procedure, (assuming arguments/variables have been declared)

if not exists(select top 1 url_id url_info url = @newurl) insert url_info(url) values(@newurl) 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -