hook_schema()

We use hook_schema() to define the current version of the database schema.

During the module installation process in Drupal, you can create DB tables using .sql query or you can use hook_schema() to create tables and fields. 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 be located in your module’s .install file.

By implementing hook_schema() and specifying the tables your module declares, you can create and drop DB 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.

Parameters

No parameters for this function

Return value

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

Usage sample:

function page_title_schema() {
    $schema['page_title'] = array(
    'fields' => array(
    'type'       => array('type' => 'varchar', 'length' => 15,  'not null' => TRUE, 'default' => 'node'),
    'id'         => array('type' => 'int', 'unsigned' => TRUE,  'not null' => TRUE, 'default' => 0),
    'page_title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')
    ),
    'primary key' => array('type', 'id'),
    );
    return $schema;
}

If you have multiple table, add another $schema array. Below is an example of multiple tables installed during your installation process.

function module_schema(){
$schema= array();
$schema["table1"] = array(...); // table1 schema

$schema["table2"] = array(...);// table 2 schem
etc.
return $schema;
}

to install both tables, you just use hook_install to install schema

function module_install(){
drupal_install_schema('module');
// where module is name of your module
}