dtc99987 2011-11-10 12:00
浏览 77
已采纳

MySQL覆盖auto_increment?

I'm creating a messaging system (using PHP) and want to assign an ID number to each message (aside from each actual message having an unique ID number)...however, if someone replies to a message then i want to be able to give that message the same ID as the message being replied to...then of course I can disply them by time and show them in order.

So, if i give the field an auto_increment type is that able to be overwritten?

Meaning...each new message has auto value e.g. 1, 2, 3 etc but someone replies to number 2 so it's ID needs to also 2

Or is there a better way to do this?

  • 写回答

5条回答 默认 最新

  • doushiposong30622 2011-11-10 12:07
    关注

    Absolutely nothing prevents you from assigning any arbitrary value to an AUTO_INCREMENT column. If necessary, the table counter will adjust accordingly.

    However, you cannot set as AUTO_INCREMENT a column that's not unique.

    Honestly, I can't understand your design. A typical messaging system would look like this:

    message_id in_reply_to
    ========== ===========
             1        NULL
             2        NULL
             3           1
             4        NULL
             5           1
             6           3
             7        NULL
    

    Duplicating IDs kind of beats the purpose of using IDs.

    Update #1: OMG, it seems that it can actually be done under certain circumstances:

    For MyISAM tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

    http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html

    Update #2: For the records, I've just tested it and you can use duplicate auto-incremented IDs in InnoDB tables as well:

    CREATE TABLE foo (
        foo_id INT(10) NOT NULL DEFAULT '0',
        bar_id INT(10) NOT NULL AUTO_INCREMENT,
        PRIMARY KEY (foo_id),
        INDEX bar_id (bar_id)
    )
    ENGINE=InnoDB
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了