| MySQL Basic |
| |
| Choosing Column Type |
| |
| When you choose a numeric type, consider the range of values you need to represent and choose the smallest type that will cover the range. Choosing a larger type wastes space, leading to tables that are unnecessarily large and that cannot be processed as efficiently as if you had chosen a smaller type. |
| |
| For integer values, TINYINT is the best if the range of values in your data is small, such as a person's age or number of siblings. MEDIUMINT can represent millions of values and can be used for many more types of values, at some additional cost in storage space. |
| |
| BIGINT has the largest range of all but requires twice as much storage as the next smallest integer type (INT) and should be used only when really necessary. For floating-point values, DOUBLE takes twice as much space as FLOAT. |
| |
| Unless you need exceptionally high precision or an extremely large range of values, you can probably represent your data at half the storage cost by using FLOAT. |
| |
| Every numeric column's range of values is determined by its type. If you attempt to insert a value that lies outside the column's range, truncation occurs; MySQL clips the value to the appropriate endpoint of the range and uses the result. No truncation occurs when values are retrieved. |
| |
| Value truncation occurs according to the range of the column type, not the display width. For example, a SMALLINT(3) column has a display width of 3 and a range from -32768 to 32767. |
| |
| The value 12345 is wider than the display width but within the range of the column, so it is inserted without clipping and retrieved as 12345. The value 99999 is outside the range, so it is clipped to 32767 when inserted. Subsequent retrievals retrieve the value 32767. |
| |
| In general, values assigned to a floating-point column are rounded to the number of decimals indicated by the column specification. If you store 1.23456 in a FLOAT(8,1) column, the result is 1.2. If you store the same value in a FLOAT(8,4) column, the result is 1.2346. |
| |
| This means you should declare floating-point columns with a sufficient number of decimals to give you values as precise as you require. If you need accuracy to thousandths, don't declare a type with only two decimal places. |
| |
| The DECIMAL type is a floating-point type, but it differs from FLOAT and DOUBLE in that DECIMAL values actually are stored as strings and have a fixed number of decimals. |
| |
| The significance of this fact is that DECIMAL values are not subject to roundoff error the way that FLOAT and DOUBLE columns are-a property that makes DECIMAL especially applicable to currency calculations. |
| |
| The corresponding tradeoff is that DECIMAL values are not as efficient as floating-point values stored in native format that the processor can operate on directly. |
| |
| How D Affects the Range of DECIMAL(M,D) |
Type Specification |
Range (for MySQL < 3.23) |
Range (for MySQL >3.23) |
DECIMAL(4,0) |
–999 to 9999 |
–9999 to 99999 |
DECIMAL(4,1) |
–9.9 to 99.9 |
–999.9 to 9999.9 |
DECIMAL(4,2) |
–.99 to 9.99 |
–99.99 to 999.99 |
|
| |
| The maximum possible range for DECIMAL is the same as for DOUBLE, but the effective range is determined by the values of M and D. If you vary M and hold D fixed, the range becomes larger as M becomes larger. |
| |
| How M Affects the Range of DECIMAL(M,D) |
Type Specification |
Range (for MySQL < 3.23) |
Range (for MySQL |
DECIMAL(4,1) |
–9.9 to 99.9 |
–999.9 to 9999.9 |
DECIMAL(5,1) |
–99.9 to 999.9 |
–9999.9 to 99999.9 |
DECIMAL(6,1) |
–999.9 to 9999.9 |
–99999.9 to 999999.9 |
|
| |
| The range for a given DECIMAL type depends on your version of MySQL. As of MySQL 3.23, DECIMAL values are handled according to the ANSI specification, which states that a type of DECIMAL(M,D) must be able to represent any value with M digits and D decimal places. |
| |
| For example, DECIMAL(4,2) must be able to represent values from -99.99 to 99.99. Because the sign character and decimal point must still be stored, this requires an extra two bytes, so DECIMAL(M,D) values for MySQL 3.23 and later use M+2 bytes. For DECIMAL(4,2), six bytes are needed for the "widest" value (-99.99). |
| |
| At the positive end of the range, the sign byte is not needed to hold a sign character, so MySQL uses it to extend the range beyond that required by the ANSI specification. In other words, for DECIMAL(4,2), the maximum value that can be stored in the six bytes available is 999.99. |
| |
| There are two special conditions that reduce the DECIMAL storage requirement of M+2 bytes to a lesser value: |
| |
| . If D is 0, DECIMAL values have no fractional part and no byte need be allocated to store the decimal point. This reduces the required storage by one byte. |
| |
| . If a DECIMAL column is UNSIGNED, no sign character need be stored, also reducing the required storage by one byte. |
| |
| For versions of MySQL prior to 3.23, DECIMAL values are represented in a slightly different fashion. A DECIMAL(M,D) column is stored using M bytes per value, and the sign character and decimal point (if needed) are included in the M bytes. |
| |
| Thus, for a type DECIMAL(4,2), the range is -.99 to 9.99 because those cover all the possible 4-character values. If D is 0, no decimal point need be stored, and the byte usually used for that purpose can be used to store another digit. The effect is to extend the range of the column by an extra order of magnitude. |
| |
| |
|
|
| |
| |