Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to define a "year" column to be of type Integer or String?

I do realize that it is better for a column to be an Integer if one has to perform mathematical calculations on it.

I probably have to perform mathematical calculations on the "year" column but minimally. So would it be better to store it as a String or Integer?

Thanks.

like image 425
westoque Avatar asked Aug 04 '14 19:08

westoque


People also ask

What is the difference between the string and integer data type?

Integer is a numeric value, while String is a character value represented in quotes.

What is the best data type for age in SQL?

So, we should use integer data type for age.

What should be the datatype for year?

If you need to store a year in the database, you would either want to use an Integer datatype (if you are dead set on only storing the year) or a DateTime datatype (which would involve storing a date that basically is 1/1/1990 00:00:00 in format).

Is year an integer in SQL?

SQL Server YEAR() function overviewThe YEAR() function returns an integer value which represents the year of the specified date.


2 Answers

Save it as an integer.

While there may be some application where you are reading and serving this data so frequently that the int->string conversion is a problem... that is going to be an edge case.

On the other side

  • Integers provide smaller options than strings in data storage (such as TINYINT)
  • You avoid conversions due to math
  • It's going to confuse/annoy/frustrate all the developers that come after you when they query a data type that is naturally a number and get a string.
like image 125
Russell Steen Avatar answered Nov 15 '22 16:11

Russell Steen


If you are not expecting your YEAR variable to ever contain non-digit values then yes you should store it as a number.

I would not store it as INT since I doubt year will reach the limit that INT has to offer. I would save it as SMALLINT or even TINYINT either should be unsigned.

SMALLINT UNSIGNED gives you max value of 65535, unless you are storing years that exceed the year 65535 this should suffice.

like image 20
GGio Avatar answered Nov 15 '22 17:11

GGio