Mysql字段操作—增加字段、删除字段、修改字段名、修改字段类型(约束条件)
1.增加字段:
alter table tablename add new_field_id type not null default '0';
例:
alter table mmanapp_mmanmedia add appid_id integer not null default 372;
增加主键:
alter table tabelname add new_field_id type default 0 not nullauto_increment ,add primary key (new_field_id);
增加外键:
在已经存在的字段上增加外键约束
ALTER TABLE yourtablename ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...) REFERENCES tbl_name (index_col_name, ...) [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}] [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
2.删除字段:
alter table tablename drop column colname;
例:
alter table mmanapp_mmanmedia drop column appid_id;
3.修改字段名:
alter table tablename change old_field_name new_field_name old_type;
4.修改字段类型:
alter table tablename change filed_name filed_name new_type;