What is the difference between CHAR and VARCHAR in MySQL?
| Point | CHAR(n) | VARCHAR(n) |
|---|---|---|
| Length | Fixed. Always n characters, padded | Variable, up to n characters |
| Space | Always n | Only the text length (plus 1 or 2 bytes) |
| Best for | Values of the same length: country code, PIN | Names, emails, titles |
country_code CHAR(2), -- IN, US, UK
name VARCHAR(100) -- any name up to 100 characters
Use VARCHAR for most text. See MySQL Data Types.
Learn more about the similar topics:
| Tutorials |
|---|
| MySQL Data Types |
| MySQL CREATE DATABASE |
| MySQL USE and SHOW DATABASES |
| MySQL DROP DATABASE |
| MySQL CREATE TABLE |
| MySQL Constraints |
| MySQL DROP TABLE |
| Exercises & Assignments |
|---|
| Write a query to get all column names of a database table |
| Write a query to create a new table with the data and structure of another table |