Schema API

  1. drupal
    1. drupal6 database.inc
    2. drupal7

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:

  • 'description': A string describing this table and its purpose. References to other tables should be enclosed in curly-brackets. For example, the node_revisions table description field might contain "Stores per-revision title and body data for each {node}."
  • 'fields': An associative array ('fieldname' => specification) that describes the table's database columns. The specification is also an array. The following specification parameters are defined:
  • 'description': A string describing this field and its purpose. References to other tables should be enclosed in curly-brackets. For example, the node table vid field description might contain "Always holds the largest (most recent) {node_revision}.vid value for this nid."
  • 'type': The generic datatype: 'varchar', 'int', 'serial' 'float', 'numeric', 'text', 'blob' or 'datetime'. Most types just map to the according database engine specific datatypes. Use 'serial' for auto incrementing fields. This will expand to 'int auto_increment' on mysql.
  • 'size': The data size: 'tiny', 'small', 'medium', 'normal', 'big'. This is a hint about the largest value the field will store and determines which of the database engine specific datatypes will be used (e.g. on MySQL, TINYINT vs. INT vs. BIGINT). 'normal', the default, selects the base type (e.g. on MySQL, INT, VARCHAR, BLOB, etc.).

Not all sizes are available for all data types. See db_type_map() for possible combinations.

  • 'not null': If true, no NULL values will be allowed in this database column. Defaults to false.
  • 'default': The field's default value. The PHP type of the value matters: '', '0', and 0 are all different. If you specify '0' as the default value for a type 'int' field it will not work because '0' is a string containing the character "zero", not an integer.
  • 'length': The maximal length of a type 'varchar' or 'text' field. Ignored for other field types.
  • 'unsigned': A boolean indicating whether a type 'int', 'float' and 'numeric' only is signed or unsigned. Defaults to FALSE. Ignored for other field types.
  • 'precision', 'scale': For type 'numeric' fields, indicates the precision (total number of significant digits) and scale (decimal digits right of the decimal point). Both values are mandatory. Ignored for other field types.

All parameters apart from 'type' are optional except that type 'numeric' columns must specify 'precision' and 'scale'.

  • 'primary key': An array of one or more key column specifiers (see below) that form the primary key.
  • 'unique keys': An associative array of unique keys ('keyname' => specification). Each specification is an array of one or more key column specifiers (see below) that form a unique key on the table.
  • 'foreign keys': An associative array, each key references a column of the local table, each value is an array with a single key pair as 'tablename' => 'column' where 'column' is the foreign column to reference.
  • 'indexes': An associative array of indexes ('indexame' => specification). Each specification is an array of one or more key column specifiers (see below) that form an index on the table.

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),
    'uid'      => array('type' => 'int', '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')
  ),
  'foreign keys' => array(
    'uid'  => array('users' => 'uid'),
  )
  'indexes' => array(
    'nid'                 => array('nid'),
    'node_title_type'     => array('title', array('type', 4)),
  ),
);

?>

See also

drupal_install_schema()

Klassen

NameBeschreibung
DatabaseSchema
DatabaseSchema_mysql
DatabaseSchema_pgsql
DatabaseSchema_sqlite

Funktionen & Methoden

NameBeschreibung
addFieldAdd a new field to a table.
addIndexAdd an index.
addPrimaryKeyAdd a primary key.
addUniqueKeyAdd a unique key.
buildTableNameConditionBuild a condition to match a table name against a standard information_schema.
changeFieldChange a field definition.
columnExistsCheck if a column exists in the given table.
createTableCreate a new table from a Drupal table definition.
dropFieldDrop a field.
dropIndexDrop an index.
dropPrimaryKeyDrop the primary key.
dropTableDrop a table.
dropUniqueKeyDrop a unique key.
fieldNamesReturn an array of field names from an array of key/index column specifiers.
fieldSetDefaultSet the default value for a field.
fieldSetNoDefaultSet a field to have no default value.
findTablesFind all tables that are like the specified base table name.
getFieldTypeMapThis maps a generic data type in combination with its data size to the engine-specific data type.
prepareCommentPrepare a table or column comment for database query.
renameTableRename a table.
tableExistsCheck if a table exists.
__construct
addField
addIndex
addPrimaryKey
addUniqueKey
buildTableNameConditionBuild a condition to match a table name against a standard information_schema.
changeField
createFieldSqlCreate an SQL string for a field to be used in table creation or alteration.
createKeySql
createKeysSql
createKeysSqlHelper
createTableSqlGenerate SQL to create a new table from a Drupal schema definition.
dropField
dropIndex
dropPrimaryKey
dropTable
dropUniqueKey
fieldSetDefault
fieldSetNoDefault
getCommentRetrieve a table or column comment.
getFieldTypeMap
prepareComment
processFieldSet database-engine specific properties for a field.
renameTable
addFieldAdd a new field to a table.
addIndexAdd an index.
addPrimaryKeyAdd a primary key.
addUniqueKeyAdd a unique key.
changeFieldChange a field definition.
createFieldSqlCreate an SQL string for a field to be used in table creation or alteration.
createTableSqlGenerate SQL to create a new table from a Drupal schema definition.
dropFieldDrop a field.
dropIndexDrop an index.
dropPrimaryKeyDrop the primary key.
dropTableDrop a table.
dropUniqueKeyDrop a unique key.
fieldSetDefaultSet the default value for a field.
fieldSetNoDefaultSet a field to have no default value.
getCommentRetrieve a table or column comment.
getFieldTypeMapThis maps a generic data type in combination with its data size to the engine-specific data type.
processFieldSet database-engine specific properties for a field.
queryTableInformationFetch the list of blobs and sequences used on a table.
renameTableRename a table.
_createIndexSql
_createKeys
_createKeySql
addFieldAdd a new field to a table.
addIndexAdd an index.
addPrimaryKeyAdd a primary key.
addUniqueKeyAdd a unique key.
alterTableCreate a table with a new schema containing the old content.
changeFieldChange a field definition.
columnExists
createColumsSqlBuild the SQL expression for creating columns.
createFieldSqlCreate an SQL string for a field to be used in table creation or alteration.
createIndexSqlBuild the SQL expression for indexes.
createKeySqlBuild the SQL expression for keys.
createTableSqlGenerate SQL to create a new table from a Drupal schema definition.
dropFieldDrop a field.
dropIndexDrop an index.
dropPrimaryKeyDrop the primary key.
dropTableDrop a table.
dropUniqueKeyDrop a unique key.
fieldSetDefaultSet the default value for a field.
fieldSetNoDefaultSet a field to have no default value.
findTablesFind all tables that are like the specified base table name.
getFieldTypeMapThis maps a generic data type in combination with its data size to the engine-specific data type.
introspectSchemaFind out the schema of a table.
processFieldSet database-engine specific properties for a field.
renameTableRename a table.
tableExists
db_add_fieldAdd a new field to a table.
db_add_indexAdd an index.
db_add_primary_keyAdd a primary key.
db_add_unique_keyAdd a unique key.
db_change_field
db_column_existsCheck if a column exists in the given table.
db_create_tableCreate a new table from a Drupal table definition.
db_drop_fieldDrop a field.
db_drop_indexDrop an index.
db_drop_primary_keyDrop the primary key.
db_drop_tableDrop a table.
db_drop_unique_keyDrop a unique key.
db_field_namesReturn an array of field names from an array of key/index column specifiers.
db_field_set_defaultSet the default value for a field.
db_field_set_no_defaultSet a field to have no default value.
db_find_tablesFind all tables that are like the specified base table name.
db_rename_tableRename a table.
db_table_existsCheck if a table exists.
db_type_mapThis maps a generic data type in combination with its data size to the engine-specific data type.
db_type_placeholderGiven a Schema API field type, return the correct %-placeholder.
drupal_get_schemaGet the schema definition of a table, or the whole database schema.
drupal_get_schema_unprocessedReturns the unprocessed and unaltered version of a module's schema.
drupal_install_schemaCreate all tables that a module defines in its hook_schema().
drupal_schema_fields_sqlRetrieve a list of fields from a table schema. The list is suitable for use in a SQL query.
drupal_uninstall_schemaRemove all tables that a module defines in its hook_schema().
drupal_write_recordSave a record to the database based upon the schema.
_db_create_keys_sql
_drupal_initialize_schemaFill in required default values for table definitions returned by hook_schema().

Properties

NameBeschreibung
protected $connection
/** * A cache of information about blob columns and sequences of tables. * * This is collected by DatabaseConnection_pgsql->queryTableInformation(), * by introspecting the database. * * @see DatabaseConnection_pgsql->queryTableInformation() * @var A cache of information about blob columns and sequences of tables.

Konstanten

NameBeschreibung
Maximum length of a column comment in MySQL.

includes/database/schema.inc, line 9

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen