mysql修改已存在的表增加ID屬性為auto_increment自動增長
今天有需要將已經存在表設置自動增長屬性
具體如下
alter table customers change id id int not null auto_increment primary key;
擴展知識:
//添加字段並設置主鍵
ALTER TABLE tabelname ADD new_field_id int(5) unsigned default 0 not null auto_increment ,ADD primary key (new_field_id);
//加主關鍵字的索引
ALTER TABLE tablename ADD primary key(id);
//加索引 www.2cto.com
ALTER TABLE tablename CHANGE depno depno int(5) not null;
ALTER TABLE tablename ADD INDEX 索引名 (字段名1[,字段名2 …]);
ALTER TABLE tablename ADD INDEX emp_name (name);
//加唯一限制條件的索引
ALTER TABLE tablename ADD UNIQUE emp_name2(cardnumber);
//刪除某個索引
ALTER TABLE tablename DROP INDEX emp_name;
//增加字段:
ALTER TABLE table_name ADD field_name field_type;
//刪除字段
ALTER TABLE table_name DROP field_name;
//重命名列
ALTER TABLE table_name CHANGE field_name1 field_name2 integer;
//調整字段順序
ALTER TABLE `users` CHANGE `user_password` `user_password` varchar( 20 ) NOT NULL AFTER user_name;
//改變列的類型
ALTER TABLE table_name CHANGE field_name field_name bigint not null;
ALTER TABLE infos CHANGE list list tinyint not null default '0';
//修改原字段名稱及類型: www.2cto.com
ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
//重命名表
ALTER TABLE table_name rename new_table_name;
//級聯更新 和 刪除(紅色部分,不區分大小寫 )
DROP TABLE IF EXISTS `mail_model`;create TABLE mail_model(id varchar(50) primary key not null ,mail_filename varchar(200),content varchar(2000))ENGINE=InnoDB DEFAULT CHARSET=gbk;
DROP TABLE IF EXISTS `mail_model_extend`;create TABLE mail_model_extend(id int(6) auto_increment not null primary key,rid varchar(50) not null,content varchar(2000),INDEX (RID),FOREIGN KEY (RID) REFERENCES mail_model(ID) ON DELETE CASCADE ON UPDATE CASCADE)ENGINE=InnoDB DEFAULT CHARSET=gbk;
作者 DouglasLau