添加单列?
? ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name]
?
mysql> show columns from user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | |+----------+-------------+------+-----+---------+----------------+3 rows in setmysql> describe user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | |+----------+-------------+------+-----+---------+----------------+3 rows in set
?添加一列
?
mysql> alter table user add userno varchar(20) not null;Database changedmysql> describe user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | || userno | varchar(20) | NO | | | |+----------+-------------+------+-----+---------+----------------+4 rows in set
? age 到所有列前面
mysql> alter table user add age varchar(2) first;Database changedRecords: 1 Duplicates: 0 Warnings: 0mysql> describe user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| age | varchar(2) | YES | | NULL | || id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | || userno | varchar(20) | NO | | | |+----------+-------------+------+-----+---------+----------------+5 rows in set
? sex 到username后面
?
mysql> alter table user add column sex char(1) after username;Database changedRecords: 1 Duplicates: 0 Warnings: 0mysql> describe user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| age | varchar(2) | YES | | NULL | || id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || sex | char(1) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | || userno | varchar(20) | NO | | | |+----------+-------------+------+-----+---------+----------------+6 rows in set
?? 添加多列 缺点就是只能放在最后面,不能排在某字段的前面或后面
?
mysql> alter table user add column(a1 varchar(1),a2 varchar(2));Database changedRecords: 1 Duplicates: 0 Warnings: 0mysql> describe user;+----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+----------------+| age | varchar(2) | YES | | NULL | || id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | YES | | NULL | || sex | char(1) | YES | | NULL | || pid | smallint(6) | YES | MUL | NULL | || userno | varchar(20) | NO | | | || a1 | varchar(1) | YES | | NULL | || a2 | varchar(2) | YES | | NULL | |+----------+-------------+------+-----+---------+----------------+
?删除一列
?
mysql> alter table user drop a1;Database changedRecords: 1 Duplicates: 0 Warnings: 0
?删除多列用逗号隔开
mysql> alter table user drop sex,drop age;Database changed
修改列
?
mysql> alter table user modify id int after pid;Database changedRecords: 0 Duplicates: 0 Warnings: 0mysql> show columns from user;+----------+---------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+---------------------+------+-----+---------+-------+| username | varchar(20) | YES | | NULL | || pid | smallint(6) | YES | | NULL | || id | int(11) | YES | | NULL | || age | tinyint(3) unsigned | NO | | | |+----------+---------------------+------+-----+---------+-------+4 rows in set
??修改表名用rename?
? 法一?
mysql> alter table user rename deploy;Database changedmysql> show tables;+--------------+| Tables_in_t1 |+--------------+| deploy || dept || pet |+--------------+3 rows in set
?用 rename to
mysql> rename table deploy to user;Database changedmysql> show tables;+--------------+| Tables_in_t1 |+--------------+| dept || pet || user |+--------------+3 rows in set
?
?
?
添加主键约束
??
mysql> create table user( -> id smallInt, -> username varchar(20));Database changedmysql> alter table user add constraint pk_user_id primary key(id);Database changedRecords: 0 Duplicates: 0 Warnings: 0mysql> show create table user;+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+| user | CREATE TABLE `user` ( `id` smallint(6) NOT NULL default '0', `username` varchar(20) default NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 |+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+? 添加唯一约束
?
?
mysql> alter table user add unique(username);Database changedRecords: 0 Duplicates: 0 Warnings: 0mysql> describe user;+----------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| id | smallint(6) | NO | PRI | 0 | || username | varchar(20) | YES | UNI | NULL | |+----------+-------------+------+-----+---------+-------+2 rows in set?添加外键约束
?
??
mysql> alter table user add foreign key(pid) references dept(id);Database changedRecords: 0 Duplicates: 0 Warnings: 0mysql> show columns from user;+----------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| id | smallint(6) | NO | PRI | 0 | || username | varchar(20) | YES | UNI | NULL | || pid | smallint(6) | YES | MUL | NULL | |+----------+-------------+------+-----+---------+-------+3 rows in set?添加默认约束
?