简体   繁体   中英

MySQL Database Unique Field (Not Table Unique)

I built a database and have about 8 different tables for different kinds of items. I did this because each table is a category and each item has different fields so I couldn't make them all 1 table. The problem I'm running into is that I need the itemID field to be unique across all tables. 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. 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?

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). Or have a table called say Entities with a unique id in it, and then add a foreign key to it. Idea is, you insert into entities, get your id back and then use that to insert in the child tables.

Bet you won't make this mistake again. :(

The easiest way to do this would be to convert each PK to use a GUID . There is a good articly by Jeff Atwood that discusses the relative merits of IDs vs GUIDs. There is even a function in MySQL to generate UUIDs . 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. 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.

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