mysql 修改数据表(alter)[转]
时间:2011-01-10 来源:huangfox
为了方便后面的说明,先创建一个表,创建语句如下:
Create Table member(
id Int(3),
name Varchar(8),
pass Varchar(25)
);
1.向表member中增加一个新列email,MySQL语句如下:
Alter Table member Add email Varchar(50) Not Null;
Describe member;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(3) | YES | | NULL | |
| name | varchar(8) | YES | | NULL | |
| pass | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
2.向表member中添加一个新的主键id,MySQL语句如下:
Alter Table member Add Primary Key(id);
Describe member;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(3) | NO | PRI | 0 | |
| name | varchar(8) | YES | | NULL | |
| pass | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3.修改已存在字段id的名字和类型,MySQL语句如下:
Alter Table member Change id mid Int(8) Auto_Increment Unique;
Describe member;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| mid | int(8) | NO | PRI | NULL | auto_increment |
| name | varchar(8) | YES | | NULL | |
| pass | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
说明:修改字段类型时,MySQL中也可以将Change更改为Modify,两者的区别在于:Change要求在修改表时指定旧的的新的字段名,而Modify则只是修改相应字段的类型,但不更改该字段的名字.如下所示:
Alter Table member Change id id Int(8) Auto_Increment Unique;
Alter Table member Modify id Int(8) Auto_Increment Unique;
4.删除字段键pass,MySQL语句如下:
Alter Table member Drop pass;
Describe member;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| mid | int(8) | NO | PRI | NULL | auto_increment |
| name | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
5.将表member重新命名为memb,使用MySQL语句如下:
Alter Table member Rename To memb
6.可以使用Alter Table命令的First和After子句控制字段的位置,在mid字段后增加一个status字段的MySQL语句如下:
Alter Table memb Add status Int(2) After mid;
Describe memb;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| mid | int(8) | NO | PRI | NULL | auto_increment |
| status | int(2) | YES | | NULL | |
| name | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
7.使用Set Default和Drop Default子句为一个字段设置及删除默认值,将status的默认值设为1的MySQL语句如下:
Alter Table memb Alter status Set Default 1;
Describe memb;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| mid | int(8) | NO | PRI | NULL | auto_increment |
| status | int(2) | YES | | 1 | |
| name | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
8.使用Add Foreign Key子句向表添加外键参照.由于前面仅创建了一张表,故无法给出修改后的结果.假设还有一张表book,则可使用以下MySQL语句将book中的字段id作为一个外键加到gememb中:
Alter Table memb Add Foreign Key(bookId) References book(id);
9.在Alter Table命令中包含Add Index或者Drop Index子句添加或者删除索引.
10.在Alter Table命令中指定一个新的Type子句来更改表类型.将表memb改为InnoDB类型的MySQL语句如下:
Alter Table memb Type = InnDB;
11.向一个表添加Unique键,如果这个包含重复的值,则可以通过包含Ignore从句来从表中删除在那个键上具有重复的所有记录,只保留第一条记录.将表memb中的字段email设为Unique,对email重复出现的只保留第一个记录的MySQL语句如下:
Alter Ignore Table memb Modify email Varchar(50) Not Null Unique;
Describe memb:
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| mid | int(8) | NO | PRI | NULL | auto_increment |
| status | int(2) | YES | | 1 | |
| name | varchar(8) | YES | | NULL | |
| email | varchar(50) | NO | UNI | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)