Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Database/database.api.php \hook_schema()
  2. 7.x modules/system/system.api.php \hook_schema()
  3. 8.9.x core/lib/Drupal/Core/Database/database.api.php \hook_schema()
  4. 9 core/lib/Drupal/Core/Database/database.api.php \hook_schema()

Define the current version of the database schema.

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 your module's .install file.

By implementing hook_schema() and specifying the tables your module declares, you can easily create and drop these tables on all supported database engines. You don't have to deal with the different SQL dialects for table creation and alteration of the supported database engines.

See the Schema API documentation at http://drupal.org/node/146843 for details on hook_schema(), where database tables are defined.

Return value

A schema definition structure array. For each element of the array, the key is a table name and the value is a table structure definition.

Related topics

26 functions implement hook_schema()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_schema in modules/aggregator/aggregator.install
Implementation of hook_schema().
block_schema in modules/block/block.install
Implementation of hook_schema().
blogapi_schema in modules/blogapi/blogapi.install
Implementation of hook_schema().
book_schema in modules/book/book.install
Implementation of hook_schema().
comment_schema in modules/comment/comment.install
Implementation of hook_schema().

... See full list

File

developer/hooks/install.php, line 140
Documentation for the installation and update system.

Code

function hook_schema() {
  $schema['node'] = array(
    // example (partial) specification for table "node"
    'description' => 'The base table for nodes.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The current {node_revisions}.vid version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The {node_type} of this node.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'node_changed' => array(
        'changed',
      ),
      'node_created' => array(
        'created',
      ),
    ),
    'unique keys' => array(
      'nid_vid' => array(
        'nid',
        'vid',
      ),
      'vid' => array(
        'vid',
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  return $schema;
}