What type of identifier is better to request and use as a foreign key in mysql:
1) Varchar ID with size 20
2) Int id
Usually int is better because it gives better performance.
If your element has a natural key , which is varchar, you can use it and it will work. But you will get a performance boost (and some other benefits) by adding a surrogate key , which is an integer. You can add a column AUTO_INCREMENTfor this purpose.
AUTO_INCREMENT
INT identifiers are always preferred.
It is faster and uses less storage in the database.
, VARCHAR, INT
Int IDSs are more efficient because the database will only compare once.
So let's say you have lines n. If you use int, you look at O(n)how the worst case is. If you use varchar(20), you lookO(n*20)
n
int
O(n)
varchar(20)
O(n*20)