Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best data type for a 1 character code column in a SQL table?

this is not a primary key, but merely used as a marker in existing system that marks these items with a 1 letter code ie 'N' for new, 'R' for return, etc. I was going to go with a varchar(50) as the data type but wondering if there's anything wrong with say varchar(2) instead aiming for efficiency. thx!

like image 460
mirezus Avatar asked Feb 01 '26 21:02

mirezus


1 Answers

The best datatype would be char(1).

varchar means variable width, that is, if on a varchar(50) you store only 3 characters, there will be no 47 wasted bytes. But there is a (small) performance hit on accessing it as it has to go check the actual length of the field for each row.

If the requirement changes later you can alter table and modify the datatype without much trouble.

like image 67
Vinko Vrsalovic Avatar answered Feb 04 '26 13:02

Vinko Vrsalovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!