Warning message

Documentation is currently being migrated into the new system. Some pages might be temporarily missing, and some guides might appear empty. Thank you for your patience while we are improving Drupal.org documentation.

Schema Reference

Last updated on
October 13, 2016 - 19:48

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', 'blob' or 'datetime'. The types map to the underlying database engine specific datatypes. Use 'serial' for auto incrementing fields.
    • '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_suffix': In MySQL databases, a string that is added as a suffix in the CREATE TABLE sentence. Drupal 6 only. In Drupal 7 use mysql_engine’, ‘mysql_character_set‘ and ‘collation’ . Example:
        'mysql_suffix' => " DEFAULT CHARACTER SET UTF8 ENGINE = INNODB AUTO_INCREMENT=3844 ",
    
  • 'mysql_engine': In MySQL databases, the engine to use instead of the default. In Drupal 6, use mysql_suffix instead. Example:
      'mysql_engine' => 'MyISAM',
    
  • 'mysql_character_set': In MySQL databases, the character set to use instead of the default. In Drupal 6, use mysql_suffix instead.
  • 'collation': In MySQL databases, the collation to use instead of the default. In Drupal 6, use mysql_suffix instead.