Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Re: Re: mysql auto_incremented id

by cchampion (Curate)
on Dec 17, 2003 at 22:08 UTC ( #315398=note: print w/replies, xml ) Need Help??

in reply to Re: Re: mysql auto_incremented id
in thread mysql auto_incremented id

How do you deal with first and last image then?

I insist on my belief that a primary key should be doing only one task, i.e. identifying the record uniquely. If you need a display id, you have two choices:

  • Adding a field to your table for display purposes, but then you would have two further problems.
    • filling the gaps when you delete a record.
    • Always displaying the records in the same order. No chance of diplaying with different criteria.
  • Creating a display number when you select the records. And you can do it either in (at least) two ways:
    • with a MySQL variable.
      SELECT @count := @count+1 as sequence, id, name, filename from mytable
    • In your Perl script.
      my $query = "SELECT id, name, filename from mytable"; my $sth=$dbh->prepare($query); $sth->execute(); my @results; my $count =0; while (my $row = $sth->fetchrow_arrayref) { push @results, [ $count++ , @$row ] }
      Now your @results have a counter that you can use for displaying purposes.
      Modifying your query with a different WHERE or ORDER BY will change the display order.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://315398]
[nysus]: What happened to the submit button? All I have is a preview button.
[erix]: perlmonks has become read-only ?
[marto]: erix unlikely, we've had posts within the last few minutes ;)
[ambrus]: nysus: make sure you enter at least two lines for the title and then preview, and read warnings the preview form prints

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2017-12-15 11:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.