Schema Reference

Last updated on
3 January 2020

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

A Drupal schema definition is an array structure representing one or more tables and their related keys and indexes. A schema is defined by hook_schema(), which must live in the modulename.install file. hook_schema() should return an array mapping 'tablename' => array(table definition) for each table that the module defines. The following keys in the table definition are processed during table creation:

  • 'description': A string describing this table and its purpose. References to other tables should be enclosed in curly brackets. For example, the node_revisions table description field might contain "Stores per-revision title and body data for each {node}."
  • 'fields': An array mapping 'fieldname' => array(field definition) that describes the table's database columns. The specification is also an array. The following specification parameters are defined:
    • 'description': A string describing this field and its purpose. References to other tables should be enclosed in curly brackets. For example, the node table vid field description might contain "Always holds the largest (most recent) {node_revisions}.vid value for this nid."
    • 'type': The generic datatype: 'varchar', 'char', 'int', 'serial', 'float', 'numeric', 'text' or 'blob'. The types map to the underlying database engine specific datatypes. Use 'serial' for auto incrementing fields. Note also the absence of a datetime type, this should be either stored through other datatypes ('varchar', 'int' and so on) or by using the database driver specific types.
    • 'mysql_type', 'pgsql_type', 'sqlite_type', etc: The database driver specific type. For example, 'mysql_type' => 'TIME' is 'pgsql_type' => 'time without time zone'.
    • 'size': The data size: 'tiny', 'small', 'medium', 'normal', 'big'. This is a hint about the largest value the field will store and determines which of the database engine specific datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT). 'normal', the default, selects the base type (e.g. on MySQL, INT, VARCHAR, BLOB, etc.). Data Types and Sizes are explained here.
    • 'not null': If true, no NULL values will be allowed in this database column. Defaults to false.
    • 'default': The field's default value. The PHP type of the value matters: '', '0', and 0 are all different. If you specify '0' as the default value for a type 'int' field it will not work because '0' is a string containing the character "zero", not an integer.

      Note that type 'text' and 'blob' fields cannot have default values.

    • 'length': The maximal length of a type 'char', 'varchar' or 'text' field. Ignored for other field types. Note, length is required for 'varchar's.
    • 'unsigned': A boolean indicating whether a type 'int', 'float' and 'numeric' only is signed or unsigned. Defaults to FALSE. Ignored for other field types.
    • 'precision', 'scale': For type 'numeric' fields, indicates the precision (total number of significant digits) and scale (decimal digits right of the decimal point). Both values are mandatory. Ignored for other field types.
    • 'serialize': A boolean indicating whether the field will be stored as a serialized string.
    • 'binary': A boolean indicating that MySQL should force 'char', 'varchar' or 'text' fields to use case-sensitive binary collation. This has no effect on other database types for which case sensitivity is already the default behavior.

    All parameters apart from 'type' are optional except that type 'numeric' columns must specify 'precision' and 'scale', and 'varchar' columns must specify 'length'.

  • 'primary key': An array of one or more key column specifiers that form the primary key.
    • A key column specifier is either a string naming a field or an array of two elements, a string naming a field and an integer prefix length. If a prefix length is specified, only that many bytes or characters of the named field are used as part of the key. If the database engine does not support this optimization, the prefix length is ignored.
    • All fields listed in the primary key must have 'not null' => TRUE in their specification.
  • 'unique keys': An associative array of unique keys ('keyname' => specification). Each specification is an array of one or more key column specifiers (see above) that form a unique key on the table.
  • 'indexes': An associative array of indexes ('indexname' => specification). Each specification is an array of one or more key column specifiers (see above) that form an index on the table.
  • 'foreign keys': An associative array of foreign keys ('keyname' => specification). Each specification is an array with 'table' and 'columns' elements that form a foreign key for the table.
    'table' is a string specifying the foreign table, and 'columns' is an associative array in the format 'source_column' => 'target_column'. The source_column refers to a column in the table being defined right in this hook_schema implementation, while target_column refers to a column in the foreign table.
    Note: Foreign key definitions were added in Drupal 7 for documentation purposes only, and do not modify the database.
    Example from the {node} table:
       'foreign keys' => array(
         'node_revision' => array(
           'table' => 'node_revision',
           'columns' => array('vid' => 'vid'),
         ),
         'node_author' => array(
           'table' => 'users',
           'columns' => array('uid' => 'uid'),
         ),
       ),
    
  • 'mysql_engine': In MySQL databases, the engine to use instead of the default. Example:
      'mysql_engine' => 'MyISAM',
    
  • 'mysql_character_set': In MySQL databases, the character set to use instead of the default.
  • 'collation': In MySQL databases, the collation to use instead of the default.

Help improve this page

Page status: No known problems

You can: