简体   繁体   中英

Which Datatype in MYSQL do I use to store only numbers 1 to 60 inside a data table?

I have created one table and I want to store numbers from 1 to 60 numbers only inside the field.

What should I put in the datatype of the table field? Should I use TINYINT (4) datatype?

"Best" data type is open to interpretation. Here are three options:

numeric(2, 0)
varchar(2)
tinyint(2)

These have different sizes, but that doesn't make them "best" -- except under certain circumstances where storage space is a primary concern. I am guessing that your "numbers" are not really numbers, but are codes of some sort that vary from 1 to 60.

If these are referencing a reference table, then tinyint makes sense as the key, because keys are often numbers. However, I often use int for such keys. The extra three bytes usually have little impact on performance.

If the code is zero-padded (so '01' rather than '1' ), then char(2) is the appropriate type. It might take more space, but it accurately represents the value.

If these are indeed numbers -- like addition or multiplication is defined -- then tinyint is definitely the most appropriate type.

Yes TINYINT is the best option its from -128 to 127!

Documentation Link: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM