简体   繁体   中英

MySQL: How to create a unique identifier based on another column

I can't figure this out. How would I be able to create a table in MySQL where I can organize it by two unique columns but one of the columns is only unique based on the other column For example

Rep | ID

A --|-- 1

A --|-- 2

A --|-- 3

B --|-- 1

B --|-- 2

C --|-- 1

C --|-- 2

C --|-- 3

C --|-- 4

And so on...

To explain what I'm doing: My table will hold images. These images will all correlate to their report. But there will be many images in every report. These images need to be uniquely identified 1-9 (If there was 9 images in that report).

So as shown in my example: the ID has to auto_increment and has to be unique; based upon the report it's associated with.

Thanks for any help.

************Found the answer here*********** mysql two column primary key with auto-increment

Try this:

You can create UNIQUE INDEX on both column.

ALTER TABLE `tablea` ADD UNIQUE INDEX `UniqueIndexName` (`Rep`, `Id`)

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