Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databases - String as Primary key? [duplicate]

I develop an application with Laravel 5 and use sqlite during development. I want to switch to something faster later on. I'd like to have a string as unique identifier for the users-table. Is this a problem in any way? For example with foreign keys? Or is there no difference to auto-increment integers?

like image 674
norbertVC Avatar asked Apr 16 '15 21:04

norbertVC


People also ask

Is it OK to use string as primary key?

Yes, from a performance standpoint (i.e. inserting or querying or updating) using Strings for primary keys are slower than integers. But if it makes sense to use string for the primary key then you should probably use it.

Can primary key be a duplicate?

You can define keys which allow duplicate values. However, do not allow duplicates on primary keys as the value of a record's primary key must be unique. When you use duplicate keys, be aware that there is a limit on the number of times you can specify the same value for an individual key.

Can two databases have the same primary key?

Every table can have (but does not have to have) a primary key. The column or columns defined as the primary key ensure uniqueness in the table; no two rows can have the same key. The primary key of one table may also help to identify records in other tables, and be part of the second table's primary key.

Can primary key have duplicate values in mysql?

Since both primary key and unique columns do not accept duplicate values, they can be used for uniquely identifying a record in the table. This means that, for each value in the primary or unique key column, only one record will be returned.


2 Answers

The short answer : It's perfectly fine to use a string as a primary key.

The long answer : We are terrible at choosing strings to be primary keys.

What is a good primary key candidate ?

  1. It should be unique.
  2. It should rarely, if at all, change.

Now, You are probably thinking that your string would never change, and that it's extremely unique, Until it will stop being unique.

Another (minor) concern, is performance. Searching, Joining etc. is a bit faster on integers than on strings, mostly due to the length(numbers are normaly shorter than strings, so comparing is easier).

I would think long and hard on what string to use on the primary key, Most of the times it's a bad idea

like image 122
Patrick Avatar answered Sep 23 '22 13:09

Patrick


The indexing of string is a lot different than a incremental int but you can have it as a primary key without problems.

Collision detection when inserting is an issue that you'll have to handle yourself and wouldn't even exist on an auto increment situaiton.

From the foreign key stand point there wouldn't be any problem also, the only problem that you would face is that a char/varchar would be less efficient than an integer.

like image 22
FabioCosta Avatar answered Sep 20 '22 13:09

FabioCosta