MySQL ALTER 命令

mysql alter 命令

mysql alter table命令用于修改表的結(jié)構(gòu),包括:添加、修改或者刪除表的字段、索引、主鍵,以及修改表的名字,調(diào)整字段順序等等。

我們先創(chuàng)建一張測試表test_tbl,用于測試 alter table 命令的各種用法。

root@host# mysql -u root -p password;
enter password:*******
mysql> use yapf;
database changed
mysql> create table test_tbl
    -> (
    -> i int,
    -> c char(1)
    -> );
query ok, 0 rows affected (0.05 sec)
mysql> show columns from test_tbl;
+-------+---------+------+-----+---------+-------+
| field | type    | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| i     | int(11) | yes  |     | null    |       |
| c     | char(1) | yes  |     | null    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 

1. 刪除、添加或修改表字段

如下命令使用了 alter 命令及 drop 子句來刪除以上創(chuàng)建表的 i 字段:

mysql> alter table test_tbl  drop i;

如果數(shù)據(jù)表中只剩余一個字段則無法使用drop來刪除字段。

mysql 中使用 add 子句來向數(shù)據(jù)表中添加列,如下范例在表 test_tbl 中添加 i 字段,并定義數(shù)據(jù)類型:

mysql> alter table test_tbl add i int;

執(zhí)行以上命令后,i 字段會自動添加到數(shù)據(jù)表字段的末尾。

mysql> show columns from test_tbl;
+-------+---------+------+-----+---------+-------+
| field | type    | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| c     | char(1) | yes  |     | null    |       |
| i     | int(11) | yes  |     | null    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

如果你需要指定新增字段的位置,可以使用mysql提供的關(guān)鍵字 first (設(shè)定位第一列), after 字段名(設(shè)定位于某個字段之后)。

嘗試以下 alter table 語句, 在執(zhí)行成功后,使用 show columns 查看表結(jié)構(gòu)的變化:

alter table test_tbl drop i;
alter table test_tbl add i int first;
alter table test_tbl drop i;
alter table test_tbl add i int after c;

first 和 after 關(guān)鍵字可用于 add 與 modify 子句,所以如果你想重置數(shù)據(jù)表字段的位置就需要先使用 drop 刪除字段然后使用 add 來添加字段并設(shè)置位置。

 

2. 修改字段類型及名稱

如果需要修改字段類型及名稱, 你可以在alter命令中使用 modify 或 change 子句 。

例如,把字段 c 的類型從 char(1) 改為 char(10),可以執(zhí)行以下命令:

mysql> alter table test_tbl modify c char(10);

使用 change 子句, 語法有很大的不同。 在 change 關(guān)鍵字之后,緊跟著的是你要修改的字段名,然后指定新字段名及類型。嘗試如下范例:

mysql> alter table test_tbl change i j bigint;

mysql> alter table test_tbl change j j int;

 

3. alter table 對 null 值和默認(rèn)值的影響

當(dāng)你修改字段時,你可以指定是否包含值或者是否設(shè)置默認(rèn)值。

以下范例,指定字段 j 為 not null 且默認(rèn)值為100 。

mysql> alter table test_tbl 
    -> modify j bigint not null default 100;

如果你不設(shè)置默認(rèn)值,mysql會自動設(shè)置該字段默認(rèn)為 null。

 

4. 修改字段默認(rèn)值

你可以使用 alter 來修改字段的默認(rèn)值,嘗試以下范例:

mysql> alter table test_tbl alter i set default 1000;
mysql> show columns from test_tbl;
+-------+---------+------+-----+---------+-------+
| field | type    | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| c     | char(1) | yes  |     | null    |       |
| i     | int(11) | yes  |     | 1000    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

你也可以使用 alter 命令及 drop子句來刪除字段的默認(rèn)值,如下范例:

mysql> alter table test_tbl alter i drop default;
mysql> show columns from test_tbl;
+-------+---------+------+-----+---------+-------+
| field | type    | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| c     | char(1) | yes  |     | null    |       |
| i     | int(11) | yes  |     | null    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
changing a table type:

修改數(shù)據(jù)表類型,可以使用 alter 命令及 type 子句來完成。嘗試以下范例,我們將表 test_tbl 的類型修改為 myisam :

注意:查看數(shù)據(jù)表類型可以使用 show table status 語句。

mysql> alter table test_tbl engine = myisam;
mysql>  show table status like 'test_tbl'\g
*************************** 1. row ****************
           name: test_tbl
           type: myisam
     row_format: fixed
           rows: 0
 avg_row_length: 0
    data_length: 0
max_data_length: 25769803775
   index_length: 1024
      data_free: 0
 auto_increment: null
    create_time: 2007-06-03 08:04:36
    update_time: 2007-06-03 08:04:36
     check_time: null
 create_options:
        comment:
1 row in set (0.00 sec)

 

5. 修改表名

如果需要修改數(shù)據(jù)表的名稱,可以在 alter table 語句中使用 rename 子句來實現(xiàn)。

嘗試以下范例將數(shù)據(jù)表 test_tbl 重命名為 alter_tbl:

mysql> alter table test_tbl rename to alter_tbl;

下一節(jié):mysql index 索引

mysql 教程

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频