Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Re-order MySQL Table
bakr_2k5
post Dec 3 2006, 07:25 PM
Post #1


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Hello you all,

I've got a question smile.gif

Let's say I have a database width the table "news". It contains about 10 items which is ordered by the field "id".
Now from my admin page i do this:
CODE
<?PHP
mysql_query("DELETE FROM news WHERE id=4");
?>

And a few days later i do:
CODE
<?PHP
mysql_query("DELETE FROM news WHERE id=7");
?>


Now there are two gaps in the table => 1, 2, 3, 5, 6, 8, 9, 10 (no 4 and 7).
It want to reallocate the whole table to fill the gaps like this => 1, 2, 3, 4, 5, 6, 7, 8.
Of course true a php script which lies in my admin environment.

But I can't figure out how to, so i thought let's ask it here smile.gif

Bakr_2k5

EDIT:
Perhaps it can be done with a temporary table or something like that, but still don't know really how. And it would be nicer if it could be done without the temporary table smile.gif
END EDIT

This post has been edited by bakr_2k5: Dec 3 2006, 07:29 PM
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Dec 4 2006, 11:18 AM
Post #2


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



Are you sure you want to do this? In case these IDs act as references (FOREIGN KEYS) to other tables - that might collapse the whole database structure and create a lot of orphaned/mis-linked records - and that's not desirable at all.

Anyways, I believe I'd come across a statement which does what you want - but can't recall it at the moment. Let me jog my memory a bit and I'll get back to you.
Go to the top of the page
 
+Quote Post
pyost
post Dec 4 2006, 11:56 AM
Post #3


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,049
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:9.92



I don't have time to write the PHP script, so I'll just try to explain what I have in mind.

First, you select all the rows in that table. Next, you start a while loop that will go through all those rows. Also, you create a new variable, let's say $i, which is 1. Now, in the loop, if the current row isn't empty, you will save it in the same table, but the row will be $i, and then you will increase $i. If, however, the row is empty, just increase $i without saving anything.

What will you accomplish this way? You will go through all the rows, and save them one after another if they are not empty. Be aware, that this would leave you with a certain number of unwanted rows. You can delete this by starting from row $i (as $i will be the row after the last one).

I hope I was clear enough smile.gif
Go to the top of the page
 
+Quote Post
bakr_2k5
post Dec 4 2006, 02:08 PM
Post #4


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(pyost @ Dec 4 2006, 11:56 AM) *

I don't have time to write the PHP script, so I'll just try to explain what I have in mind.

First, you select all the rows in that table. Next, you start a while loop that will go through all those rows. Also, you create a new variable, let's say $i, which is 1. Now, in the loop, if the current row isn't empty, you will save it in the same table, but the row will be $i, and then you will increase $i. If, however, the row is empty, just increase $i without saving anything.

What will you accomplish this way? You will go through all the rows, and save them one after another if they are not empty. Be aware, that this would leave you with a certain number of unwanted rows. You can delete this by starting from row $i (as $i will be the row after the last one).

I hope I was clear enough smile.gif

@pyost:

This isn't exactly what I had in mind. You see, the "empty" rows don't exist. They are deleted true the mysql DELETE thing. Which ends up with 1,2,3,5,6,8,9,10 (4 and 7 are deleted) so those aren't empty they just don't exist. And I have set the "id" field from the "news" table to "auto increment". BUT when I delete a row 4&7, the id's don't get updated to "recount auto_increment" so it leaves a gap between id 3&5 and 6&8. Well there isn't more to say I think. Maybe you don't exactly understand what I mean, but I'm no professional in English smile.gif! Although your explanation was very clear but just not what I'm looking for. (Hmm... It could also be that I don't understand what you mean, but then I'll hear it from you wink.gif)
Well thanks anyways!

@miCRoSCoPiC^eaRthLinG:

I hope you can dig it somewhere from your memory smile.gif

Bakr_2k5
Go to the top of the page
 
+Quote Post
jlhaslip
post Dec 4 2006, 04:17 PM
Post #5


Advanced Member
Group Icon

Group: Members
Posts: 189
Joined: 15-November 05
From: Inland from the Left Coast of Canada
Member No.: 9,627
myCENTs:62.43



The gaps in the index for this table are not critical to anything. The Index does not need to be continuous at all, what-so-ever. As mentioned by m^e, you could create more grief by messing with this than it is worth to have nice, neat indexes. Let the MySql worry about that.
Go to the top of the page
 
+Quote Post
bakr_2k5
post Dec 4 2006, 04:24 PM
Post #6


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(jlhaslip @ Dec 4 2006, 04:17 PM) *

The gaps in the index for this table are not critical to anything. The Index does not need to be continuous at all, what-so-ever. As mentioned by m^e, you could create more grief by messing with this than it is worth to have nice, neat indexes. Let the MySql worry about that.

Yes that's completely true, but it was just a thought that popped into my head! tongue.gif
And it was more like a question if it was possible. But I'd guess it IS possible but like m^e and you said it's not worth it, and could break things. So I'll quickly "remove" the thought and go on with life smile.gif

Thank you all anyways!!

Bakr_2k5
Go to the top of the page
 
+Quote Post
pyost
post Dec 4 2006, 07:15 PM
Post #7


Nenad Bozidarevic
Group Icon

Group: [MODERATOR]
Posts: 1,049
Joined: 7-November 05
From: Belgrade, Serbia
Member No.: 9,500
myCENTs:9.92



QUOTE(bakr_2k5 @ Dec 4 2006, 03:08 PM) *

This isn't exactly what I had in mind. You see, the "empty" rows don't exist.


Oh, this makes it even easier. Again, you select all the rows, and create a variable $i = 1. In a "while" loop you go through all the rows and check if id is equal to $i. If it is, you just increase $i and go to the next row. If it is not, you update the current row so that id becomes $i, increase $i and move on to the next row.

I hope this is what you were asking for smile.gif
Go to the top of the page
 
+Quote Post
bakr_2k5
post Dec 4 2006, 08:27 PM
Post #8


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(pyost @ Dec 4 2006, 09:15 PM) *

Oh, this makes it even easier. Again, you select all the rows, and create a variable $i = 1. In a "while" loop you go through all the rows and check if id is equal to $i. If it is, you just increase $i and go to the next row. If it is not, you update the current row so that id becomes $i, increase $i and move on to the next row.

I hope this is what you were asking for smile.gif

mellow.gif That's EXACTLY what I meant! Thank you! Damn, I'm not clearly minded these days. It's so simple and even can't think of this! Hmm maybe I was thinking a bit too much the difficult way! smile.gif

Anyway thanks, if I need it I know how to smile.gif ... Although those posts earlier from m^e and jlhaslip kinda discouraged me to do this. But I might be handy sometime!

Bakr_2k5
Go to the top of the page
 
+Quote Post
TavoxPeru
post Dec 6 2006, 02:34 AM
Post #9


Super Member
Group Icon

Group: [HOSTED]
Posts: 805
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579
myCENTs:46.87



QUOTE(bakr_2k5 @ Dec 4 2006, 03:27 PM) *

mellow.gif That's EXACTLY what I meant! Thank you! Damn, I'm not clearly minded these days. It's so simple and even can't think of this! Hmm maybe I was thinking a bit too much the difficult way! smile.gif

Anyway thanks, if I need it I know how to smile.gif ... Although those posts earlier from m^e and jlhaslip kinda discouraged me to do this. But I might be handy sometime!

Bakr_2k5

Yes it is so simple and efective and works perfect, but, it only works if your column id is not a foreign key in another table, but dont worry, if you have such case simply add another SQL statement that deletes the rows referenced:
CODE
<?php
mysql_query("DELETE FROM news WHERE id=$n");
mysql_query("DELETE FROM table_with_fk WHERE table_with_fk.id=$n");
// $n = id to delete
?>

Also don't forget to complete the code to update all the rows with the foreign key column in the second table when id is not equal to $i.

You can also use ALTER TABLE to reset the AUTO_INCREMENT value:
CODE
<?php
mysql_query("ALTER TABLE news AUTO_INCREMENT=$n");
// $n = row count plus 1
?>

Best regards,
Go to the top of the page
 
+Quote Post
yordan
post Dec 6 2006, 02:45 PM
Post #10


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 2,242
Joined: 16-August 05
Member No.: 7,896
myCENTs:56.55



QUOTE
mysql_query("DELETE FROM news WHERE id=$n");

I love this ! Because it's in a loop on the whole database ! So, in case of mistake, you detroyed the whole database ! biggrin.gif
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL & PHP coding(9)
  2. Displaying Data From Mysql?(2)
  3. Php/mysql Data Display(3)
  4. Need Help With A PHP - MySQL Registration Script(13)
  5. Need For PHP/MySQL Creator(1)
  6. Printing Out A Table(6)
  7. Need Some Help Using PHP & MySQL(4)
  8. [PHP + MySQL] Separating The Results By Pages(0)
  9. [PHP + MySQL] Encrypting Data(11)
  10. [php] Index.php?section=xx&pag=yy(6)
  11. How Do You Create A Secure Loging?(4)
  12. Important: Basics Of Using PHP And MySQL(10)
  13. Need Help With Php/mysql And Web Servers Such As Asta's.(4)
  14. Need MySQL Alternative To The Syntax "or die()"(8)
  15. PHP & MySQL: Displaying Content From A Given ID(6)
  1. How To Show Serial Nums In PHP Table For Contents Of MySQL DB(4)
  2. Php Mysql Errors(2)
  3. Sql Injection Prevention (passing Numerical Data Across Pages).(9)
  4. Php/mysql And Manual Page Caching?(4)
  5. Too Many Connections?(4)
  6. Extracting Mysql Maths Using Php(2)
  7. Anyone Know Of A Really Good Mysql Class?(4)
  8. Warning: Mysql_num_rows()(1)
  9. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  10. Making A Link = Mysql_query(8)
  11. Making Something In Mysql Happen Only Once(10)
  12. Mysql Question(inserting Number From A Textfield)(3)
  13. Letting Users Add Mysql Data With Php(1)


 



- Lo-Fi Version Time is now: 5th December 2008 - 10:50 AM