MajAfy MajAfy - 28 days ago 14
MySQL Question

The logic of field type in relationship

I have some tables for user preferences like

currency
,
language
and ...

currency
structure and sample records :

id | title | slug
----+------------+--------
1 | US Dollar | usd
2 | CA Dollar | cad
3 | Euro | eur
4 | Swiss Franc| chf


language
structure and sample records :

id | title | code | flag
----+-------------+-----------+----------
1 | English | en | img/flags/en.png
2 | French | fr | img/flags/fr.png


I want create a relation between these tables and
users
tables, I want to know which field is better for foreign key ? for example in
currency
table
currency.id
or
currency.slug
?

Answer

If you see sql speed than you should go for currency.id because finding integer value from lots so many rows (in thousands) would be faster then finding into varchar.

But here you see currency won't be more then 500 so you can use both it won't affect your speed.

you can use both in this particular example: currency.id or currency.slug