【mysql动态添加字段】MySQL动态添删改列字段命令

更新时间:2019-08-11    来源:mysql教程    手机版     字体:

【www.bbyears.com--mysql教程】

MySQL如何动态添删改列字段呢,SQL如下:

动态增加列字段:

ALERT TABLE table1 add transactor varchar(10) not Null;

动态删除列字段:

ALERT TABLE TableName drop column field_id;

动态修改列字段:

ALERT TABLE table_name change old_field_name  new_field_name field_type;

动态修改表结构

ALERT TABLE table_name MODIFY field_name field_type

创建表格后添加: alter table tablename add id int auto_increment primary key

设置自增字段初始值:alter table tablename  auto_increment =x ;

设置主键:alter table tablename add primary key(field_name);

创建复合主键:create table tablename (
                                  studentno int,
                                  courseid int,
                                  score int,
                                  primary key (studentno,courseid) );


设置复合主键:alter table tablename add primary key (列1,列2,列3);

重命名表: alter table table_old_name rename table_new_name;

改变字段的类型:alter table tableName modify field_name field_type;

重命名字段:alter table tableName change old_field_name new_field_name new_field_type;

删除字段:alter table tableName drop column field_name;

增加一个新字段:alter table tableName add new_field_name field_type;  

新增一个字段,默认值为0,非空:alter table tableName add new_field_name field_type not null default "0";

新增一个字段,默认值为0,非空,自增,主键:alter table tabelname add new_field_name field_type default 0 not null   auto_increment ,add primary key (new_field_name); 

修改字段类型:

alter table tablename change filed_name filed_name new_type;

 

本文来源:http://www.bbyears.com/shujuku/61547.html

热门标签

更多>>

本类排行