简体   繁体   English

更新,插入和删除MySQL更新ID

[英]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. 我想做的是使用ID列。 So if I update id 10 to be id 1 then id 1 that was there previously becomes 2. 因此,如果我将ID 10更新为ID 1,则以前在那里的ID 1变为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? 所以我的问题是,在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. 父母的ID比孩子的ID低,以确保脚本创建了将孩子放在里面的数组。 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. 可能的,但是如果我更新一个父级,那么我也必须唯一地更新其所有子级,从而导致我想要更多的MySQL查询。

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. 我不是MySQL专家,所以这就是为什么我提出这个问题,我觉得可能有一个完美的解决方案,它可以在应用程序速度上带来最小的开销。

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.... 在ID列上执行此操作将很困难,因为您永远不会有2个具有相同ID的行,因此,在将第1行设置为第2行之前,您无法将第10行设置为第1行,但无法设置从第1行到第2行,直到将第2行设置为第3行,依此类推。您必须删除第10行,然后执行更新ID + = 1 WHERE ID <10 ...,但是您还必须告诉MySQL从最高的数字开始然后下降....

You'd have to do it in separate queries like this: 您必须在单独的查询中执行以下操作:

Move ID 10 to ID 2 将ID 10移至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 如果您不想删除并重新插入,另一种选择是将第10行的id设置为MAX(id)+ 1,然后在之后将其设置为1。

Also if you want to move row 2 to row 10 you'd have to subtract the id: 另外,如果要将第2行移至第10行,则必须减去ID:

Move ID 2 to ID 10 将ID 2移至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. 如果未将ID列设置为UNSIGNED ,则可以将所有要切换为负ID的ID设置为负ID,因为AUTO_INCREMENT不会做负数。 Still this is pretty hacky and I wouldn't recommend it. 仍然这很hacky,我不推荐它。 You also probably need to lock the table so no other writes happen while this is running.: 您可能还需要锁定表,以便在该表运行时不会发生其他写操作。

Move ID 2 to ID 10 将ID 2移至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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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