Numeric Dataypes in Mysql
Monday, March 31st, 2008
|
TINYINT(M) |
8-bit integer (1 byte, - 128 to +127) |
|
SMALLINT(M) |
16-bit integer (2 bytes, - 32,769 to +32,767) |
|
MEDIUMINT |
24-bit integer (3 bytes, - 8,388,608 to +8,388,607) |
|
BIGINT(M) |
64-bit integer (8 bytes, ±9.22 × 1018) |
|
INT, INTEGER(M) |
32-bit integer (4 bytes, - 2,147,483,648 to +2,147,483,647) |
With the INT data type, both positive and negative numbers are generally allowed. With the attribute UNSIGNED, the range can be restricted to the positive integers. But note that then subtraction returns UNSIGNED integers, which can lead to deceptive and confusing results.
With TINYINT, numbers between -128 and +127 are allowed. With the attribute UNSIGNED, the range is 0 to 255. If one attempts to store a value above or below the given range, MySQL simply replaces the input with the largest or, respectively, smallest permissible value.
Optionally, in the definition of an integer field, the desired column width (number of digits) can be specified, such as, for example, INT(4). This parameter is called M (for maximum display size) in the literature. It assists MySQL as well as various user interfaces in presenting query results in a readable format.
AUTO_INCREMENT Integers
With the optional attribute AUTO_INCREMENT you can achieve for integers that MySQL automatically inserts a number that is 1 larger than the currently largest value in the column when a new record is created for the field in question. AUTO_INCREMENT is generally used in the definition of fields that are to serve as the primary key for a table.
The following rules hold for AUTO_INCREMENT:
-
This attribute is permitted only when one of the attributes NOT NULL, PRIMARY KEY,or UNIQUE is used as well.
-
It is not permitted for a table to possess more than one AUTO_INCREMENT column.
-
The automatic generation of an ID value functions only when in inserting a new data record with INSERT, a specific value or NULL is not specified. However, it is possible to generate a new data record with a specific ID value, provided that the value in question is not already in use.
-
If you want to find out the AUTO_INCREMENT value that a newly inserted data record has received, after executing the INSERT command (but within the same connection or transaction), execute the command SELECT LAST_INSERT_ID( ).
-
If the AUTO_INCREMENT counter reaches its maximal value, based on the selected integer format, it will not be increased further. No more insert operations are possible. With tables that experience many insert and delete commands, it can happen that the 32-bit INT range will become used up, even though there are many fewer than two billion records in the table. In such a case, use a BIGINT column
FLOAT(M,D)— A floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2 is the number of decimals and 10 is the total number of digits (including decimals). Decimal precision can go to 24 places for a FLOAT.
DOUBLE(M,D)— A double precision floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE.
DECIMAL(M,D)— An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL.
Of all the MySQL numeric data types, you will likely use INT most often. You can run into problems if you define your fields to be smaller than you actually need; for example, if you define an id field as an unsigned TINYINT, you won’t be able to successfully insert that 256th record if ID is a primary key (and thus required).