7 system.api.php hook_schema()
6 install.php hook_schema()
8 system.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.

The tables declared by this hook will be automatically created when the module is installed, and removed when the module is uninstalled. This happens before hook_install() is invoked, and after hook_uninstall() is invoked, respectively.

By declaring the tables used by your module via an implementation of hook_schema(), these tables will be available 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 Handbook at http://drupal.org/node/146843 for details on schema definition structures.

Return value

array 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.

See also


Related topics

25 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.

ban_schema in core/modules/ban/ban.install
Implements hook_schema().
book_schema in core/modules/book/book.install
Implements hook_schema().
comment_schema in core/modules/comment/comment.install
Implements hook_schema().
database_test_schema in core/modules/system/tests/modules/database_test/database_test.install
Implements hook_schema().
dblog_schema in core/modules/dblog/dblog.install
Implements hook_schema().

... See full list

2 invocations of hook_schema()
drupal_get_complete_schema in core/includes/schema.inc
Gets the whole database schema.
drupal_get_schema_unprocessed in core/includes/schema.inc
Returns the unprocessed and unaltered version of a module's schema.


core/modules/system/system.api.php, line 95
Hooks provided by Drupal core and the System module.


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_field_revision}.vid version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      'type' => array(
        'description' => 'The 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'),
    'foreign keys' => array(
      'node_revision' => array(
        'table' => 'node_field_revision',
        'columns' => array('vid' => 'vid'),
      'node_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
    'primary key' => array('nid'),
  return $schema;


Some of the table definitions have moved to *::schemaDefinition in D8

{node} table is not defined in hook_schema anymore (D8.0.0.beta3). It seems all entity definitions are handled in code (i wonder why not yml?) and the rest is handled in hook_schema().

It seems the node entity and it's table structure is defined in modules/node/src/Entity/Node.php::baseFieldDefinitions(). The way it's defined is very clumsy, which should really be defined in yml. Hope things will change in the final release.