rnumbering a table numbered 14 7 9 14 (newbe like)

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default rnumbering a table numbered 14 7 9 14 (newbe like)

    Hi,

    I have been fooling around trying to learn mySql and I have a table wich
    ID's are not in continuous numbers: 1 4 8 13.

    How do we renumber a table as 1 2 3 4 5 etc?

    Jean Pierre
    --

    --
    [url]http://web.jeanpierredaviau.com[/url]


    Québec Guest

  2. Similar Questions and Discussions

    1. Can't make bullets, numbered lists or indent
      When editing a page in Contribute 3, and I'm not in a table, all the editing icons and menu items seem to work except bullets, numbered lists,...
    2. PDF Numbered Bookmarks Similar to Word Document Map
      When converting from .doc to .pdf using PDFMaker from within Microsoft Word, is it possible to automatically create numbered bookmarks similar to...
    3. Numbered Pages
      I have seen the light. I can create multiple paged documents in Acrobat 6.0 and print them out. My desire now is to have Acrobat number these pages...
    4. How to make a file where the pages are numbered correctly?
      I'm supposed to print a small "book", page size A5, 40 pages. How can I arrange the pages easily so that they are in the right place when you...
    5. Batch import multiple .Tif as numbered frames
      select the 210 cast members, then select castToTime function from, I think the modify menu. The member sequence wil be inserted over 210 frames,...
  3. #2

    Default Re: rnumbering a table numbered 14 7 9 14 (newbe like)

    On Wed, 8 Sep 2004 13:23:26 -0400, "Québec" <Once@WasEno.ugh> wrote:
    >I have been fooling around trying to learn mySql and I have a table wich
    >ID's are not in continuous numbers: 1 4 8 13.
    >
    >How do we renumber a table as 1 2 3 4 5 etc?
    First off - why do you want to do this? If they're IDs, then gaps aren't
    significant.

    If you still want to, then you can't from MySQL, so do it from PHP with a
    loop.

    <?php
    $conn = mysql_connect()
    or die(mysql_error());
    mysql_select_db('test')
    or die(mysql_error());

    mysql_query('drop table if exists t')
    or die(mysql_error());

    mysql_query('create table t (id int unsigned not null primary key)')
    or die(mysql_error());

    foreach (array(1,4,8,13) as $v)
    mysql_query(sprintf('insert into t values (%d)', $v))
    or die(mysql_error());

    $res = mysql_query('select id from t order by id')
    or die(mysql_error());
    while ($row = mysql_fetch_assoc($res))
    print $row['id'] . '<br>';

    print "<hr>";

    mysql_query('lock table t');

    $res = mysql_query('select id from t order by id')
    or die(mysql_error());

    $id = 1;
    while ($row = mysql_fetch_assoc($res))
    mysql_query(
    sprintf('update t set id=%d where id=%d', $id++, $row['id']));

    $res = mysql_query('select id from t order by id')
    or die(mysql_error());
    while ($row = mysql_fetch_assoc($res))
    print $row['id'] . '<br>';
    ?>

    Outputs:

    1
    4
    8
    13
    ---------------
    1
    2
    3
    4

    But note that this script is woefully incomplete, because presumably you'll
    have foreign keys referencing this table, which would all have to be updated as
    well. Which is a pretty good reason for not doing it.

    --
    Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
    Andy Hassall Guest

  4. #3

    Default Re: rnumbering a table numbered 14 7 9 14 (newbe like)

    Yes.


    Thank you.

    Jean
    "Andy Hassall" <andy@andyh.co.uk> a écrit dans le message de
    news:99quj0lu6f4s6gr6ranqp70hfi1b7agdie@4ax.com...
    > On Wed, 8 Sep 2004 13:23:26 -0400, "Québec" <Once@WasEno.ugh> wrote:
    >
    > >I have been fooling around trying to learn mySql and I have a table wich
    > >ID's are not in continuous numbers: 1 4 8 13.
    > >
    > >How do we renumber a table as 1 2 3 4 5 etc?
    >
    > First off - why do you want to do this? If they're IDs, then gaps aren't
    > significant.
    >
    > If you still want to, then you can't from MySQL, so do it from PHP with a
    > loop.
    >
    > <?php
    > $conn = mysql_connect()
    > or die(mysql_error());
    > mysql_select_db('test')
    > or die(mysql_error());
    >
    > mysql_query('drop table if exists t')
    > or die(mysql_error());
    >
    > mysql_query('create table t (id int unsigned not null primary key)')
    > or die(mysql_error());
    >
    > foreach (array(1,4,8,13) as $v)
    > mysql_query(sprintf('insert into t values (%d)', $v))
    > or die(mysql_error());
    >
    > $res = mysql_query('select id from t order by id')
    > or die(mysql_error());
    > while ($row = mysql_fetch_assoc($res))
    > print $row['id'] . '<br>';
    >
    > print "<hr>";
    >
    > mysql_query('lock table t');
    >
    > $res = mysql_query('select id from t order by id')
    > or die(mysql_error());
    >
    > $id = 1;
    > while ($row = mysql_fetch_assoc($res))
    > mysql_query(
    > sprintf('update t set id=%d where id=%d', $id++, $row['id']));
    >
    > $res = mysql_query('select id from t order by id')
    > or die(mysql_error());
    > while ($row = mysql_fetch_assoc($res))
    > print $row['id'] . '<br>';
    > ?>
    >
    > Outputs:
    >
    > 1
    > 4
    > 8
    > 13
    > ---------------
    > 1
    > 2
    > 3
    > 4
    >
    > But note that this script is woefully incomplete, because presumably
    you'll
    > have foreign keys referencing this table, which would all have to be
    updated as
    > well. Which is a pretty good reason for not doing it.
    >
    > --
    > Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
    > <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

    Québec Guest

  5. #4

    Default Re: rnumbering a table numbered 14 7 9 14 (newbe like)

    *** Québec wrote/escribió (Wed, 8 Sep 2004 13:23:26 -0400):
    > I have been fooling around trying to learn mySql and I have a table wich
    > ID's are not in continuous numbers: 1 4 8 13.
    >
    > How do we renumber a table as 1 2 3 4 5 etc?
    I guess you only want to do something as simple as displaying line numbers
    on screen:

    $res=mysql_query('SELECT * FROM table');
    $line=0;
    while($row=mysql_fetch_assoc($res)){
    $line++;

    print $line . ': ' . $row['field1'] . ', ' . $row['field2'] . '<br>';
    }


    If your application design depends on having consecutive IDs in your
    database, you'd better redesign it from scratch.

    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- Thank you for not e-mailing me your questions
    --
    Alvaro G Vicario Guest

  6. #5

    Default Re: rnumbering a table numbered 14 7 9 14 (newbe like)

    Emptying a data base set everything to zero and keep the database structure?

    Jean Pierre




    Québec Guest

  7. #6

    Default Re: rnumbering a table numbered 14 7 9 14 (newbe like)

    I will just try it now on a foo table
    Thanks


    Québec Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139