Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2002
    Posts
    19

    Question Unanswered: Sort a table in a DB?

    Hy,
    I have just a small question:
    Can I sort a table in the DB in order to prevent the necessity of the ORDER BY statement in the SQL query?
    And if yes - how?

    regards boa


  2. #2
    Join Date
    Dec 2002
    Location
    BELGIUM
    Posts
    18

    Thumbs up

    Of course, and it's very simple.

    Assume I want to have a table with reverse ordering on the id. You can just create a view on the original table:

    CREATE VIEW v_session_reverse AS
    SELECT * FROM t_session order by id desc


    now, all your future queries can be done on the v_session_reverse, which acts as an ordinary table.

    regards,

    Benny Lootens
    Java Developer
    BELGIUM

  3. #3
    Join Date
    Nov 2002
    Posts
    19
    ...and if I delete a tuple from the view - is this tuple then deleted in the table as well?

    boa

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    if the database allows you to delete a row from the view, then yes, you have indeed deleted the row from the table

    a view is "virtual" and does not have any rows itself

    by the way, boaconstrictor, your original question could be interpreted two ways

    first, how to get ordered results without using an ORDER BY yourself -- the view fills that job nicely

    however, you have not actually "avoided" the sort -- the ORDER BY is in the view and it does get executed when you use the view, unless you override it with a different ORDER BY


    rudy
    http://rudy.ca/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •