您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

mysql怎么修改属性名

2024/5/20 4:13:28发布9次查看
在mysql中可以通过alter table命令实现修改属性名,其修改命令语句是“alter table 表名 change 原列名 新列名”,以后执行该语句即可直接修改表的列属性名。
本教程操作环境:windows10系统、mysql5.5版本、dell g3电脑。
mysql怎么修改属性名?
mysql中修改表名,表属性名等的操作
alter table 表名 change 原列名 新列名 类型; --修改表的列属性名alter table 表名 modify 列名 类型 ; --修改表的类类型alter table 表名 drop 列名; --删除表的某一列alter table 表名 add 列名 类型;--添加某一列alter table 表名 rename 新表名; --修改表名
相关拓展介绍:
mysql alter命令
当我们需要修改数据表名或者修改数据表字段时,就需要使用到mysql alter命令。
开始本章教程前让我们先创建一张表,表名为:testalter_tbl。
root@host# mysql -u root -p password;enter password:*******mysql> use runoob;database changedmysql> 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)
删除,添加或修改表字段
如下命令使用了 alter 命令及 drop 子句来删除以上创建表的 i 字段:
mysql> alter table testalter_tbl drop i;
如果数据表中只剩余一个字段则无法使用drop来删除字段。
mysql 中使用 add 子句来向数据表中添加列,如下实例在表 testalter_tbl 中添加 i 字段,并定义数据类型:
mysql> alter table testalter_tbl add i int;
执行以上命令后,i 字段会自动添加到数据表字段的末尾。
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)
如果你需要指定新增字段的位置,可以使用mysql提供的关键字 first (设定位第一列), after 字段名(设定位于某个字段之后)。
尝试以下 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;
first 和 after 关键字可用于 add 与 modify 子句,所以如果你想重置数据表字段的位置就需要先使用 drop 删除字段然后使用 add 来添加字段并设置位置。
修改字段类型及名称
如果需要修改字段类型及名称, 你可以在alter命令中使用 modify 或 change 子句 。
例如,把字段 c 的类型从 char(1) 改为 char(10),可以执行以下命令:
mysql> alter table testalter_tbl modify c char(10);
使用 change 子句, 语法有很大的不同。 在 change 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段名及类型。尝试如下实例:
mysql> alter table testalter_tbl change i j bigint;mysql> alter table testalter_tbl change j j int;
alter table 对 null 值和默认值的影响
当你修改字段时,你可以指定是否包含值或者是否设置默认值。
以下实例,指定字段 j 为 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)
你也可以使用 alter 命令及 drop子句来删除字段的默认值,如下实例:
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)changing a table type:
修改数据表类型,可以使用 alter 命令及 type 子句来完成。尝试以下实例,我们将表 testalter_tbl 的类型修改为 myisam :
注意:查看数据表类型可以使用 show table status 语句。
mysql> alter table testalter_tbl engine = myisam;mysql> show table status like 'testalter_tbl'\g*************************** 1. row **************** name: testalter_tbl type: myisam row_format: fixed rows: 0 avg_row_length: 0 data_length: 0max_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;
推荐学习:《mysql视频教程》
以上就是mysql怎么修改属性名的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录