本节内容:
mysql alter语句用法实例
例句如下:
//增加一个新列
mysql>ALTER TABLE tablename ADD fieldname fieldtype
示例:
ALTER TABLE t2 ADD d TIMESTAMP;
ALTER TABLE infos ADD ex TINYINT NOT NULL DEFAULT '0';
//重命名列
ALTER TABLE t1 CHANGE a b INTEGER;
//删除列
mysql>ALTER TABLE table_name DROP field_name
示例:
ALTER TABLE t2 DROP COLUMN c;
//修改原字段名称及类型
mysql>ALTER TABLE tablename CHANGE old_field_name new_field_name field_type;
//改变列的类型
ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
ALTER TABLE infos CHANGE list list TINYINT NOT NULL DEFAULT '0';
//重命名表
ALTER TABLE t1 RENAME t2;
//主键
ALTER TABLE tablename add new_field_id int(5) UNSIGNED DEFAULT 0 NOT NULL anto_increment,ADD PRIMARY KEY (new_field_id);
//加索引
mysql>ALTER TABLE tablename CHANGE depno depno int(5) NOT NULL;
mysql>ALTER TABLE tablename ADD INDEX 索引名 (字段名1[,字段名2……]);
mysql>ALTER TABLE tablename ADD INDEX emp_name(name);
//加主关键字的搜索
mysql>ALTER TABLE tablename ADD PRIMARY KEY(id);
//加唯一限制条件的索引
mysql>ALTER TABLE tablename ADD UNIQUE temp_name2(cardnumber);
//删除指定的某索引
mysql>ALTER TABLE tablename DROP INDEX temp_name;