mysql alter语句用法实例_MySQL
程序员文章站
2023-12-28 21:18:46
...
bitsCN.com
//主键
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 ADD fieldname fieldtype
如:
ALTER TABLE t2 ADD d TIMESTAMP;
ALTER TABLE infos ADD ex TINYINT NOT NULL DEFAULT '0';
//删除列
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 a b INTEGER;
//改变列的类型
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;
//加索引
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 emp_name2(cardnumber);
//删除某个索引
mysql>ALTER TABLE tablename DROP INDEX emp_name;bitsCN.com
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 ADD fieldname fieldtype
如:
ALTER TABLE t2 ADD d TIMESTAMP;
ALTER TABLE infos ADD ex TINYINT NOT NULL DEFAULT '0';
//删除列
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 a b INTEGER;
//改变列的类型
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;
//加索引
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 emp_name2(cardnumber);
//删除某个索引
mysql>ALTER TABLE tablename DROP INDEX emp_name;bitsCN.com