简体   繁体   中英

MySQL override 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 eg 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?

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

不,auto_increment列不能多次出现。

I would keep each message ID unique - whether it is via auto increment or a uuid. Add an additional column to the message structure for thread_id - unique on creation, and then have all replies include this thread_id to link them together logically.

The way you would do this is have another column in your table called parent_id or something of the sort.

The original message would have a parent_id of NULL.

Then, when anyone posts a reply to a message, then the original ID of the message goes into the parent_id column of the new message. For instance:

id    text           parent_id    created_at
============================================
1    'Lorem ipsum'     null         [time]
2    'Lorem ipsum'     1            [time]
3    'Lorem ipsum'     1            [time]

You could even go further and have the replies nested:

id    text           parent_id    created_at
============================================
1    'Lorem ipsum'     null         [time]
2    'Lorem ipsum'     1            [time]
3    'Lorem ipsum'     2            [time]

In the latter case, you'd probably need some sort of recursive function to get all of the nested messages; the first way is simpler.

Best way to put unique id is; to add uniqid(rand()).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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