MySQL
MySQL

Data Types

Every column in a table has a data type, which tells MySQL exactly what kind of value that column will hold and how to store it efficiently. For whole numbers, INT is the common choice (a student's age, a quantity). For numbers with decimals, DECIMAL(10,2) is common for money, because it stores exact values rather than the tiny rounding errors floating-point types can introduce. For text, VARCHAR(255) is a variable-length string with a maximum size you set (a name, an email) it only uses as much storage as the actual text needs, up to that limit. TEXT is for much longer text, like a full blog post, where you don't want to cap the length tightly. For dates and times, DATE stores just a calendar date (2026-09-25), DATETIME stores a date and time together, and TIMESTAMP is similar but is commonly used for tracking exactly when a row was created or changed. BOOLEAN (stored internally as a tiny integer) holds true/false-style values, like whether an account is active. Choosing the right type isn't just a technicality it directly affects what values MySQL will accept, how much space is used, and how correctly your data can be sorted and compared later.

Why this matters

  • ·The right data type stops invalid values (like text in a number column) from ever being stored.
  • ·DECIMAL avoids the rounding errors that can creep into financial data stored as floating-point numbers.
  • ·Choosing appropriately sized types keeps a growing table efficient rather than bloated.

Common mistakes

  • ·Storing money as a floating-point type (FLOAT/DOUBLE) can introduce tiny but real rounding errors over many calculations.
  • ·Making every text column an oversized VARCHAR or TEXT 'just in case' wastes space and can hide real data problems.
  • ·Storing dates as plain text instead of a DATE/DATETIME type makes sorting and date-based filtering unreliable.
Your codePHP
Expected output
+-------+--------------+------+-----+---------+
| Field | Type         | Null | Key | Default |
+-------+--------------+------+-----+---------+
| id    | int          | NO   | PRI | NULL    |
| name  | varchar(100) | NO   |     | NULL    |
| age   | int          | YES  |     | NULL    |
+-------+--------------+------+-----+---------+
3 rows in set

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

Always for money. DECIMAL stores exact values, while FLOAT/DOUBLE can introduce small rounding errors that add up over many calculations.

Practice questions

Question 1

Write DESCRIBE for a table called products.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Write DESCRIBE for a table called orders.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

Which type is best for storing a price?