Home > Database > Mysql Tutorial > body text

mysql alter语句用法实例

WBOY
Release: 2016-06-07 17:55:18
Original
1079 people have browsed it

这里简单分享几个mysql alter语句用法,方便大家使用

//主键
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;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template