Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: The fine art of database programming

by mpeppler (Vicar)
on Apr 30, 2002 at 00:57 UTC ( #162971=note: print w/ replies, xml ) Need Help??


in reply to The fine art of database programming

One additional pitfall for most people using a relational database for the first time: there are no record numbers!

An RDBMS is based on set theory, and a particular row has no inherent position in its table (although some database systems fake this).

Hence the very common question: how do I fetch rows 100-120, where the database independant answer is always: use an appropriate WHERE clause.

Michael


Comment on Re: The fine art of database programming
Re: Re: The fine art of database programming
by pdcawley (Hermit) on Apr 30, 2002 at 12:47 UTC
    And there was me thinking it was:
    SELECT * FROM table LIMIT 99, 21;
    No WHERE clause in sight. If you want be be a bit more specific on how things are ordered (rather than insertion order (which I believe is defined as being the standard ordering)), you do
    SELECT * FROM table ORDER BY col LIMIT 99, 21;
    Still no WHERE clause.
      I believe that LIMIT is a DBMS-specific extension. For example...
      in MS SQL Server:
      SELECT TOP 10 col FROM table
      in IBM DB2:
      SELECT col FROM table FETCH FIRST 10 ROWS ONLY
      I think the only platform independent way to do this is:
      SELECT col FROM table A WHERE 10 > (SELECT COUNT(*) FROM table B WHERE A.col < B.col) ORDER BY col DESC
      I am sorry, pdcawley, but I have to disagree.
      One of the fundaments of the relational model is that you don't make assumptions on the physical storage of the records. What you are suggesting is quite dangerous, as the following example shows.
      mysql> describe test_order; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | | PRI | 0 | | | name | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> select * from test_order; #all the records +----+------+ | id | name | +----+------+ | 1 | aaaa | | 2 | bbbb | | 3 | cccc | | 4 | dddd | | 5 | eeee | | 6 | ffff | | 7 | gggg | | 8 | hhhh | | 9 | iiii | | 10 | jjjj | +----+------+ 10 rows in set (0.01 sec) mysql> select * from test_order limit 4,3; #1st request. OK so far +----+------+ | id | name | +----+------+ | 5 | eeee | | 6 | ffff | | 7 | gggg | +----+------+ 3 rows in set (0.00 sec) # now we remove one of the records we were selecting through LIMIT mysql> delete from test_order where id = 5; Query OK, 1 row affected (0.00 sec) # and we insert another record mysql> insert into test_order values (11, "kkkk"); Query OK, 1 row affected (0.00 sec) # Then we insert the deleted record again mysql> insert into test_order values (5, "eeee"); Query OK, 1 row affected (0.00 sec) mysql> select * from test_order limit 4,3; +----+------+ | id | name | +----+------+ | 11 | kkkk | | 6 | ffff | | 7 | gggg | +----+------+ 3 rows in set (0.00 sec)
      The same query, after a couple of delete/insert statements, gives you different results.
      It is true that you can correct the result using the ORDER BY clause, but you are not dealing with "record numbers".
      The LIMIT clause, for those database that support it, guarantees that N records from the result dataset, starting at a given position are returned. There is no assumption about their position in the table.
      If you want to return correct records, use the WHERE clause.
       _  _ _  _  
      (_|| | |(_|><
       _|   
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-12-18 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls