phpMyadmin中重新设置表自动增量的触发器

发布于 2024-10-06 09:08:25 字数 919 浏览 1 评论 0原文

我有一个数据库,tvguide,这些是值:

id bigint 255 UNSIGNED AUTO_INCREMENT
programme varchar 255 
default value (which is from another table
with list of programs, the only linked field that
is shared with other table):<b><a href="gossipgirl.php">Gossip Girl</a></b>
channel varchar 255
airdate DATETIME - actual airtime
displayair DATETIME (rounded-up datetime, e.g. programme that airs 1.59pm 
would be shown as 2.00pm, or 6.08pm as 6.10pm)
expiration DATETIME - when program finishes
episode VARCHAR 255 NULL - has null option if episode unknown
series VARCHAR 255
epno VARCHAR 255 - episode number
setreminder VARCHAR255 (shows "set reminder", sends email to user, but that's 
another thing to work on).

我想在我的数据库中插入一个触发器,以便如果删除一条记录,ID 自动增量会自动重新设置?

关于如何实现这一目标有什么想法吗?到目前为止,我已经手动完成了此操作,但我正在考虑删除记录时可以使用 phpMyadmin 做什么。

顺便说一句,我制作了该表的副本以用于备份目的。

谢谢

I have a database, tvguide and these are the values:

id bigint 255 UNSIGNED AUTO_INCREMENT
programme varchar 255 
default value (which is from another table
with list of programs, the only linked field that
is shared with other table):<b><a href="gossipgirl.php">Gossip Girl</a></b>
channel varchar 255
airdate DATETIME - actual airtime
displayair DATETIME (rounded-up datetime, e.g. programme that airs 1.59pm 
would be shown as 2.00pm, or 6.08pm as 6.10pm)
expiration DATETIME - when program finishes
episode VARCHAR 255 NULL - has null option if episode unknown
series VARCHAR 255
epno VARCHAR 255 - episode number
setreminder VARCHAR255 (shows "set reminder", sends email to user, but that's 
another thing to work on).

I want to insert a trigger into my database so that if a record is deleted, the ID auto-increment re-sets itself automatically?

Any ideas on how to accomplish this? I've done it manually so far but am looking at seeing what I can do with phpMyadmin when I delete the record.

BTW I made a copy of the table for backup purposes.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-10-13 09:08:25

我同意whitstone86的评论,你可以“让它成长”。

话虽如此,要随时更改 AUTO_INCRMENT,您只需运行 ALTER TABLE 即可。

一些伪代码...

SELECT MAX(id)+1 AS max_id_plus_one FROM my_table;

ALTER TABLE my_table AUTO_INCREMENT = $max_id_plus_one;

对于生产,竞争条件可能是一个问题。然而,如果只是你在 phpmyadmin 中闲逛,这应该足够了。

I agree with whitstone86's comment that you can just "let it grow".

Having said that, to change the AUTO_INCREMENT at any time, you can just run an ALTER TABLE.

Some pseudo code...

SELECT MAX(id)+1 AS max_id_plus_one FROM my_table;

ALTER TABLE my_table AUTO_INCREMENT = $max_id_plus_one;

For production, race conditions might be an issue. However, if it's just you puttering around in phpmyadmin, this should suffice.

握住我的手 2024-10-13 09:08:25

这是一个相当危险的想法。

让它成长,它不会发生:250000000011 在你的程序中使用与 250000000012 相同的位置
表,特别是因为它是一个 bigint (8 字节)。

否则

  • 你怎么知道 6 个月前的 Id=25 是今天的 Id=25 ?
  • 你如何保持完整性约束?您必须确保在 update|delete 级联中到处都有外键
  • ,如果您不总是删除表中的最后一条记录,则自下一个添加的记录 Id 起,增益为 0,至少是先前添加的 +1一。

This is a rather dangerous idea.

Let it grow, it doesn't take place: 250000000011 use the same place as 250000000012 in your
table, especially since it's a bigint (8 bytes).

Otherwise

  • how do you know than Id=25 from 6 months ago is today Id=25 ?
  • how do you maintain integrity constraints ? you must make sure than you have foreign keys everywhere with on update|delete cascade
  • if you don't always delete the last record in the table, there is 0 gain since the next added record Id with at least be +1 of the previously added one.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文