简体   繁体   中英

MySQL update IDs on update, insert and delete

In my current application I am making a menu structure that can recursively create sub menu's with itself. However due to this I am finding it difficult to also allow some sort of reordering method. Most applications might just order by an "ordering" column, however in this case although doing that does not seem impossible, just a little harder.

What I want to do is use the ID column. So if I update id 10 to be id 1 then id 1 that was there previously becomes 2.

What I was thinking at a suggestion from a friend was to use cascades. However doing a little more research that does not seem to work as I was thinking it might.

So my question is, is there an ability to do this naively in MySQL? If so what way might I do that? And if not what would you suggest to come to the end result?

Columns:

id title alias icon parent

parents have a lower id then their children, to make sure the script creates the array to put the children inside. That part works, however If I want to use an ordering column I will have to make a numbering system that would ensure a child element is never higher then its parent in the results. Possible, but if I update a parent then I must uniquely update all its children as well, resulting in more MySQL queries that I would want.

I am no MySQL expert so this is why I brought up this question, I feel there might be a perfect solution to this that can allow the least overhead when it comes to the speed of the application.

Doing it on the ID column would be tough because you can't ever have 2 rows with the same ID so you can't set row 10 to row 1 until after you've set row 1 to row 2 but you can't set row 1 to row 2 until you set row 2 to row 3, etc. You'd have to delete row 10 and then do an update ID += 1 WHERE ID < 10... but you'd also have to tell MySQL to start from the highest number and go down....

You'd have to do it in separate queries like this:

Move ID 10 to ID 2

DELETE FROM table WHERE id = 10;

UPDATE table SET id = id + 1 WHERE id >= 2 AND id < 10 ORDER BY id DESC

INSERT INTO table (id, ...) VALUES (2, ...);

Another option, if you don't want to delete and reinsert would be to set the id for row 10 to be MAX(id) + 1 and then set it to 1 after

Also if you want to move row 2 to row 10 you'd have to subtract the id:

Move ID 2 to ID 10

DELETE FROM table WHERE id = 2;

UPDATE table SET id = id - 1 WHERE id > 2 AND id <= 10 ORDER BY id DESC

INSERT INTO table (id, ...) VALUES (10, ...);

If you don't have your ID column set as UNSIGNED you could make all the IDs you want to switch to negative ids since AUTO_INCREMENT doesn't do negative numbers. Still this is pretty hacky and I wouldn't recommend it. You also probably need to lock the table so no other writes happen while this is running.:

Move ID 2 to ID 10

UPDATE table SET id = id * -1 WHERE id > 2 AND id <= 10;

UPDATE table SET id = 10 WHERE id = 2;

UPDATE table SET id = id * -1 - 1 WHERE id < -2 AND id >= -10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM