Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which column type for storing the year field in a table with rows of yearly data

My Laravel web app uses the schema builder for database portability, so the MySQL-specific YEAR column type is not available.

I want to be able to sort and select rows of data by year (including BETWEEN). What's the best portable column type for storing year values?

like image 696
mtmacdonald Avatar asked Feb 10 '23 21:02

mtmacdonald


1 Answers

What's the best portable column type for storing year values?

smallint is a good choice to represent years, and it's ANSI SQL, so will work in most databases. It will last until the year 32767.

Some databases support create domain which basically lets you create your own types. You could create a year domain for other databases. I prefer the simplicity of smallint for year though.

like image 183
Neil McGuigan Avatar answered Feb 12 '23 11:02

Neil McGuigan