Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Perl DBI and Foreign Keys

by afoken (Canon)
on Apr 09, 2019 at 19:07 UTC ( #1232363=note: print w/replies, xml ) Need Help??


in reply to Re: Perl DBI and Foreign Keys
in thread Perl DBI and Foreign Keys

This may not be practical, but: do not tell the DB to create an autoincrement id for you. Instead give each new table an id YOU create and guaranteed to be unique.

That's commonly called a sequence and is often implemented in the DB server, guaranteed to deliver unique IDs under all circumstances.

Other DB servers offer only an automatic ID generator, and for that, you need to use last_insert_id in DBI (see SELECT LAST_INSERT_ID does not work).


Related: Some very common errors (or, if you want to be polite, misconceptions):

The ID generated by either of these mechanisms is JUST THAT. A unique value.

  • In any usage scenario with more than one user and/or more than one process/thread and/or more than one CPU core, the next ID is not predictable.
  • The ID is not a sort criteria.
  • IDs are commonly set up to increment by one for each use. But that's an inplementation detail. They could also be set up to return multiples of seven starting from a ridiculously large positive number and going down to ridiculously large negative number. The returned IDs would still be unique. The bits from such a sequence could even be shuffled around so that any returned ID looks completely random (but still is unique).
  • The numeric sequence of used IDs may have gaps, some times big gaps, e.g. after rolling back transactions. This is intentionally and by design to guarantee uniqueness. It does not hurt. It does not waste anything. Trying to fill those gaps is an error. The IDs in those gaps were once used, filling the gaps would reuse IDs and make them non-unique.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: Perl DBI and Foreign Keys
by bliako (Prior) on Apr 10, 2019 at 09:32 UTC

    really useful to know, thanks, bliako

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-07-12 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?