在mysql字段的操作通常是使用alert来进行如修改,增加,删除,改类型或修改自增ID等等,下面我整理了一些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);
创建复合主键:
- createtabletablename(
- --phpfensi.com
- studentnoint,
- courseidint,
- scoreint,
- primarykey(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;