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;