简体   繁体   English

MySQL:如何基于另一列创建唯一标识符

[英]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 我将如何在MySQL中创建一个表,在该表中我可以按两个唯一列来组织表,但是其中一个列仅基于另一列是唯一的。例如

Rep | 代表| ID ID

A --|-- 1 A-|-1

A --|-- 2 A-|-2

A --|-- 3 A-|-3

B --|-- 1 B-|-1

B --|-- 2 B-|-2

C --|-- 1 C-|-1

C --|-- 2 C-|-2

C --|-- 3 C-|-3

C --|-- 4 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). 这些图像需要唯一标识1-9(如果该报告中有9张图像)。

So as shown in my example: the ID has to auto_increment and has to be unique; 因此,如我的示例所示:ID必须为auto_increment,并且必须是唯一的; based upon the report it's associated with. 根据与之关联的报告。

Thanks for any help. 谢谢你的帮助。

************Found the answer here*********** mysql two column primary key with auto-increment ************在这里找到答案*********** 带有自动增量的mysql两列主键

Try this: 尝试这个:

You can create UNIQUE INDEX on both column. 您可以在两列上创建UNIQUE INDEX

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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