Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATABASE DESIGN - Primary key for COUNTRY, CURRENCY int or varchar

  • For my country table, I used the country code as the primary key "AU, US, UK, FR" etc

  • For my currency table, I used the currency code as the primary key "AUD, GBP, USD" etc

I think what I did is ok, but another developer wants me to change all the primary keys to an int, because the country code, currency code might change sometime in the future he said. We just don't know that, well in this case he is right, his path is the safest path to take.

Should I change the primary keys to an int to be safe rather than be sorry? Can't I just keep it?

like image 447
001 Avatar asked Feb 02 '11 06:02

001


People also ask

Is primary key can be VARCHAR?

It is perfectly acceptable to use a varchar column as the primary key.

Can primary key be foreign?

The primary key column(s) can be used (along with the foreign key) to create a reference between two tables. As shown earlier, the primary key column of one table can be the foreign key column in another table. Thus, it creates a link between the two tables.

Does primary key need to be integer?

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.

What data type should primary keys be?

The data type of a primary key should be a numeric, an integer or a short, fixed-width character. Uniqueness. A primary key must have a unique value, which implies that no other row in the table has the same value in the column.


1 Answers

I would use the ISO codes with char columns.

If a country ever splits then you'd get new ISO codes (say SC, WL, EN) but UK will still be valid for historic data.

It's the same for currency. A transaction in 2000 would be in the currency at that time: French Francs, Deutschmarks, Belgium Banana but not Euro.

like image 95
gbn Avatar answered Sep 21 '22 16:09

gbn