Numeric Data Types
Integer Types
Integer types range from TINYINT (1 byte, up to 255 unsigned) to BIGINT (8 bytes, enormous range) — picking the smallest type that comfortably fits your data saves storage and speeds up indexes on large tables.
Example: Integer Types
CREATE TABLE items (
small_count TINYINT,
big_count BIGINT
);
Decimal Types
DECIMAL stores numbers as exact fixed-point values rather than approximations, making it the correct choice for currency and other figures where even tiny rounding drift is unacceptable over many calculations.
Example: Decimal Types
CREATE TABLE invoices (amount DECIMAL(10,2));
INSERT INTO invoices VALUES (19.99);
Float and Double
FLOAT and DOUBLE trade a small amount of precision for speed and compactness, which is fine for scientific measurements or graphing data but risky for anything requiring exact totals, like invoices.
Example: Float and Double
CREATE TABLE measurements (temperature FLOAT, distance DOUBLE);
Unsigned Numbers
Marking a numeric column UNSIGNED removes the ability to store negative values but doubles the positive range available — a good fit for columns like age or quantity that are never negative anyway.
Example: Unsigned Numbers
CREATE TABLE people (age INT UNSIGNED);
Zerofill Attribute
ZEROFILL pads a numeric column's displayed value with leading zeros up to its defined width (e.g. 007 instead of 7), and as a side effect it implicitly makes the column UNSIGNED as well.
Example: Zerofill Attribute
CREATE TABLE codes (id INT ZEROFILL);
INSERT INTO codes VALUES (7);
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: