7๏ธโƒฃ ๐Ÿ”ฃ MySQL Data Types
Estimated reading: 4 minutes 23 views

๐Ÿ”ข MySQL Numeric Types โ€“ INT, FLOAT, DECIMAL, TINYINT, BOOLEAN, BIT Explained


๐Ÿงฒ Introduction โ€“ Why Numeric Data Types Matter in MySQL?

Numeric types in MySQL are used to store quantities, identifiers, prices, scores, flags, and measurements. Choosing the right type affects:

  • โœ… Data accuracy
  • โœ… Storage size
  • โœ… Query performance
  • โœ… Validation and precision

MySQL offers integer, fixed-point, and floating-point types, as well as binary and boolean-like types for flags and status checks.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The syntax, range, and purpose of each numeric type
  • Differences between INT, FLOAT, and DECIMAL
  • Special types like BOOLEAN, BIT, and TINYINT
  • Real-world examples and best practices

๐Ÿ”ข 1. Integer Types โ€“ Whole Numbers

๐Ÿ”น Common Integer Types

TypeStorageSigned RangeUnsigned Range
TINYINT1 byte-128 to 1270 to 255
SMALLINT2 bytes-32,768 to 32,7670 to 65,535
MEDIUMINT3 bytes-8.3M to 8.3M0 to 16.7M
INT4 bytes-2.1B to 2.1B0 to 4.2B
BIGINT8 bytes~ยฑ9 quintillion0 to ~18 quintillion

๐Ÿ”น Example

CREATE TABLE employees (
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  age TINYINT,
  years_with_company SMALLINT
);

Explanation:

  • id: Unique, positive integer (UNSIGNED) for employee ID.
  • age: Small number, stored efficiently with TINYINT.
  • years_with_company: Range of years, using SMALLINT.

๐Ÿงฎ 2. Floating Point Types โ€“ Approximate Precision

TypeStoragePrecisionUse Case
FLOAT(p)4 bytes~7 digits (p โ‰ค 24)Light-weight approximate values
DOUBLE(p)8 bytes~15-16 digits (p > 24)High-range scientific values

๐Ÿ”น Example

CREATE TABLE weather (
  temperature FLOAT,
  humidity DOUBLE
);

Explanation:

  • temperature: Approximate float with 7-digit precision.
  • humidity: Higher-precision value for scientific storage.

โš ๏ธ Warning: Avoid for financial dataโ€”may cause rounding errors.


๐Ÿ’ฐ 3. DECIMAL(p, s) โ€“ Exact Precision (Fixed-Point)

๐Ÿ”น Syntax

DECIMAL(p, s)
  • p: Total number of digits.
  • s: Digits after the decimal point.

๐Ÿ”น Example

CREATE TABLE products (
  product_name VARCHAR(100),
  price DECIMAL(10, 2)
);

Explanation:

  • price: Stores values like 99999999.99 with exact precision.
  • โœ… Ideal for money, tax, billing, currency.

โœ… 4. BOOLEAN โ€“ True/False Alias

CREATE TABLE flags (
  is_active BOOLEAN
);

Explanation:

  • MySQL maps BOOLEAN to TINYINT(1).
  • 0 = FALSE, 1 = TRUE.
  • โœ… Useful for status flags and checkboxes.

๐Ÿ”น Insert & Select Examples

INSERT INTO flags (is_active) VALUES (TRUE), (FALSE);
SELECT * FROM flags WHERE is_active = TRUE;

๐Ÿงท 5. BIT(n) โ€“ Binary Flags

๐Ÿ”น Syntax

BIT(n)  -- where n = number of bits (1โ€“64)

๐Ÿ”น Example

CREATE TABLE permissions (
  role_id INT,
  perms BIT(4)
);

Explanation:

  • BIT(4) stores binary flags like 0001, 1110, etc.
  • Efficient way to store multiple YES/NO flags in one column.

๐Ÿ”น Insert Example

INSERT INTO permissions (role_id, perms) VALUES (1, b'1010');

Explanation:
Stores the permission mask 1010 (binary) for role 1.


๐Ÿ“Š Numeric Types Comparison Table

TypeExact?Range / SizeBest For
INTโœ… Exactยฑ2BIDs, counters
TINYINTโœ… Exactยฑ128Age, flags, booleans
BIGINTโœ… Exactยฑ9 quintillionLarge keys, UUIDs
FLOATโŒ Approx~7 digitsSensor, analytics
DOUBLEโŒ Approx~15 digitsScientific, large-scale measurements
DECIMALโœ… ExactControlled by (p, s)Currency, billing
BOOLEANโœ… AliasInternally TINYINT(1)TRUE/FALSE values
BITโœ… ExactBinary 1โ€“64 bitsCompact flag storage

๐Ÿ“˜ Best Practices

โœ… Tip๐Ÿ’ก Why It Matters
Use DECIMAL for financial calculationsAvoids rounding errors from floating point math
Use smallest integer that fits your rangeSaves storage and improves performance
Use UNSIGNED for IDs if never negativeDoubles positive range
Use BOOLEAN for true/false logicCleaner than raw TINYINT
Use BIT for compact, fast permission flagsAvoids extra columns or separate flag tables

๐Ÿš€ Real-World Use Cases

ScenarioTypeReason
Employee IDINT UNSIGNEDPositive, auto-increment primary key
Product PriceDECIMAL(10, 2)Exact pricing values
User Active StatusBOOLEANSimple on/off flag
Sensor ReadingsFLOATApproximate with fast processing
Role PermissionsBIT(8)Bitmask encoding for roles

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Numeric types in MySQL are fundamental to data accuracy, size efficiency, and logical correctness. Choosing the right one for each scenario helps avoid bugs, boosts performance, and keeps schemas optimized.

๐Ÿ” Key Takeaways

  • Use INT/BIGINT for IDs and counters.
  • Use DECIMAL for accurate currency or calculations.
  • Avoid FLOAT/DOUBLE for precise math.
  • Use BOOLEAN or TINYINT(1) for flags.
  • Use BIT(n) for compact binary storage.

โš™๏ธ Real-World Relevance

From billing apps to IoT platforms, smart numeric type design drives accuracy, performance, and clarity across systems.


โ“ FAQ โ€“ MySQL Numeric Types


โ“ When should I use DECIMAL instead of FLOAT?

โœ… Use DECIMAL for money, billing, and finance where rounding is unacceptable.


โ“ Can BOOLEAN store more than TRUE/FALSE?

โŒ No. It’s an alias for TINYINT(1), and only 0 and 1 are valid in strict mode.


โ“ Can I use BIT as a boolean?

โœ… Yes, but itโ€™s less readable than BOOLEAN or TINYINT(1).


โ“ Whatโ€™s the max value for an INT?

Signed: -2,147,483,648 to 2,147,483,647
Unsigned: 0 to 4,294,967,295


โ“ Are FLOAT and DOUBLE accurate?

โŒ No. They are approximate. Use only for scientific, sensor, or large-range dataโ€”not financials.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”ข MySQL Numeric Types (INT, FLOAT, DECIMAL, TINYINT, BOOLEAN, BIT)

Or Copy Link

CONTENTS
Scroll to Top