WIPIVERSE All articles

Varchar

VARCHAR (short for "variable character") is a data type used in relational database management systems to store character string data of variable length. It is defined in the SQL standard and is one of the most commonly used data types for storing textual information such as names, addresses, and descriptions.

Overview

The term "VARCHAR" is a contraction of "variable character." Unlike fixed-length character types such as CHAR, VARCHAR columns only consume storage space proportional to the actual length of the data stored, rather than a pre-allocated fixed amount. This makes VARCHAR more storage-efficient when the lengths of stored values vary considerably across rows.

In SQL syntax, VARCHAR is declared with a maximum length parameter, typically in the form VARCHAR(n), where n specifies the maximum number of characters (or in some implementations, bytes) that the column can hold. The value n is a positive integer that defines an upper bound on the stored data.

Storage Behavior

VARCHAR values are stored as the actual character data plus a small amount of overhead to record the length of the data. This overhead typically amounts to one or two additional bytes, depending on the database system and the maximum declared length.

When a value shorter than the declared maximum is stored, no padding is added to fill the remaining space, in contrast to CHAR, which right-pads stored values with spaces to reach its fixed length.

Maximum Length by Database System

The maximum length of a VARCHAR column varies among database implementations:

  • Oracle (11g): up to 4,000 bytes.
  • MySQL (5.7): up to 65,535 bytes, subject to the row-size limit of the table.
  • Microsoft SQL Server (2008): up to 8,000 bytes for a standard VARCHAR, with the VARCHAR(MAX) variant supporting up to 2 gigabytes.
  • PostgreSQL: the type VARCHAR is an alias for CHARACTER VARYING, with a maximum length that can be specified up to approximately 10.5 million characters.

ISO Synonyms

The SQL standard defines the ISO synonyms CHAR VARYING and CHARACTER VARYING as equivalent names for the VARCHAR data type. Many database systems accept these alternative forms.

Differences from CHAR

The primary distinction between VARCHAR and CHAR is storage allocation:

  • CHAR uses fixed-length storage; every value occupies the full declared length, with trailing spaces used as padding.
  • VARCHAR uses variable-length storage; each value occupies only as much space as the actual data requires, plus length metadata.

As a general guideline, CHAR is recommended when data entries have consistent sizes, while VARCHAR is preferred when entry sizes vary considerably.

Variants

NVARCHAR is a related data type that stores Unicode character data, allowing for a broader range of international character sets. The choice between VARCHAR and NVARCHAR depends on the character encoding requirements of the application.

VARCHAR(MAX) (in SQL Server) and TEXT (in some other systems) are used for storing very large character strings exceeding the standard VARCHAR limits.

References

  • "The VARCHAR data type," IBM, 2018.
  • "Database Concepts," Oracle documentation.
  • "The CHAR and VARCHAR Types," MySQL Reference Manual.
  • "char and varchar (Transact-SQL)," Microsoft Learn.
  • "Character Types," PostgreSQL Documentation.
Browse

More topics to explore

    Browse all articles