MySQL provides exact integer and fixed-point types as well as approximate floating-point types. Select the type based on range, exactness and storage requirements.
Quick query: DECIMAL is suited to exact fixed-point values, such as prices.
SELECT CAST(199.95 AS DECIMAL(10,2)) AS price;
Expected result
| price |
|---|
| 199.95 |
| Type | Bytes | Signed range | Unsigned range |
|---|---|---|---|
| TINYINT | 1 | -128 to 127 | 0 to 255 |
| SMALLINT | 2 | -32,768 to 32,767 | 0 to 65,535 |
| MEDIUMINT | 3 | -8,388,608 to 8,388,607 | 0 to 16,777,215 |
| INT | 4 | -2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| BIGINT | 8 | -2^63 to 2^63-1 | 0 to 2^64-1 |
DECIMAL(M,D) stores exact fixed-point values. M is total precision and D is the scale.
price DECIMAL(10,2)This is a common choice for currency-like values.
FLOAT and DOUBLE are approximate floating-point types. Use them for scientific measurements where approximation is acceptable, not for values that require exact decimal arithmetic.
INT(3). A display width does not restrict an integer to three digits and should not be used as a range rule.For example, an inventory item needs an ID, a quantity, a price and possibly an approximate measurement. Create this sample table in a test database:
CREATE TABLE numeric_demo (
id INT UNSIGNED NOT NULL,
quantity SMALLINT UNSIGNED NOT NULL,
unit_price DECIMAL(10,2) NOT NULL,
weight_kg DOUBLE,
PRIMARY KEY (id)
);
Insert one row, then calculate the value of the quantity in stock:
INSERT INTO numeric_demo (id, quantity, unit_price, weight_kg)
VALUES (1, 3, 199.95, 1.25);
SELECT quantity, unit_price,
quantity * unit_price AS total_price
FROM numeric_demo
WHERE id = 1;
Expected output (example data)
| quantity | unit_price | total_price |
|---|---|---|
| 3 | 199.95 | 599.85 |
DECIMAL(10,2) permits 10 total decimal digits, with 2 after the decimal point. FLOAT and DOUBLE use approximate binary floating-point representations and are suitable where small approximation errors are acceptable, such as measurements. Avoid approximate types for money that requires exact decimal arithmetic.
UNSIGNED removes negative values from supported integer types and increases the positive range. For a count or ID that cannot be negative, it may be appropriate; choose it based on your data model and compatibility with related columns.
SHOW COLUMNS FROM numeric_demo;
For related examples, see BOOLEAN/TINYINT, ROUND() and CREATE TABLE.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.