简体   繁体   English

删除表中的行时如何更新autoincremented id?

[英]How to update autoincremented id when delete row in table?

I am creating application that uses MYSQL database in C#. 我正在创建在C#中使用MYSQL数据库的应用程序。 I want to delete row and update autoincremented value of id in table. 我想在表中删除行并更新id的自动增量值。 For example, I have table with two columns: id and station, and table is station list. 例如,我有两列的表:id和station,table是station list。 Something like this 像这样的东西

id station id站
1 pt1 1 pt1
2 pt2 2 pt2
3 pt3 3 pt3

If i delete second row, after deleting the table looks something like this: 如果我删除第二行,删除表后看起来像这样:

id station id站
1 pt1 1 pt1
3 pt3 3 pt3

Is there any way that I update id of table, for this example that id in third row instead value 3 have value 2? 有没有什么方法可以更新表的id,对于这个例子,第三行中的id而不是值3的值是否为2? Thanks in advance! 提前致谢!

I suggest you don't do anything special when a row is deleted. 我建议您在删除行时不要做任何特殊操作。 Yes you will have gaps in the ids, but why do you care? 是的,你会在ids中留下空白,但为什么要关心? It is just an id. 这只是一个身份证。

If you change the value of id_station, you would also need to update the value in all tables that have an id_station field. 如果更改id_station的值,则还需要更新具有id_station字段的所有表中的值。 It causes more unnecessary UPDATES. 它会导致更多不必要的更新。

An autoincrement column, by definition, should not be changed manually. 根据定义,不应手动更改自动增量列。
What happen if some other tables use this ID (3) as foreign key to refer to that record in this table? 如果其他一些表使用此ID(3)作为外键来引用此表中的记录,会发生什么? That table should be changed accordingly. 该表应相应更改。
(Think about it, in your example is simple, but what happen if you delete ID = 2 in a table where the max(ID) is 100000? How many updates in the main table and in the referring tables?) (想想看,在你的例子中很简单,但如果在max(ID)为100000的表中删除ID = 2会发生什么?主表和引用表中有多少次更新?)

And in the end there is no real problem if you have gaps in your numbering. 如果你的编号有差距,最后也没有问题。

The only way to change the value of the id column in other rows is with an UPDATE statement. 更改其他行中id列的值的唯一方法是使用UPDATE语句。 There is no builtin mechanism to accomplish what you want. 没有内置机制来完成你想要的东西。

I concur with the other answers here; 我同意这里的其他答案; normally, we do not change the value of an id column in other rows when a row is deleted. 通常,删除行时,我们不会更改其他行中的id列的值。 Normally, that id column is a primary key, and ideally, that primary key value is immutable (it is assigned once and it doesn't change.) If it does change, then any references to it will also need to change. 通常,该id列是主键,理想情况下,该主键值是不可变的(它被分配一次并且不会更改。)如果它确实发生了更改,那么对它的任何引用也需要更改。 (The ON UPDATE CASCADE for a foreign key will propagate the change to a child table, for storage engines like InnoDB that support foreign keys, but not with MyISAM. (对于外键,ON UPDATE CASCADE会将更改传播到子表,对于支持外键的InnoDB等存储引擎,但不支持MyISAM。

Basically, changing an id value causes way more problems than it solves. 基本上,更改id值会导致比解决的问题更多的问题。

There is no "automatic" mechanism that changes the value of a column in other rows when a row is deleted. 删除行时,没有“自动”机制可以更改其他行中列的值。

With that said, there are times in the development cycle where I have had "static" data, and I wanted control over the id values, and I have made changes to id values. 话虽如此,在开发周期中有些时候我有“静态”数据,我想控制id值,并且我已经对id值进行了更改。 But this is an administrative exercise, not a function performed by an application. 但这是一个管理练习,而不是应用程序执行的功能。

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

相关问题 如何使用 SQLiteCommand object 获取自动递增的 ID 插入行 - How to get the autoincremented ID inserted row using SQLiteCommand object ON DELETE NO ACTION时如何在EF Core中删除表中的行? - How to delete row in table in EF Core when ON DELETE NO ACTION? Asp.net 核心 -> 如何使用 pKey(invoice_id, row_id) 从表中删除行 - Asp.net core -> How to Delete Row from table with pKey(invoice_id, row_id) 如何获取最后插入的ID以更新表中的SAME行? - How to get last inserted ID to update the SAME row in a table? 尝试根据表行的最高Id更新特定SQL Server表列时出错 - Error when trying to update specific SQL Server table column based on the highest Id of the table row 自动增加外键并在我不想在的表中添加新行 - Autoincremented foreignkey and added new row in a table where I don't want to 如何从数据表和数据库中按ID删除行 - How to Delete Row By ID From Datatable and Database 如何在多用户环境中更新/删除行 - How to update/delete row in multi user environment 当具有多个具有相同ID的单元格时,如何仅删除所选行? - How can I delete only the selected row, when having several cells with the same ID? 一次为多个订单创建自动递增的订单 ID - Creating autoincremented order id For multiple orders at a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM