I need to do some maintenance and changes to my database. I do not want to write PHP code to do this, I would much rather just plug in some code into the SQL execution window in phpMyAdmin.
For example I would like to set the ID value of all rows in a table +1. But since they are unique if I try to say UPDATE table SET column_ID = column_ID +1
it will yell at me, saying that it cant do duplicate IDs - of course not because it will start at ID 1, set it to 2, but ID 2 already exists.
So I would like to go ahead and do something like this: start at ID 100, and then work my way down to 1.
DECLARE @id_to_update = 100
WHILE @id_to_update >= 1
BEGIN
UPDATE table SET column_ID = column_ID +1 WHERE column_ID = @id_to_update
@id_to_update = @id_to_update -1
END
But this must not be the correct syntax...
So here are my questions:
- How can I make the query above work?
- What should I know about basic mySQL syntax for things OTHER than simply selecting, inserting, or updating rows? I mean creating variables, running loops, doing other programming procedures...
- Do you have any good resources (tutorials) you can point me to? and I mean other than the mySQL manual, because I am not smart enough to understand any of whats written in there...
EDIT: Why do I want to do this? Well my database system is still sort of in a development stage, and while I am adding features and changing things around - mostly because my application is growing, but also because I am getting better ideas how to organize my stuff, I may need to make changes.
In particular on this one I am updating a database which sets definitions, that I am accessing throughout my app. I am adding a value, the IDs have been numbered with auto_increment, but now the value I am adding should logically be on top, having an ID of 1.
So think of it as if I am referencing colors. and right now I have red, yellow, green. Buy now I really need blue, and blue is actually the most important color, so I need it sorted to be on top.
I did never specify any foreign keys, I only pull the data from my DB using JOIN statements. I don't think my tables talk to each other outside of PHP...
Another example why I might need techniques like that are for general DB administration and maintenance. Also strongly determined by the fact that my app is growing, and while I am BETA testing it, I may have to change stuff around, have to do some bulk edits to the data, which I would much rather do with an atomized function (FOR, WHILE, etc.) then to click into each field and change it manually.
Hope this makes more sense now.
Aside from that: Hey, even if it brakes my stuff, I just wanna know how to get it to work. How to run code in mySQL. Again: I could simply write this up in PHP, switching back and forth between PHP code and DB queries, but since SQL seems to offer programming commands, why not use those and save the hassle of uploading and debugging PHP scripts, and simply punch in the code directly into phpMyAdmin... Think of a different example, if you like! :D