API to handle database schemas.
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', and type 'varchar' must specify the 'length' parameter.
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':
$schema['node'] = array(
'description' => 'The base table for nodes.',
'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' => '',
),
'language' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'comment' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'promote' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'moderate' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'sticky' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'tnid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'translate' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'node_changed' => array(
'changed',
),
'node_created' => array(
'created',
),
'node_moderate' => array(
'moderate',
),
'node_frontpage' => array(
'promote',
'status',
'sticky',
'created',
),
'node_status_type' => array(
'status',
'type',
'nid',
),
'node_title_type' => array(
'title',
array(
'type',
4,
),
),
'node_type' => array(
array(
'type',
4,
),
),
'uid' => array(
'uid',
),
'tnid' => array(
'tnid',
),
'translate' => array(
'translate',
),
),
'unique keys' => array(
'vid' => array(
'vid',
),
),
'foreign keys' => array(
'node_revision' => array(
'table' => 'node_revision',
'columns' => array(
'vid' => 'vid',
),
),
'node_author' => array(
'table' => 'users',
'columns' => array(
'uid' => 'uid',
),
),
),
'primary key' => array(
'nid',
),
);
Name | Location | Description |
---|---|---|
hook_schema |
drupal/ |
Define the current version of the database schema. |
drupal_get_schema |
drupal/ |
Gets the schema definition of a table, or the whole database schema. |
drupal_get_complete_schema |
drupal/ |
Gets the whole database schema. |
drupal_get_schema_versions |
drupal/ |
Returns an array of available schema versions for a module. |
drupal_get_installed_schema_version |
drupal/ |
Returns the currently installed schema version for a module. |
drupal_set_installed_schema_version |
drupal/ |
Updates the installed version information for a module. |
drupal_install_schema |
drupal/ |
Creates all tables defined in a module's hook_schema(). |
drupal_uninstall_schema |
drupal/ |
Removes all tables defined in a module's hook_schema(). |
drupal_get_schema_unprocessed |
drupal/ |
Returns the unprocessed and unaltered version of a module's schema. |
_drupal_schema_initialize |
drupal/ |
Fills in required default values for table definitions from hook_schema(). |
drupal_schema_fields_sql |
drupal/ |
Retrieves a list of fields from a table schema. |
drupal_write_record |
drupal/ |
Saves (inserts or updates) a record to the database based upon the schema. |
db_create_table |
drupal/ |
Creates a new table from a Drupal table definition. |
db_field_names |
drupal/ |
Returns an array of field names from an array of key/index column specifiers. |
db_index_exists |
drupal/ |
Checks if an index exists in the given table. |
db_table_exists |
drupal/ |
Checks if a table exists. |
db_field_exists |
drupal/ |
Checks if a column exists in the given table. |
db_find_tables |
drupal/ |
Finds all tables that are like the specified base table name. |
_db_create_keys_sql |
drupal/ |
|
db_rename_table |
drupal/ |
Renames a table. |
db_drop_table |
drupal/ |
Drops a table. |
db_add_field |
drupal/ |
Adds a new field to a table. |
db_drop_field |
drupal/ |
Drops a field. |
db_field_set_default |
drupal/ |
Sets the default value for a field. |
db_field_set_no_default |
drupal/ |
Sets a field to have no default value. |
db_add_primary_key |
drupal/ |
Adds a primary key to a database table. |
db_drop_primary_key |
drupal/ |
Drops the primary key of a database table. |
db_add_unique_key |
drupal/ |
Adds a unique key. |
db_drop_unique_key |
drupal/ |
Drops a unique key. |
db_add_index |
drupal/ |
Adds an index. |
db_drop_index |
drupal/ |
Drops an index. |
db_change_field |
drupal/ |
Changes a field definition. |
Name | Location | Description |
---|---|---|
SCHEMA_UNINSTALLED |
drupal/ |
Indicates that a module has not been installed yet. |
SCHEMA_INSTALLED |
drupal/ |
Indicates that a module has been installed. |
Name | Location | Description |
---|---|---|
Schema |
drupal/ |
|
Schema |
drupal/ |
|
Schema |
drupal/ |
|
Schema |
drupal/ |