Mysql Data Types and database creation policies

Source: Internet
Author: User

1. digit type. The number types are classified into three types by my classification: integer, decimal, and number.
My so-called "number classes" refer to decimal and numeric. They are of the same type. Strictly speaking, it is not a numeric type, because they actually save numbers as strings; Each digit of his value (including the decimal point) occupies a byte storage space, therefore, this type consumes a lot of space. However, one of its outstanding advantages is that the number of digits in decimal places is fixed and will not be "distorted" in the computation ", therefore, it is suitable for "price" and "amount" fields with low precision requirements but high accuracy requirements.
Decimals, that is, floating point numbers. Float (single precision) and double (Double Precision) are supported based on the precision. Their advantage is accuracy. Float can indicate that the absolute value is very small, as small as about 1.17e-38 (0.000... 0117, 37 digits after the decimal point), and double can indicate that the absolute value is smaller than about 2.22e-308 (0.000... 0222, with a decimal point followed by 307 zeros. The storage space occupied by float and double types is 4 bytes and 8 bytes respectively. If you need to use decimal fields, the precision is not high, of course, float is used! But to be honest, how does our "Civil" data require high accuracy? I have never used these two types so far-I have not encountered any examples that are suitable for using them.
The most commonly used, most cost-effective, is the integer type. From tinyint, which occupies only one byte of storage space, to bigint, which occupies eight bytes, selecting a type that is "adequate" and occupies the smallest storage space should be considered during database design. The storage space occupied by tinyint, smallint, mediumint, Int, and bigint is 1 byte, 2 byte, 3 byte, 4 byte, and 8 byte, respectively. For unsigned integers, the maximum Integers of these types are 255, 65535, 16777215, 4294967295, and 18446744073709551615. If it is used to save the user's age (for example, it is not advisable to store the age in the database), tinyint is enough. In 9-city's vertical bar, skill values, smallint is enough. If you want to use it as the identify field of auto_increment for a table with no more than 16000000 rows, of course, mediumint is not used. Imagine that each row saves one byte, 16000000 rows can save more than 10 MB!
Ii. Date and Time type.

The date and time types are simple. They are only date, time, datetime, timestamp, and year types. It is unnecessary to use date instead of datetime for fields that are only sensitive to date and have no requirements for time. Time is also used occasionally; however, datetime is used most. There is nothing in the date and time typeArticleYou can do this.

3. character (string) type.

Do not assume that the character type is Char! The difference between char and varchar is that char is a fixed length. As long as you define a field as char (10), no matter whether the data you store reaches 10 bytes, it occupies 10 bytes of space, while varvhar is variable length. If a field may have an unfixed length, we only know that it cannot exceed 10 characters, defining it as varchar (10) is the most cost-effective. The actual length of the varchar type is its value (actual length + 1 ). Why "+ 1? How long is this Byte actually used for saving! From this "+ 1", we can also see that if a field has a maximum value of 10 characters, and in most cases, 10 characters are used, varchar is not suitable: In most cases, the actual space occupied is 11 bytes, occupying one more byte than char (10!

For example, a storage stock name andCodeTable, the stock name is mostly four characters, that is, 8 bytes; stock code, Shanghai is a six-digit number, Shenzhen is a four-digit number. These are fixed length, and the stock name must use char (8). Although the stock code is not fixed length, if varvhar (6) is used ), A stock code in Shenzhen actually occupies 5 bytes, while a stock code in Shanghai occupies 7 bytes! Considering that Shanghai has more shares than Shenzhen, varchar (6) is not as good as char (6.

Although the maximum length of a char or varvhar can be up to 255, I think that a char larger than 20 is almost useless-there are very few fixed lengths larger than 20 bytes, right? Use varchar if it is not a fixed length! Varchar of more than 100 is almost useless-it is better to use text that is larger than this. Tinytext, the maximum length is 255, the occupied space is also (actual length + 1); text, the maximum length is 65535, the occupied space is (actual length + 2); mediumtext, the maximum length is 16777215, the occupied space is (actual length + 3); longtext, maximum length of 4294967295, and occupied space is (actual length + 4 ). Why "+ 1 "? "+ 2 "? "+ 3 "? "+ 4 "? If you do not know, you should play pp. These can be used in forums, news, or something to save the text of an article. Select different types from small to large based on the actual situation.

Iv. Enumeration and set types.

Enumeration (Enum) type. You can select a maximum of 65535 different strings. You can only select one of them and the storage space is one or two bytes, it is determined by the number of enumerated values. The set type can have a maximum of 64 members. You can select zero to multiple unlimited members, the storage space is one to eight bytes, which is determined by the number of possible members of the set.

For example, in sqlserver, you can save a bit type to indicate the gender (male/female), but MySQL does not have a bit, using tintint? No, you can use Enum (handsome guy, crush )! There are only two options, so only one byte is needed-as big as tinyint, but it can be accessed directly using string handsome guy and Meimei. It's so convenient!

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.