資料庫 Schema API

1 篇文章 / 0 new
author
資料庫 Schema API
hook_schema 用來建立資料表, Schema API 文件
function MNAME_schema() {
    $schema[''] = array( // table name
        'description' => '', //資料表敘述
        'fields' => array( // 欄位集合
            'nid' => array( //欄位名稱
                'description' => '',
                'type' => 'serial', //int, varchar, float, text, datetime, numeric
                'not null' => TRUE,
                //'size' => ,
                'unsigned' => TRUE,
                'default' => 0,
            ),
        ),
        'primary key' => array(''), // array(欄位集)
        'indexes' => array( '' => array(''), ), // 名稱 => array(欄位集)
        'unique keys' => array( '' => array('', ''), ), // 名稱 => array(欄位集)
        'foreign keys' => array(
            '' => array( // 名稱
                'table' => '', //參照 table name
                'columns' => array('' => ''), // 本地欄位 => 參照欄位
            ),
        ),        
    );
    return $schema;
}
hook_update_N : 依版次 N 進行資料表schema異動, 於 example.com/update.php 時被呼叫.
N : 共四碼
第一碼 : drupal 核心主版本, 6.*, 7.*
第二碼 : 模組發行主版本 7.x-1.*, 0 : 安裝模組時
第三, 四碼 : 更新流水號, 系統會依 四碼組成依小到大來執行.
function NNAME_update_7002() {
  db_rename_table('原名稱', '新名稱');
}
hook_schema_alter : 變更已存在資料表 schema
function NNAME_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['node']['fields']['marked'] = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'description' => t('Whether the node has been marked by the markednode module.'),
  );
}
常用欄位型態
Varchar
$field['fieldname'] = array(
   'type' => 'varchar', // Required.
   'length' => 255, // Required.
   'not null' => TRUE, // Defaults to FALSE.
   'default' => 'chocolate', // See below.
   'description' => t('Always state the purpose of your field.'),
); 
Char
$field['fieldname'] = array(
  'type' => 'char',           // Required.
  'length' => 64,             // Required.
  'not null' => TRUE,         // Defaults to FALSE.
  'default' => 'strawberry',  // See below.
  'description' => t('Always state the purpose of your field.'),
);
Text
$field['fieldname'] = array(
  'type' => 'text',   // Required.
  'size' => 'small',  // tiny | small | normal | medium | big
  'not null' => TRUE, // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
Integer
$field['fieldname'] = array(
  'type' => 'int',    // Required.
  'unsigned' => TRUE, // Defaults to FALSE.
  'size' => 'small',  // tiny | small | medium | normal | big
  'not null' => TRUE, // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
Serial
$field['fieldname'] = array(
  'type' => 'serial',  // Required.
  'unsigned' => TRUE,  // Defaults to FALSE. Serial numbers are usually positive.
  'size' => 'small',   // tiny | small | medium | normal | big
  'not null' => TRUE,  // Defaults to FALSE. Typically TRUE for serial fields.
  'description' => t('Always state the purpose of your field.'),
);
Float
$field['fieldname'] = array(
  'type' => 'float',  // Required.
  'unsigned' => TRUE,  // Defaults to FALSE.
  'size' => 'normal',   // tiny | small | medium | normal | big
  'not null' => TRUE, // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
Numeric
$field['fieldname'] = array(
  'type' => 'numeric', // Required.
  'unsigned' => TRUE,  // Defaults to FALSE.
  'precision' => 5,    // Significant digits.
  'scale' => 2,        // Digits to the right of the decimal.
  'not null' => TRUE,  // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
Date and Time: Datetime
$field['fieldname'] = array(
  'type' => 'datetime', // Required.
  'not null' => TRUE,   // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
Binary: Blob
$field['fieldname'] = array(
  'type' => 'blob',   // Required.
  'size' => 'normal'  // normal | big
  'not null' => TRUE, // Defaults to FALSE.
  'description' => t('Always state the purpose of your field.'),
);
特定資料庫型態
$field['fieldname'] = array(
  'mysql_type' > 'TINYBLOB',  // MySQL will use this.
  'type' => 'blob',           // Other databases will use this.
  'size' => 'normal',         // Other databases will use this.
  'not null' => TRUE,
  'description' => t('Wee little blobs.')
);
► 欄位型態 與 相關資料庫 欄位定義之對應
Schema 定義 資料庫
Type 參數(MySQL) size 參數 PostgreSQL SQLite
varchar   varchar varchar
char   char varchar
text tiny TINYTEXT text TEXT
small
medium MEDIUMTEXT text
big LONGTEXT text
  TEXT text
serial tiny TINYINT serial INTEGER
small SMALLINT serial
medium MEDIUMINT serial
big BIGINT bigserial
  INT serial
int tiny TINYINT smallint INTEGER
small SMALLINT smallint
medium MEDIUMINT int
big BIGINT bigint
  INT int
float tiny FLOAT real FLOAT
small
medium
big DOUBLE double precision
  FLOAT real
numeric   DECIMAL numeric NUMERIC
blob big LONGBLOB bytea BLOB
  BLOB bytea
datetime   DATETIME timestamp TIMESTAMP
Free Web Hosting