Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the difference between CHAR and VARCHAR, and how do you choose the right data types for a table?

CHAR(n) is fixed width — every value occupies n characters and shorter values are padded with spaces that are stripped on retrieval. VARCHAR(n) is variable width and stores a one or two byte length prefix plus the actual characters.

Use CHAR only when values really are a constant length: a country code, a hash of known width, an MD5 digest. Use VARCHAR for anything with a spread of lengths — names, emails, titles.

Choosing types generally, in priority order:

  • Pick the smallest type that comfortably fits. Narrower rows mean more rows per page, which means fewer disk reads and a more effective buffer pool. Do not use BIGINT for a column that will never exceed a few million.
  • Never store money in FLOAT or DOUBLE. Use DECIMAL, which is exact.
  • Prefer NOT NULL where the value is genuinely mandatory. Nullable columns cost an extra bit and make every query author reason about three-valued logic.
  • Store dates in DATE, DATETIME or TIMESTAMP, never in a VARCHAR. TIMESTAMP converts to UTC and is range-limited to 2038; DATETIME does not convert and has a much wider range.
All MySQL interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as