【www.gdgbn.com--php与数据库】

rename table语法
rename table tbl_name to new_tbl_name    [, tbl_name2 to new_tbl_name2] ...本语句用于对一个或多个表进行重命名。

重命名操作自动进行,这意味着当重命名正在运行时,其它线程不能读取任何表。例如,如果您有一个原有的表old_table,您可以创建另一个具有相同结构的空表new_table,然后用此空表替换原有的表:

create table new_table (...);rename table old_table to backup_table, new_table to old_table;如果此语句用于对多个表进行重命名,则重命名操作从左至右进行。如果您想要交换两个表的名称,您可以这样做(假设不存在名称为tmp_table的表):

rename table old_table to tmp_table,             new_table to old_table,             tmp_table to new_table;只要两个数据库教程位于同一文件系统中,您还可以对表进行重命名,把表从一个数据库中移动到另一个数据库中:

rename table current_db.tbl_name to other_db.tbl_name;当您执行rename时,您不能有被锁定的表,也不能有处于活性状态的事务。您还必须拥有原表的alter和drop权限,以及新表的create和insert权限。

如果mysql教程对多个表进行重命名时遇到了错误,mysql会对所有已被重命名的表进行反向重命名,返回到原来的状态。

只要您不尝试通过重命名把视图加入另一个数据库中,则rename table也可以用于视图


陆上到mysql后,use test改变当前数据库,具体步骤如下所示:

mysql> use test

reading table information for completion of table and column names

you can turn off this feature to get a quicker startup with -a

database changed

mysql> show tables; //查看表,有关show命令的用法本人博客有,请另外参考

+----------------+

| tables_in_test |

+----------------+

| articles       |

| me             |

| mytime         |

+----------------+

3 rows in set (0.01 sec)

mysql> desc mytime;

+-------+-----------+------+-----+-------------------+-------+

| field | type      | null | key | default           | extra |

+-------+-----------+------+-----+-------------------+-------+

| f1    | datetime  | yes  |     | null              |       |

| f2    | timestamp | no   |     | current_timestamp |       |

+-------+-----------+------+-----+-------------------+-------+

2 rows in set (0.02 sec)

mysql>   select * from mytime;

+---------------------+---------------------+

| f1                  | f2                  |

+---------------------+---------------------+

| 2008-12-18 17:32:48 | 2008-12-18 17:32:48 |

| 1234-12-12 11:23:45 | 0000-00-00 00:00:00 |

| 2034-12-12 11:23:45 | 2008-12-18 17:39:37 |

+---------------------+---------------------+

3 rows in set (0.03 sec)

mysql> create table  my  select * from mytime;

query ok, 3 rows affected (0.01 sec)

records: 3  duplicates: 0  warnings: 0

此操作相当于复制表,但也可以说重命名。我们得到了和原来一样的表。如下:

mysql>   select * from my;

+---------------------+---------------------+

| f1                  | f2                  |

+---------------------+---------------------+

| 2008-12-18 17:32:48 | 2008-12-18 17:32:48 |

| 1234-12-12 11:23:45 | 0000-00-00 00:00:00 |

| 2034-12-12 11:23:45 | 2008-12-18 17:39:37 |

+---------------------+---------------------+

3 rows in set (0.00 sec)

此时我们可以删除原来的mytime表

mysql> drop table mytime;

query ok, 0 rows affected (0.00 sec)

我们也可以用专门的sql语句实现。那就是rename命令,如下:

mysql> rename table mytime to my;

query ok, 0 rows affected (0.00 sec)

mysql>   select * from  mytime ;

error 1146 (42s02): table "test.mytime" doesn"t exist

mysql>   select * from  my ;

+---------------------+---------------------+

| f1                  | f2                  |

+---------------------+---------------------+

| 2008-12-18 17:32:48 | 2008-12-18 17:32:48 |

| 1234-12-12 11:23:45 | 0000-00-00 00:00:00 |

| 2034-12-12 11:23:45 | 2008-12-18 17:39:37 |

+---------------------+---------------------+

3 rows in set (0.00 sec)

还有就是alter table 表名1 rename to 表名2 ,如下所示:

mysql> alter table my rename to mytime;

query ok, 0 rows affected (0.01 sec)


一些其实实例语名

##mysql重命名表,建立外键,增、删、改列名实例

##增加到某个字段之后
alter table tb_nippon_mms_info add province varchar(50) default null after retcode;
alter table tb_nippon_mms_info add city varchar(50) default null after province;

##增加到某个字段之前
alter table tb_nippon_mms_info add province varchar(50) default null before retcode;
alter table tb_nippon_mms_info add city varchar(50) default null before province;


##删除名字为states的列
alter table tb_nine_integral_mo_info drop  column states ;

##改变手机号码字段为约束键
alter table business.tb_nine_ticket_popedom change phone  phone  varchar(50)  not null unique;

##改变列名flag为states
alter table tb_nine_integral_mo_info change flag  states tinyint(1);


–重命名表
rename table t_softwareport to software_port;

–建立外键
alter table software_port add constraint fk_software_port_softwareprocessid foreign key (softwareprocessid)
references software_process (id) on delete restrict on update restrict;

 

本文来源:http://www.gdgbn.com/jiaocheng/28409/