Varchar

Last updated on
8 February 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

The varchar, or variable length character field, is the most frequently used field type for storing text less than 256 characters in length.

  

Sample code:
$field['fieldname'] = array(
  'type' => 'varchar',
  'length' => 255, //
  'not null' => FALSE, // TRUE or FALSE.
  'default' => 'some default value',
  'description' => t('field fieldname of table tablename.'),
)

Note: The length that you set here indicates the maximum number of characters that you want to store.The MySQL's VARCHAR type is declared with a length that indicates the maximum number of characters you want to store. For example, VARCHARCHAR(30) can hold up to 30 characters.

  

MySQL Field Mapping in the schema.inc file:
  'varchar:normal'  => 'VARCHAR'
  
Max Characters Allowed:
MySQL Version Max Characters Allowed
MySQL 5.0.2 and earlier 255 characters
MySQL 5.0.3 and later 65,535 characters

  

Storage Information :

L + 1 bytes if column values require 0 − 255 bytes, L + 2 bytes if values may require more than 255 bytes where L is L is the byte length of the string that you are storing and not the maximum size that you have set.

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

Help improve this page

Page status: No known problems

You can: