Results 1 to 5 of 5

Thread: Condense MySql Database

  1. #1
    Join Date
    Oct 2008
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Condense MySql Database

    I have a mysql database that has more and more data added to it daily. Sometimes however, i delete a certain record from the database, then add another, created a gap in the information. IE:
    [1][John Smith]
    [2][Another Name]
    [-][------]
    [4][Yet Another]

    Is there a way to condense this database down to look like this?
    [1][John Smith]
    [2][Another Name]
    [3][Yet Another]

    Thanks

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    If the field to which you are referring is an autoincrement field then I think there is only 1 way to 'condense' the table.

    You need to delete the field, and then recreate it, with same name and as an autoincrement.

    This will renumber the field with sequential numbers.

    You need to take care if the number sequence is important to whatever you are doing, although this is not good use of the autoincrement feature, because this will give an autoincrement number to each record in the sequence that they appear in the table.

  3. #3
    Join Date
    Oct 2008
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Im not quiet sure how to delete the field and then recreate it since it is an auto increment column.
    The basics of what im trying to do is have people post certain things on my site. If they decide to look at a 'random' one, that page generates a random number between 1 and (the number of rows) in that table, this number is the auto incremented ID. However, like i said in my previous post, when i delete a post the code stops working properly.
    If anyone has any other ideas on how i can achieve this idea, i would appreciate it, thanks.

  4. #4
    Join Date
    Oct 2008
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Resolved

    I used this solution
    Code:
    $sql = "DELETE FROM tablename WHERE id=$id";
    $sql1 = "alter table tablename drop column id";
    $sql2 = "alter table tablename add id int(11) primary key auto_increment not null";
    	
    	    $result = mysql_query($sql);
     
    	    $result1 = mysql_query($sql1);
    
        $result2 = mysql_query($sql2);

  5. #5
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    You have just done what I said, you deleted and then recreated the autoincrement field.

    I don't think it was necessary for the first line, the DELETE statement, it should be impossible to have an autoincrement field that is null

Bookmarks

Posting Permissions

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