简体   繁体   English

MySQL数据库唯一字段(非表唯一)

[英]MySQL Database Unique Field (Not Table Unique)

I built a database and have about 8 different tables for different kinds of items. 我建立了一个数据库,并为不同种类的项目提供了大约8个不同的表。 I did this because each table is a category and each item has different fields so I couldn't make them all 1 table. 我这样做是因为每个表都是一个类别,并且每个项目都有不同的字段,所以我无法将它们全部做成1个表。 The problem I'm running into is that I need the itemID field to be unique across all tables. 我遇到的问题是我需要itemID字段在所有表中都是唯一的。 Not just unique to each table. 不只是每个表唯一。

For example, if an admin adds an item in 1 table, the itemID for that item can't be a duplicate of another item in a different table or it will cause problems for me. 例如,如果管理员在1个表中添加了一项,则该项的itemID不能与其他表中的另一项重复,否则会给我带来麻烦。 Please help me set this correct. 请帮助我将此设置正确。 Can I do this directly in the database? 我可以直接在数据库中这样做吗? Or do I need to make a php script to check if the item number already exists? 还是我需要制作一个php脚本来检查项目编号是否已经存在?

I hope I explained that right. 我希望我能解释正确。 :) :)

Create a table to hold the globally unique field, this will be the parent table. 创建一个表来保存全局唯一字段,这将是父表。

Make all other globally unique fields foreign keys pointing to the new table(field), these will be the child tables. 使所有其他全局唯一字段外键指向新表(字段),这些将成为子表。

On insert make sure to always insert the new value into the parent table before inserting into the child table (otherwise it will yield an integrity violation). 在插入时,请确保在插入子表之前始终将新值插入父表中(否则将产生完整性违规)。

On update make sure you always update the unique field only in the parent table, not in the child table. 在更新时,请确保始终仅在父表中而不在子表中更新唯一字段。

Add a further unique constraint on the foreign keys in the child tables, so that they can't point to the same parent table value. 在子表中的外键上添加进一步的唯一约束,以使它们不能指向相同的父表值。

You either generate a unique id (like a GUID) and use that as your key, (or as an alternate unique key). 您可以生成一个唯一的ID(如GUID)并将其用作密钥(或用作备用的唯一密钥)。 Or have a table called say Entities with a unique id in it, and then add a foreign key to it. 或者,在其中有一个名为“说实体”的表,其中具有唯一的ID,然后向其中添加外键。 Idea is, you insert into entities, get your id back and then use that to insert in the child tables. 想法是,将其插入实体中,取回ID,然后使用该ID插入子表中。

Bet you won't make this mistake again. 赌你不会再犯这个错误。 :( :(

The easiest way to do this would be to convert each PK to use a GUID . 最简单的方法是将每个PK转换为使用GUID There is a good articly by Jeff Atwood that discusses the relative merits of IDs vs GUIDs. Jeff Atwood巧妙地论述了ID与GUID的相对优点。 There is even a function in MySQL to generate UUIDs . MySQL中甚至有一个函数可以生成UUID Caveat, I've never used this function and I understand it did have some issues when it came to replication although I believe those have been fixed in recent versions. 请注意,我从未使用过此功能,并且我了解到复制确实存在一些问题,尽管我认为这些问题已在最新版本中修复。

The other way to achieve this would be to have a separate table to control the entities themselves and use an auto-increment to keep the IDs unique. 实现此目的的另一种方法是拥有一个单独的表来控制实体本身,并使用自动递增来使ID保持唯一。 Each of your individual tables would then reference this table. 然后,每个单独的表都将引用该表。 That's a little more complicated in terms of the application logic to keep the integrity of the data but it is possible. 就保持数据完整性的应用程序逻辑而言,这有点复杂,但有可能。

Or you could have master table as per the second option and one other table containing key/value pairs for the parameters that vary between the different categories. 或者,您可以按照第二个选项使用主表,而另一个表则包含在不同类别之间变化的参数的键/值对。 This might be a good option if the extra parameters are all secondary to the main entity info (which doesn't vary between different categories). 如果额外的参数都在主要实体信息之后(这在不同类别之间没有变化),则这可能是一个不错的选择。

Which option is best for you really depends on the structure and semantics of your data model and your use case (ie in what ways you are most often accessing the data). 哪种选择最适合您,实际上取决于数据模型的结构和语义以及用例(即,您最常访问数据的方式)。

There is more info about mapping this sort of inheritance structure to a relational database here . 这里有更多有关将这种继承结构映射到关系数据库的信息 Of course if you're willing to look outside of MySQL then there are various NoSQL databases that would suit much better this sort of scenario. 当然,如果您愿意在MySQL之外浏览,那么会有各种NoSQL数据库适合这种情况。

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

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