I have a table that records tickets that are separated by a column that denotes the "database". I have a unique key on the database
and cid
columns so that it increments each database uniquely (cid
has the AUTO_INCREMENT
attribute to accomplish this). I increment id
manually since I cannot make two AUTO_INCREMENT
columns (and I'd rather the AUTO_INCREMENT
take care of the more complicated task of the uniqueness).
This makes my data look like this basically:
-----------------------------
| id | cid | database |
-----------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
-----------------------------
This works perfectly well.
I am trying to make a feature that will allow a ticket to be "moved" to another database; frequently a user may enter the ticket in the wrong database. Instead of having to close the ticket and completely create a new one (copy/pasting all the data over), I'd like to make it easier for the user of course.
I want to be able to change the database
and cid
fields uniquely without having to tamper with the id
field. I want to do an UPDATE
(or the like) since there are foreign key constraints on other tables the link to the id
field; this is why I don't simply do a REPLACE
or DELETE
then INSERT
, as I don't want it to delete all of the other table data and then have to recreate it (log entries, transactions, appointments, etc.).
How can I get the next unique AUTO_INCREMENT
value (based on the new database
value), then use that to update the desired row?
For example, in the above dataset, I want to change the first record to go to "database #2". Whatever query I make needs to make the data change to this:
-----------------------------
| id | cid | database |
-----------------------------
| 1 | 3 | 2 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
-----------------------------
I'm not sure if the AUTO_INCREMENT
needs to be incremented, as my understanding is that the unique key makes it just calculate the next appropriate value on the fly.