先創建一個表,創建語句如下:
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 | |
+-------+-------------+------+-----+---------+----------------+
另一類:只改字段的屬性
ALTER TABLE tableName MODIFY cloumnName DATETIME;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子句添加或者刪除索引.
MySQL>ALTER TABLE table_name ADD field_name field_type;
MySQL>ALTER TABLE table_name DROP field_name;
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)
---------------------------------------------------------------------------------------------------------------------------------------------
"mysql中常用的修改表的命令"是由電腦編程網整理,請尊重作者權益,轉載注明出處;收藏本文請按ctrl+D;