Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

guid as primary key?

I have a mysql database which has 3 tables that have to be joined together. I receive smaller databases that must feed this mysql database, appending the new data as I get it. The problem I have is the smaller dbs that I get are generated by an outside application and are not really meant to be used all together. Therefore, when I utilize the schema of the smaller database, I have no way to know how they all the records from the 3 tables belong together.

I was thinking about inserting a guid to serve as a primary key that I can add to the tables and insert when I insert all of the new data.
However, I am leery of using a char field (used to store the guid) as a key. Is this a valid concern, or is using char field knowing that it will always be a guid a sufficient solution? Can someone recommend a better approach?

Thanks

like image 899
czuroski Avatar asked Oct 29 '10 14:10

czuroski


People also ask

Can a GUID be a primary key?

GUIDs can be considered as global primary keys. Local primary keys are used to uniquely identify records within a table. On the other hand, GUIDs can be used to uniquely identify records across tables, databases, and servers.

Should I use int or GUID as primary key?

int is smaller, faster, easy to remember, keeps a chronological sequence. And as for Guid, the only advantage I found is that it is unique. In which case using sql server guid would be better than and int and why? From what I've seen, int has no flaws except by the number limit, which in many cases are irrelevant.

Is it OK to use UUID as primary key?

For database like MySQL, Oracle, which uses clustered primary key, version 4 randomly generated UUID will hurt insertion performance if used as the primary key. This is because it requires reordering the rows to place the newly inserted row at the right position inside the clustered index.

Should I use GUID as ID?

The guid can be used as needed to globally uniquely identify the row and id can be used for queries, sorting and human identification of the row. The id identifies the row in this table. The GUID (at least in theory) identifies this row anywhere in the known universe.


2 Answers

MySQL does not provide a GUID/UUID type, so you would need to generate the key in the code you're using to insert rows into the DB. A char(32) or char(36) (if including hyphens) is the best data type to store a GUID in lieu of an actual GUID data type.

like image 124
Ryan Tenney Avatar answered Sep 30 '22 07:09

Ryan Tenney


I'm sorry I'm not 100% familiar with MYSQL, in SQL Express, there is a Unique Identifier type you can set a column to which is really a GUID. You can even set it to auto-number itself so it picks random ones.

My boss at work HATES GUIDS though, and we work with offline/online systems a lot, so he came up with another system in which each feeding database is assigned an ID (called DEPT), and whenever he inserts into the master table from one of the smaller ones, he writes its DEPT into a seperate Integer column so its easilly sortable.


To implement this, you'd make a second key (making each table the import has to be performed on a dual-key table).

Example:

PrimaryKey1    DEPT    Name
1              0       Slink
2              0       Fink
3              0       Werd
1              1       Slammer
2              1       Blam
1              2       Werrr
2              2       Soda
like image 44
Jrud Avatar answered Sep 30 '22 08:09

Jrud