MySQL data types and database building strategies

Whether it is a pitifully small free database space or a large e-commerce website, it is very necessary to design the table structure reasonably and make full use of the space. This requires us to have a full understanding of the commonly used data types of database systems. Below I will write down a little of my experience to share with you.

1. Number Types

Number types are divided into three categories according to my classification method: integer, decimal and number.
By "numeric classes" I mean DECIMAL and NUMERIC, which are the same type. It is not strictly a numeric type, because they actually store numbers as strings; each digit of its value (including the decimal point) occupies a byte of storage space, so this type consumes space for comparison Big. However, one of its outstanding advantages is that the number of decimal places is fixed, and it will not be "distorted" in the operation, so it is more suitable for fields such as "price" and "amount" that do not require high precision but require very high accuracy.
The decimal class, that is, the floating-point number type, has two types, FLOAT and DOUBLE, depending on the precision. Their advantage is precision, FLOAT can represent very small absolute values, as small as about 1.17E-38 (0.000…0117, 37 zeros after the decimal point) decimals, while DOUBLE can represent absolute values ​​as small as about 2.22E -308 (0.000…0222 with 307 zeros after the decimal point). The storage space occupied by FLOAT type and DOUBLE type is 4 bytes and 8 bytes respectively. If you need to use decimal fields, and the precision is not high, of course, use FLOAT. But to be honest, how can our "civilian" data require such high precision? I haven't used either of these types yet - I haven't come across an instance where they are suitable.
The most used, and most worthy of careful consideration, is the integer type. From TINYINT, which occupies only one byte of storage space, to BIGINT, which occupies 8 bytes, picking a type that is "enough" and takes up the least storage space should be considered when designing a database. TINYINT, SMALLINT, MEDIUMINT, INT and BIGINT occupy storage space of 1 byte, 2 bytes, 3 bytes, 4 bytes and 8 bytes respectively. For unsigned integers, the largest integers that can be represented by these types are respectively are 255, 65535, 16777215, 4294967295, and 18446744073709551615. If it is used to save the age of the user (for example, it is not advisable to save the age in the database), it is enough to use TINYINT; in The Ninetowns' "Aspect", it is also enough to use SMALLINT; if you want to use SMALLINT Make an IDENTIFY field of AUTO_INCREMENT of a table with no more than 16000000 rows, of course, use MEDIUMINT instead of INT, just imagine, save one byte per row, 16000000 rows can save more than 10 trillion.

Second, the date and time type

Date and time types are relatively simple, nothing more than several types such as DATE, TIME, DATETIME, TIMESTAMP and YEAR. For fields that are only sensitive to date, but not required for time, it goes without saying that DATE is used instead of DATETIME; the use of time alone also occurs from time to time - TIME is used; but DATETIME is the most used. There is nothing to do with datetime types, so I won't go into details here.

Three, character (string) type

Don't think 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), it will occupy 10 bytes regardless of whether the data you store reaches 10 bytes. Byte space; and VARCHAR is variable length, if the possible value of a field is not fixed length, we only know that it cannot exceed 10 characters, it is most cost-effective to define it as VARCHAR(10), The footprint of a VARCHAR type is the actual length of its value + 1. Why +1? This one byte is used to save how much length is actually used. It should also be seen from this +1 that if a field has a maximum possible value of 10 characters, and in most cases when 10 characters are used, it is not cost-effective to use VARCHAR: because in most cases , the actual occupied space is 11 bytes, which is one more byte than using CHAR(10).
For example, it is a table that stores stock names and codes. Most of the stock names are four characters, that is, 8 bytes; the stock code is six digits in Shanghai and four digits in Shenzhen. These are all fixed lengths. Of course, CHAR(8) should be used for the stock name. Although the stock code is not of fixed length, if VARCHAR(6) is used, the actual space occupied by a Shenzhen stock code is 5 bytes, while a Shanghai stock code will take up 5 bytes. The ticker symbol takes up 7 bytes! Considering that there are more stocks in Shanghai than in Shenzhen, then using VARCHAR(6) is not as cost-effective as CHAR(6).
Although the maximum length of a CHAR or VARCHAR can be up to 255, I think a CHAR greater than 20 is hardly used - there are very few fixed-length stuff greater than 20 bytes, right? Use VARCHAR if it is not a fixed length. VARCHARs greater than 100 are hardly used - TEXT is fine for larger ones. TINYTEXT, the maximum length is 255, the occupied space is also the actual length + 1; TEXT, the maximum length is 65535, the occupied space is the actual length + 2; MEDIUMTEXT, the maximum length is 16777215, the occupied space is the actual length + 3; LONGTEXT, the maximum length is 4294967295, occupied The space is the actual length + 4. Why +1, +2, +3, +4? If you don't know it, it's time to play PP. These can be used in forums, news, etc., to save the text of the article. According to the actual situation, choose different types from small to large.

Fourth, enumeration and collection types

Enumeration (ENUM) type, you can define up to 65535 different strings to choose from, you can only and must choose one of them, the storage space occupied is one or two bytes, by the enumeration Determined by the number of values; set (SET) type, can have up to 64 members, you can choose from zero to an unlimited number of them, and the storage space occupied is one to eight bytes, determined by the number of possible members of the set .
For example, in SQLServer, you can save to use a BIT type to represent gender (male/female), but mysql does not have BIT, use TINTINT? No, you can use ENUM('handsome guy', 'cute girl'), there are only two options, so only one byte - as big as TINYINT, but can be accessed directly with the strings 'handsome guy' and 'cute girl' . It's so convenient!
Well, the data types of MySQL are almost introduced, and my database building strategy is also introduced to you along with the introduction of data types. But this is only a part of it, and the space is limited, so I can't go into details; the rest depends on everyone's practice and discussion on the basis of their understanding of data types. Original link: http://www.kubiji.cn/topic-id2558.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326435491&siteId=291194637