liveBook · Manning

Varchar data type

As explained in Grokking Relational Database Design

VARCHAR is a variable-length SQL character string data type that stores values up to a declared maximum length.

VARCHAR stores character strings whose lengths can vary within a declared maximum. It fits attributes such as names, where values are relatively short, differ in length, and have a reasonably predictable upper limit. section 4.5

Choosing the right string type matters because unnecessary storage can increase memory and computation needs as a database grows. Use CHAR for uniform-length values, VARCHAR for short varied values, and TEXT for large text whose limit is difficult to estimate. section 4.5

Customer names

For a customer-name attribute, lengths vary but an upper limit of about 30 characters is easy to estimate, making VARCHAR a practical choice. By comparison, a two-character state code suits CHAR, while text regularly exceeding a thousand characters suits TEXT. section 4.5

Filtering a VARCHAR value

String values appear in single quotes when filtering. For example: SELECT name FROM product WHERE manufacturer = 'Mad Inventors Inc.'; A product table can use a string type for manufacturer and a numeric type for price, then combine conditions with AND. section 1.7

Choose among the common string types using these practical rules:

  • Use CHAR when the attribute always has a uniform length. section 4.5
  • Use VARCHAR when the attribute is relatively short, varies in length, and has an estimable maximum. section 4.5
  • Use TEXT when values regularly exceed a few thousand characters or their maximum length is hard to estimate. section 4.5

How Grokking Relational Database Design covers it

How deep it goes
Compares VARCHAR with CHAR and TEXT through storage rationale, selection rules, and a customer-name example.
Examples
Uses customer names, state codes, long text, and a manufacturer filter to distinguish VARCHAR from related types.
What you can do after
Apply it via SQL by choosing VARCHAR for suitable columns and filtering stored string values with quoted literals.
How it connects
Connects VARCHAR with SQL string literals, filtering, CHAR, TEXT, Unicode character strings, and numeric data types.

Also explained in

Compare how 4 books explain varchar data type →

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.