Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: return primary key if duplicate entry exists?

by CountZero (Bishop)
on Jan 24, 2016 at 19:12 UTC ( [id://1153514]=note: print w/replies, xml ) Need Help??


in reply to Re^4: return primary key if duplicate entry exists?
in thread return primary key if duplicate entry exists?

What is the use of 'UPDATE id = LAST_INSERT_ID(id)'?

According to the MySQL docs LAST_INSERT_ID(id) will return the value of the argument, so you are actually replacing the existing value in the field 'id' by itself and then you are using that value as the next auto-incremented key value. That is likely to create even more errors as now the database doesn't know anymore where it was in the sequence it was using and will happily re-use an already existing number.

if expr is given as an argument to LAST_INSERT_ID(), the value of the argument is returned by the function and is remembered as the next value to be returned by LAST_INSERT_ID().

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^6: return primary key if duplicate entry exists?
by diyaz (Beadle) on Jan 24, 2016 at 19:45 UTC
    After thinking a little bit. I think instead of the basic auto-increment key I will switch to using crc32. I realized this will make things easier as essentially the key is embedded in the data, and I will not need to do a SELECT..WHERE query.
      As I explain below, CRC32 is not a good candidate for a primary key. An auto-incrementing primary key is much better in this case.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found