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 usually lives in a modulename.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.
hook_schema() should return an array with a key for each table that the module defines.
The following keys are defined:
All parameters apart from 'type' are optional except that type 'numeric' columns must specify 'precision' and 'scale'.
A key column specifier is either a string naming a column or an array of two elements, column name and length, specifying a prefix of the named column.
As an example, here is a SUBSET of the schema definition for Drupal's 'node' table. It show four fields (nid, vid, type, and title), the primary key on field 'nid', a unique key named 'vid' on field 'vid', and two indexes, one named 'nid' on field 'nid' and one named 'node_title_type' on the field 'title' and the first four bytes of the field 'type':
<?php
$schema['node'] = array(
'fields' => array(
'nid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
'title' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''),
),
'primary key' => array('nid'),
'unique keys' => array(
'vid' => array('vid')
),
'indexes' => array(
'nid' => array('nid'),
'node_title_type' => array('title', array('type', 4)),
),
);
?>
| Name | Description |
|---|---|
| db_add_field | Add a new field to a table. |
| db_add_field | Add a new field to a table. |
| db_add_index | Add an index. |
| db_add_index | Add an index. |
| db_add_primary_key | Add a primary key. |
| db_add_primary_key | Add a primary key. |
| db_add_unique_key | Add a unique key. |
| db_add_unique_key | Add a unique key. |
| db_change_field | Change a field definition. |
| db_change_field | |
| db_create_table | Create a new table from a Drupal table definition. |
| db_create_table_sql | Generate SQL to create a new table from a Drupal schema definition. |
| db_create_table_sql | Generate SQL to create a new table from a Drupal schema definition. |
| db_drop_field | Drop a field. |
| db_drop_field | Drop a field. |
| db_drop_index | Drop an index. |
| db_drop_index | Drop an index. |
| db_drop_primary_key | Drop the primary key. |
| db_drop_primary_key | Drop the primary key. |
| db_drop_table | Drop a table. |
| db_drop_table | Drop a table. |
| db_drop_unique_key | Drop a unique key. |
| db_drop_unique_key | Drop a unique key. |
| db_field_names | Return an array of field names from an array of key/index column specifiers. |
| db_field_set_default | Set the default value for a field. |
| db_field_set_default | Set the default value for a field. |
| db_field_set_no_default | Set a field to have no default value. |
| db_field_set_no_default | Set a field to have no default value. |
| db_last_insert_id | Returns the last insert id. |
| db_rename_table | Rename a table. |
| db_rename_table | Rename a table. |
| db_type_map | This maps a generic data type in combination with its data size to the engine-specific data type. |
| db_type_map | This maps a generic data type in combination with its data size to the engine-specific data type. |
| db_type_placeholder | Given a Schema API field type, return the correct %-placeholder. |
| drupal_get_schema | Get the schema definition of a table, or the whole database schema. |
| drupal_get_schema_unprocessed | Returns the unprocessed and unaltered version of a module's schema. |
| drupal_install_schema | Create all tables that a module defines in its hook_schema(). |
| drupal_schema_fields_sql | Retrieve a list of fields from a table schema. The list is suitable for use in a SQL query. |
| drupal_uninstall_schema | Remove all tables that a module defines in its hook_schema(). |
| drupal_write_record | Save a record to the database based upon the schema. |
| _db_create_field_sql | Create an SQL string for a field to be used in table creation or alteration. |
| _db_create_field_sql | Create an SQL string for a field to be used in table creation or alteration. |
| _db_create_index_sql | |
| _db_create_keys | |
| _db_create_keys_sql | |
| _db_create_key_sql | |
| _db_create_key_sql | |
| _db_process_field | Set database-engine specific properties for a field. |
| _db_process_field | Set database-engine specific properties for a field. |
| _drupal_initialize_schema | Fill in required default values for table definitions returned by hook_schema(). |