当前位置: 移动技术网 > IT编程>数据库>Mysql > 详解MySQL中ALTER命令的使用

详解MySQL中ALTER命令的使用

2017年12月12日  | 移动技术网IT编程  | 我要评论

酒圣杜可风,冰封雪灵,生活感触

mysql的alter命令是非常有用的,当想改变表的名称,表的字段,或者如果要添加或删除一个现有的表中的列。

让我们开始创建一个表名为testalter_tbl的用例:

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

丢弃,添加或重新定位字段:

假设要删除一个现有的第i列从上面的mysql表,那么使用drop子句一起使用alter命令如下

mysql> alter table testalter_tbl drop i;

如果该表中剩下唯一的一个字段,drop命令是不起作用的。

要添加一列,使用“添加”add“指定的列定义。下面的语句恢复?列的testalter_tbl

mysql> alter table testalter_tbl add i int;

testalter将包含相同的两列,当第一次创建表不会有相同的结构。这是因为新列添加到默认情况下,该表结束。即使ioriginally是第一列mytbl,现在是最后一条:

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

想指定的表中的一列位置,可以使用第一个它的第一列,,或alter col_name到指示新列应该放在后col_name。请尝试以下alter table语句中,使用show columns后,每个人都有不同的影响:

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


第一和after符只能与add子句。这意味着,如果要重新定位现有的列在一个表中,必须先删除它,然后将它添加在新的位置。
更改列定义或名称:

要更改列的定义,修改或变更条款,连同alter命令。例如,要更改列c从char(1)为char(10),这样做:

mysql> alter table testalter_tbl modify c char(10);
change语法是有点不同。变更后的关键字,要更改的列的名字,然后指定新的定义,其中包括新的名称。试试下面的例子:
mysql> alter table testalter_tbl change i j bigint;

如果现在使用转换j字段的bigint为int而不改变列名,该声明应该是:

mysql> alter table testalter_tbl change j j int;

alter table的影响,null和默认值属性:

当修改或更改列,也可以指定是否该列可以包含null值,它的默认值是什么。事实上,如果不这样做,mysql会自动分配给这些属性的值。

下面是示例默认情况下,not null列的值将是100。

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

如果不使用上面的命令,那么mysql将填补所有的列中的null值。
更改列的默认值:

可以使用alter命令更改任何列的默认值。试试下面的例子。

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

可以从任何一列中删除默认约束通过使用drop子句一起使用alter命令。

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

更改表类型:

可以使用alter命令及type子句一起使用的修改表类型。试试下面的示例中,改变testalter_tbl 为innodb类型。

要找出一个表的当前类型,使用show table status语句。

mysql> alter table testalter_tbl type = innodb;
mysql> show table status like 'testalter_tbl'\g
*************************** 1. row ****************
      name: testalter_tbl
      type: innodb
   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)

重命名表:

要重命名表,使用alter table语句中rename选项。试试下面的例子,重命名testalter_tbl为alter_tbl

mysql> alter table testalter_tbl rename to alter_tbl;

可以使用alter命令来创建和删除索引在mysql文件。在下一章中,我们将看到此功能。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网