Adds a new field to a table in Drupal7

db_add_field($table, $field, $spec, $keys_new = array())

function my_module_schema_alter(&$schema) {
  $schema['existing_table']['fields']['new_field'] = array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Field added by my_module',
  );
}

function my_module_install() {
  $schema = drupal_get_schema('existing_table');
  db_add_field('existing_table', 'new_field', $schema['fields']['new_field']);
}

Don't forget to call db_drop_field() in your hook_uninstall.
 

Tags

Comment

In Drupal 8
$spec = array(
'type' => 'varchar',
'description' => "New Col",
'length' => 20,
'not null' => FALSE,
);
$schema = Database::getConnection()->schema();
$schema->addField('existing_table', 'newcol', $spec);