liveBook · Manning

Char(n)

As explained in Grokking Relational Database Design

CHAR is a fixed-length SQL character-string data type whose declared length determines its stored character width.

CHAR is a string data type for values whose length is uniform. Declare the required width in parentheses, such as CHAR(12); use it when every value for an attribute has the same number of characters. section 4.5 section 4.6

Choosing a suitable string type matters as a database grows: unnecessarily large data can increase storage, memory, and lookup costs. For short values with varying lengths, use VARCHAR; for regularly large text with an uncertain upper limit, use TEXT. section 4.5

Use CHAR for identifiers that look numeric but are not used in calculations:

  • Card numbers can use CHAR(16), preserving any leading zeros while recording their uniform 16-digit length. section 4.6
  • Expiry dates can use CHAR(4) when each value contains four digits. section 4.6
  • Universal Product Codes can use CHAR(12), because each UPC has 12 digits and may begin with zero. section 4.6

Treat codes as strings

A value can consist only of digits yet still be string data. A product code is not meant for arithmetic, and an integer type can remove leading zeros. CHAR(12) records its fixed 12-character form instead. section 4.6

How Grokking Relational Database Design covers it

How deep it goes
Defines selection rules and applies declared widths to card, expiry-date, and product-code attributes.
Examples
Uses state or province codes, card numbers, expiry dates, and UPC product codes.
What you can do after
Apply it to define fixed-width codes and numeric-looking identifiers as strings rather than integers.
How it connects
Contrasts CHAR with VARCHAR, TEXT, integer data types, and leading zeros.

Also explained in

Compare how 5 books explain char(n) →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Relational Database Design. The section links point to the parts of the book it draws on.