LambdaLabTM
Databases & SQL · Class 12 · Getting Started with MySQL
MySQLdata types⏱️ 12 min read

Data Types

Every column has to be told, in advance, what kind of value it will hold. That is its data type. It is not paperwork: it is the promise that lets the database sort dates properly, add up marks correctly, and refuse the word “hello” where a roll number belongs.

1The seven you need

MySQL data types for this course
TypeHoldsExample valueUse it for
intA whole number2035Roll numbers, class, quantity
bigintA very large whole number411122223333Aadhaar, phone numbers, big IDs
floatA number with a decimal part, stored approximately83.75Marks, percentages, measurements
decimal(p,d)A number with decimals, stored exactly1234.56Money — fees, salary, prices
char(n)Text of a fixed length, exactly n wide'IND'Codes: state, grade, Y/N flags
varchar(n)Text of a varying length, up to n'Gangtok'Names, cities, addresses
dateA calendar date'2008-08-13'Birth dates, joining dates

Declaring them looks like this — the type comes straight after the column name. Here is one table using all seven:

MySQL command line client
mysql> create table demo_types (
-> a int,
-> b bigint,
-> c float,
-> d decimal(8,2),
-> e char(3),
-> f varchar(20),
-> g date);
Query OK, 0 rows affected (0.17 sec)
 
mysql> desc demo_types;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| a | int | YES | | NULL | |
| b | bigint | YES | | NULL | |
| c | float | YES | | NULL | |
| d | decimal(8,2) | YES | | NULL | |
| e | char(3) | YES | | NULL | |
| f | varchar(20) | YES | | NULL | |
| g | date | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

And one row of real values going in and coming back out:

MySQL command line client
mysql> insert into demo_types values (7, 9876543210123, 3.5, 1234.56, "IND", "LambdaLab School", "2026-09-13");
Query OK, 1 row affected (0.03 sec)
 
mysql> select * from demo_types;
+------+---------------+------+---------+------+------------------+------------+
| a | b | c | d | e | f | g |
+------+---------------+------+---------+------+------------------+------------+
| 7 | 9876543210123 | 3.5 | 1234.56 | IND | LambdaLab School | 2026-09-13 |
+------+---------------+------+---------+------+------------------+------------+
1 row in set (0.00 sec)

2int and bigint

Both hold whole numbers. The difference is simply how big a number fits. An int stops a little above two billion, which is plenty for a roll number and nowhere near enough for an Aadhaar number:

MySQL command line client
mysql> create table small_id (a int); insert into small_id values (9876543210123);
ERROR 1264 (22003): Out of range value for column 'a' at row 1

Twelve digits will not fit. Widen the column to bigint and the same value is stored without complaint — which is exactly why the teachers table declares aadhar bigint.

A number you never calculate with is not really a number
Phone numbers are the classic case. They can begin with a zero, they are never added up, and a leading zero is lost the moment you store one as a number. Many designers use varchar(10) for them instead. Use bigint when the value is genuinely a quantity or an id you will compare numerically.

3float and decimal — and why money needs decimal

Both hold numbers with a decimal part. float stores an approximation in binary, and decimal stores the digits exactly. For marks nobody notices. For money everybody notices. Watch the same two values added up in both:

MySQL command line client
mysql> create table pay (f float, d decimal(10,2));
Query OK, 0 rows affected (0.23 sec)
 
mysql> insert into pay values (0.1, 0.1), (0.2, 0.2);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
 
mysql> select sum(f), sum(d) from pay;
+---------------------+--------+
| sum(f) | sum(d) |
+---------------------+--------+
| 0.30000000447034836 | 0.30 |
+---------------------+--------+
1 row in set (0.00 sec)

0.1 + 0.2 should be 0.3. The float column says 0.30000000447034836; the decimal column says 0.30. Neither is a MySQL bug — 0.1 has no exact binary form, so a float can only get close, and the error shows up once you add several of them together. A fee register that is a few paise out every time is not acceptable, and that is the whole argument for decimal.

Reading decimal(8,2)
The first number is the total count of digits and the second is how many of them come after the point. So decimal(8,2) holds six digits before the point and two after — up to 999999.99. It is not “8 before and 2 after”.

4What the number in brackets means

For text, varchar(30) does not mean thirty of anything. It is the maximum number of characters the column will accept. A name of six letters fits comfortably; one of thirty-one does not fit at all:

MySQL command line client
mysql> insert into demo_types values (1,1,1.0,1.0,"ABCD","x","2026-01-01");
ERROR 1406 (22001): Data too long for column 'e' at row 1

Column e was declared char(3) and "ABCD" is four characters, so the whole row is refused. Nothing is quietly cut short.

5char vs varchar

Both hold text and both take a length. The difference is what happens to the space you did not use.

char(10)
Fixed width

Always reserves room for 10 characters, whatever you store. Best when every value really is the same length — a state code, a grade, a Y/N flag.

varchar(10)
Variable width

Reserves only what the value needs, up to 10. Best when lengths differ — names, cities, addresses. This is the one you will use most.

That difference is usually described as invisible, but there is one place you can see it. Store 'hi   ' — “hi” followed by three spaces — in both, then wrap each in brackets so the spaces show:

MySQL command line client
mysql> create table cv (c char(6), v varchar(6));
Query OK, 0 rows affected (0.20 sec)
 
mysql> insert into cv values ('hi ', 'hi ');
Query OK, 1 row affected (0.03 sec)
 
mysql> select concat('[', c, ']') as chr, concat('[', v, ']') as vchr from cv;
+------+---------+
| chr | vchr |
+------+---------+
| [hi] | [hi ] |
+------+---------+
1 row in set (0.00 sec)
 
mysql> select length(c), length(v) from cv;
+-----------+-----------+
| length(c) | length(v) |
+-----------+-----------+
| 2 | 5 |
+-----------+-----------+
1 row in set (0.00 sec)
A correction worth making
Textbooks often say char(10) holding 'hi' gives you back “hi” plus eight spaces. It does not: as the output above shows, char strips trailing spaces on the way out — length 2 — while varchar keeps every one you gave it — length 5. So the fixed-vs-variable difference is real, but it is about how the value is stored, and the one visible effect is that char quietly loses trailing spaces.
char vs varchar, side by side
char(n)varchar(n)
WidthFixed — always nVariable — as much as the value needs
Storage usedThe same for every rowGrows and shrinks with the value
Trailing spacesRemoved when the value is read backKept exactly as supplied
Best forCodes of a known, equal lengthNames, cities, anything that varies
SpeedSlightly faster, being a fixed sizeSlightly slower, but saves space
Examplechar(3) for 'IND'varchar(30) for 'Rajesh Kumar'

6Dates have exactly one format

'YYYY-MM-DD'

year first, four digits, hyphens — and quotes around the whole thing

The day-first order you write by hand is not accepted. This is a favourite exam trap and a genuine everyday mistake:

MySQL command line client
mysql> insert into students values (9001,'Bad',10,80,'25-07-2050','Singtam');
ERROR 1292 (22007): Incorrect date value: '25-07-2050' for column 'dob' at row 1
Why year-first
Because it sorts correctly as plain text, and because it is unambiguous. Is 05-06-2010 the 5th of June or the 6th of May? It depends which country you are in. 2010-06-05 can only mean one thing.

7Putting the wrong thing in

MySQL command line client
mysql> insert into demo_types values ("hello",1,1.0,1.0,"AB","x","2026-01-01");
ERROR 1366 (HY000): Incorrect integer value: 'hello' for column 'a' at row 1

This is the data type earning its keep. Because grade is an int, nobody can ever type “twelve” into it, and so every query that compares grades can rely on finding numbers there.

8Which values need quotes

Quotes needed

char, varchar and date values.

"Ravi"   "2008-08-13"
No quotes

int, bigint, float and decimal values.

2035   83.75   1234.56

9Recap

int / bigint

Whole numbers. bigint when int overflows — Aadhaar, phone.

float / decimal

Approximate / exact. Money always takes decimal.

char(n) / varchar(n)

Fixed / variable text. char drops trailing spaces.

date

'YYYY-MM-DD', quoted. No other order is accepted.

Quick Check

Which type should hold a 12-digit Aadhaar number?

Quick Check

Why is decimal preferred over float for money?

Quick Check

decimal(8,2) can hold the largest value…

Quick Check

'hi ' (with three trailing spaces) is stored in both char(6) and varchar(6). What do length() return?

Quick Check

How must 13 August 2008 be written?