MySQL using a unique CHAR as primary key - mysql

I'm fairly new to SQL programming, and I have a table that has two columns, UUID (a 32 CHAR unique hexstring), and permission
(just a permission for the system I'm using, like 'admin' or 'blacklist').
I'm not the greatest on the concept of the primary key, so I was wondering if its valid to use UUID as my primary key, considering it is 100% unique without whitespace, which I am already using to easily index my queries. I'm not joining the table with anything and more than likely never will, so is it valid to use the UUID as my primary key without any unforeseen consequences?

It is valid to use a UUID as a primary key. It meets the two conditions required of a primary key:
It is unique.
It is never NULL.
However, it is a bad idea. Why? MySQL automatically clusters the data by the primary key. That is, the data is actually sorted by the primary key. UUIDs are not sequential. Inserts can occur anywhere, requiring movement of data.
I would recommend a simple auto incremented primary key and declare the UUID as unique:

Related

PrimaryKey need to be auto increment always?

I am working on banking application, during the database design I have heard that primary key should be auto increment, but it is easy to use account number as primary key for the table account, which is the mixture and string and digit. So please mention is there any problem about using account_number as primary key.
A primary key is by no means required to use the auto_increment property - it just needs to be a unique, not-null, identifier, so the account number would do just fine.
There are, however, two additional considerations worth keeping in mind:
Numbers generally take up less space than strings, which usually makes their indexes smaller, and thus faster. If you have a complicated schema and use a lot of joins on the primary key in your queries, this difference may be noticeable.
Some DBAs advocate the practice of having primary (and other) keys that are separate from your data. That way, if you one day change your account identifiers (e.g., the bank acquires another bank and has to incorporate its clients into its system), you'll just have to update some data, and not all your keys.
There's no problem with it, and you don't need to auto_increment (but its handy). A Primary Key just needs to be a unique value that identifies its entry from other entries, and not null.
Primary key should be unique but not necessarily need to be auto_increment. As long is account_number is unique, there shouldn't be any problem.
Well, there is no need that the primary key is set to auto increment if we know the primary key is always unique. We set the primary key to auto increment because it is easy to handle. As you are making a bank software, the bank account number is always unique for every user. Therefore you can use that account number as a primary key. That's not a problem.
As long as the account number is unique, immutable and never NULL, it's not a problem to use it as a primary key.
If you aren't sure about the immutability it's probably not a good idea, things change and updating a primary key is generally not the best idea due to the fact that you also need to update all foreign key referencing it.
If you're not sure, a surrogate primary key along with a UNIQUE index on the account number is probably better.

MySQL Tables with Temp Data - Include a Primary Key?

I'm putting together a new database and I have a few tables that contain temp data.
e.g.: user requests to change password - a token is stored and then later removed.
Currently I have a primary key on these tables that will auto-increment from 1 upwards.
AUTO_INCREMENT = 1;
I don't really see any use for this primary key... I will never reference it and it will just get larger.
Should tables like this have a primary key or not?
Short answer: yes.
Long answer:
You need your table to be joinable on something If you want your table
to be clustered, you need some kind of a primary key. If your table
design does not need a primary key, rethink your design: most
probably, you are missing something. Why keep identical records? In
MySQL, the InnoDB storage engine always creates a PRIMARY KEY if you
didn't specify it explicitly, thus making an extra column you don't
have access to.
Note that a PRIMARY KEY can be composite.
If you have a many-to-many link table, you create the PRIMARY KEY on
all fields involved in the link. Thus you ensure that you don't have
two or more records describing one link.
Besides the logical consistency issues, most RDBMS engines will
benefit from including these fields in an UNIQUE index.
And since any PRIMARY KEY involves creating a UNIQUE index, you should
declare it and get both logical consistency and performance.
Here is a SO thread already have same discussion.
Some people still loves to go with your opinion. Have a look here
My personal opinion is that you should have primary keys, to identify or to make a row unique. The logic can be your program logic. Can be an auto-increment or composite or whatever it can be.

Must database primary keys be integers?

I always see MySQL database primary keys as integers. Is that because primary keys must be integers, or because of ease of use when setting auto_increment on the column?
I am wondering just in case I want my primary key to be a varchar in the future.
You can use varchar as well as long as you make sure that each one is unique. This however isn't ideal (see article link below for more info).
What you are looking for is called natural key but a primary key with auto-increment and handled by the RDBMS is called surrogate key which is preferred way. Therefore you need to have it to be integer.
Learn more:
Surrogate Keys vs Natural Keys for Primary Key?
Why I prefer surrogate keys instead of natural keys in database design
Why Integers Make Good Primary Keys
It's often easier to use an integer for indexing, in comparison to a string or composite key, because it lends itself well to treating results (conceptually or in practice) as an array. Depending on the database implementation, integers may also be faster to access, sort, or compare, and the integer type usually offers additional features like auto-incrementing that aren't available for other data types. How would you go about auto-incrementing a composite key, for example?
MySQL has this to say about the primary key:
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.
SQL allows any non-null, unique column (or set of columns) to be used as a primary key. However, if you don't care about auto-incrementing, you can usually make your primary key any index that is UNIQUE and NOT NULL.
Consider Your Application's Expectations
While not a hard requirement, some frameworks optimize for integer primary keys. For example, Ruby on Rails facilitates the use of an auto-incrementing primary key by default; you have to deliberately work against the convention if you want to use something different.
That doesn't mean one should or shouldn't use integers as primary keys. It just means the choice of primary key is driven in part by your underlying database system and the queries you expect to run against it, and in part by the applications you expect to use to retrieve the data. All of those things should be taken into consideration when considering candidate keys.
A primary key is unique.int is easy to satisfy that condition with auto increment.If you make it char you have to create a way to make it unique whenever you add a data.
No, the primary key does not have to be an integer; it's just very common that it is. As an example, we have User ID's here that can have leading zeroes and so must be stored in a varchar field. That field is used as a primary key in our Employee table.
For a primary key to be an Integer, is easier to manage, and makes its index more effective.
As you know, while the keys are auto indexed, the indexes are stored as Binary tree which is best for integers in traversing.
There is no restriction on making a key to be int, you can declare it a varchar too.
Basicly a primary key needs to fulfill only 2 conditions: it has to be a not null column and it has to be unique. Any typeof column that respects this 2 conditions can be set as primary keys. In case the primary key is a multiple column one, then both columns need to be not null.
While in theory you can use other fields as primary keys, integers are the easiest to manage, as well as being the fastest indexes available.

When we don't need a primary key for our table?

Will it ever happen that we design a table that doesn't need a primary key?
No.
The primary key does a lot of stuff behind-the-scenes, even if your application never uses it.
For example: clustering improves efficiency (because heap tables are a mess).
Not to mention, if ANYONE ever has to do something on your table that requires pulling a specific row and you don't have a primary key, you are the bad guy.
Yes.
If you have a table that will always be fetched completely, and is being referred-to by zero other tables, such as some kind of standalone settings or configuration table, then there is no point having a primary key, and the argument could be made by some that adding a PK in this situation would be a deception of the normal use of such a table.
It is rare, and probably when it is most often done it is done wrongly, but they do exist, and such instances can be valid.
Depends.
What is primary key / unique key?
In relational database design, a unique key can uniquely identify each row in a table, and is closely related to the Superkey concept. A unique key comprises a single column or a set of columns. No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. Depending on its design, a table may have arbitrarily many unique keys but at most one primary key.
So, when you don't have to differentiate (uniquely identify) each row,
you don't have to use primary key
For example, a big table for logs,
without using primary key, you can have fairly smaller size of data and faster for insertion
Primary key not mandatory but it is not a good practice to create tables without primary key. DBMS creates auto-index on PK, but you can make a column unique and index it, e.g. user_name column in users table are usually made unique and indexed, so you may choose to skip PK here. But it is still a bad idea because PK can be used as foreign key for referential integrity.
In general, you should almost always have PK in a table unless you have very strong reason to justify not having a PK.
Link tables (in many to many relationship) may not have a primary key. But, I personally like to have PK in those tables as well.

Add Primary Key to a table with existing clustered index

I have to work with a database to do reporting
The DB is quite big : 416 055 104 rows
Each row is very light though, just booleans and int ids.
Each row is identify by 3 columns, but at my surprise, there is no Primary Key on it.
Only a Clustered Index with a unique constraint.
So Knowing that, I have 2 question.
Could there be ANY good reason for that?
Is there any way I can turn this into a primary key.
Regarding question 2
Creating a new primary key also creates a non-clustered index to associate with (there is already an existing clustered one).
This is not what I am looking for. I want to keep that same index, but also make it a primary key.
Is it possible?
Would that be faster that creating the whole index again? (I hope so)
What could be the consequences? (locks? crash? corrupted data?)
There is little or no difference between a PRIMARY KEY and a UNIQUE constraint on non-nullable columns. So if the columns in question are non-nullable then I suggest you do nothing. The main reason to make a candidate key into a primary key is if you have some software (such as a data modelling tool or other development tool) that expects the key to be identified with a PRIMARY KEY constraint.
Good question.
If you already have a unique index on non nullable columns then you have a candidate key. I'm not aware of any particular benefit of making this an "official" primary key. In fact I have a feeling that not making it a PK will give greater flexibility.
A unique index can allow null
values. A primary key can't.
I believe you can't "mark" an existing index as the primary key. You'd have to drop it and recreate. To avoid stuff, I'd say it'd be good to place a TABLOCKX, HOLDLOCK on the table before doing that.