Is it more beneficial basically use ID nr:s rather than VARCHARS as foreign secrets? And is it more beneficial to make use of ID nr:s isntead of VARCHARS as Primary Secrets? By ID nr I am talking about INT!

This is exactly what I've now:

category table:
cat_id ( INT ) (PK)
cat_name (VARCHAR)

category options table:
option_id ( INT ) (PK)
car_id ( INT ) (FK)
option_name ( VARCHAR ) 

I Possibly Could HAVE I THINK:

category table:
cat_name (VARCHAR) (PK)

category options table:
cat_name ( VARCHAR ) (FK)
option_name ( VARCHAR ) ( PK )

Or am I thinking drastically wrong here?

The issue with VARCHAR getting used for just about any KEY is they holds Whitened SPACE. Whitened space includes ANY non-screen-readable character, like spaces tabs, carriage returns etc. Utilizing a VARCHAR like a key could make your existence difficult when you begin to search lower why tables aren't coming back records with extra spaces in the finish of the secrets.

Sure, you CAN use VARCHAR, but you have to be cautious using the input and output. Additionally they occupy more space and therefore are likely reduced when you are performing a Queries.

Integer types possess a small listing of 10 figures which are valid, ,1,2,3,4,5,6,7,8,9. They're a far greater solution for secrets.

You can always make use of an integer-based key and employ VARCHAR like a UNIQUE value should you wanted to achieve the benefits of faster searches.

by having an int you are able to store as much as 2 billion in 4 bytes with varchars you can't you must have 10 bytes approximately to keep that, if you are using varchars there's additionally a 2 byte overhead

now you accumulate the 6 extra bytes in each and every PK and FK + the two byte varchar overhead

When I am doing design work I request myself: is it necessary anything within this data will be able to guarantee will probably be non-NULL, unique, and constant? If that's the case this is a candidate to become the main key. Otherwise, I understand I must produce a key value to make use of. Presuming, then, that my candidate key is surely a VARCHAR Then i consider the data. Could it be reasonably short long (meaning, say, 20 figures or less)? Or perhaps is the VARCHAR area rather lengthy? Whether it's short it's functional like a key - whether it's lengthy, possibly it's easier to not utilize it like a key (although whether it's in consideration to be the main key I am most likely going to need to index it anyways). A minimum of a part of my concern would be that the primary key will have to become indexed and can possibly be utilized for an overseas key from another table. Evaluations of VARCHAR fields are usually reduced compared to comparison of number fields (particularly binary number fields for example integers) so utilizing a lengthy VARCHAR area like a key may lead to slow performance. YMMV.

I'd say it's fine to make use of VARCHAR as both PRIMARY and FOREIGN Secrets.

Only problem I possibly could forsee is for those who have a table, allows say Instruments (share instruments) and also you produce the PRIMARY/FOREIGN KEY as VARCHAR, also it happens the CODE changes.

This may happen on Stock Markets, and would need you to relabel all references for this CODE, while a ID nr wouldn't require this of your stuff.

To conclude, I'd say this dependes in your intended use.


When I only say CODE, I am talking about the Ticker Code for allows say GOOG, or other share. It's possible of these codes to alter with time, allows say you appear at Dirivative/Future instruments.

There is nothing wrong with either approach, even though this question might start the typical argument of what's best: natural or surrogate secrets.

If you are using CHAR or VARCHAR like a primary key you'll finish up utilizing it like a forign key sooner or later. As it pertains lower into it, as @astander states, it is dependent in your data and just how you will utilize it.

If one makes the course title in to the ID you'll have a problem should you ever choose to relabel a category.