Login

Drupal 7 Automatic Database Table Installation

Drupal 7 automatically adds database tables described with hook_schema. In our example below, even though we don't have an implicit hook_install or hook_uninstall method defined, Drupal adds the "my_color" database table when our module is enabled, and drops the "my_color" database table when our module is un-installed.

<?php
function my_color_schema() {
  $schema['my_color'] = array(
    'description' => 'A proof of concept table for the myColor module.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a color.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
 
      'name' => array(
        'description' => 'name of color.',
        'type' => 'varchar',
        'length' => 80,
        'not null' => TRUE,
        'default' => ''),
     ),
 
     'primary key' => array('id'));
 
     return $schema;
}
?>