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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示