MySQL data type and library strategy

Source: Internet
Author: User
Tags datetime numeric
Whether in a small, free database space or a large e-commerce site, a reasonable design of the table structure, the full use of space is very necessary. This requires us to have a good understanding of the common data types of database systems. Below I will write a little of my experience to share with you.
One, the number type. The number types are grouped into three categories according to my classification: integer, Decimal, and numeric.
What I call "digital class" means decimal and numeric, which are the same type. It's strictly not a numeric type because they actually save the number as a string, and every bit of his value (including the decimal point) occupies a byte of storage space, so this type of space is much more expensive. But one of its prominent advantages is that the number of decimal digits fixed, in the operation will not "distorted", so the more suitable for the "price", "amount" for the accuracy of the request is not high accuracy requirements of a very high level of the field.
Decimal class, that is, floating point type, according to the accuracy of different, there are float (single precision) and double (double precision) two. Their advantage is precision, float can mean that the absolute value is very small, small to about 1.17E-38 (0.000 ...). 0117, a decimal number with 37 0 after the decimal point, and a double can represent an absolute value smaller than about 2.22E-308 (0.000 ...). 0222, the decimal number of 307 0 after the decimal point. The float type and double type occupy storage space of 4 bytes and 8 bytes respectively. If you need to use a decimal field, the precision is not high, of course, with float! But to say the truth, we "civil" data, which has the requirement of precision so high? These two types have not been used so far-I have not yet encountered cases that are appropriate for using them.
The most used, the most worthy of reckoning, is the integer type. From a tinyint of only one byte of storage space to a 8-byte bigint, it should be considered when designing a database to pick a "sufficient" type that occupies the smallest amount of storage space. TINYINT, SMALLINT, mediumint, int, and bigint occupy storage space of 1 bytes, 2 bytes, 3 bytes, 4 bytes, and 8 bytes, and for unsigned integers, the largest integers that these types can represent are 255, 65535, 16777215, respectively. 4294967295 and 18446744073709551615. If you are saving the age of the user (for example, the age in the database is not advisable), with tinyint is enough; in the "vertical and horizontal" of the nine city, the skill value, with smallint also enough; If you want to use a table that is definitely not more than 16000000 rows, auto_ Increment identify field, of course, with Mediumint not INT, imagine, each line to save a byte, 16000000 lines can save 10 trillion more!
Date and Time type.
The date and time types are simpler, with a few types of dates, times, DATETIME, timestamp, and year. Only date sensitive, but not required for time fields, use date without a datetime is needless to say, the use of time alone is also the case---use times, but the most use or DateTime. There is nothing to do on the date-time type, and it is no longer detailed here.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.