数据库中如何更改表的字段?

数据库中如何更改表的字段?,第1张

增加表的字段alter table 表名 add 字段名 列属性

alter table xxx1 add age int(11)

修改表的字段(重命名、修改约束)

alter table xxx1 modify age varchar(11) -- 修改约束

alter table xxx1 change age age1 int(11)  -- 字段重命名

删除表的字段

alter table xxx1 drop age1

MySQL中,如何使用SQL语句来对表中某一个字段进行重命名呢?我们将使用alter table 这一SQL语句。

重命名字段的语法为:alter table <表名>change <字段名><字段新名称><字段的类型>。

现在我们来尝试把test表中的t_name字段重命名为t_name_new字段。

1、首先查看一下当前test表的`结构

mysql>describe test

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

| Field | Type | Null | Key | Default | Extra |

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

| t_id | int(11) | YES | | NULL | |

| t_name | var20) | YES | | NULL | |

| t_password | 32) | YES | | NULL | |

| t_birth | date | YES | | NULL | |

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

4 rows in set (0.00 sec)

2、使用alter table语句来修改字段名称

mysql>alter table test change t_name t_name_new var20)

Query OK, 0 rows affected (0.11 sec)

Records: 0 Duplicates: 0 Warnings: 0

3、查看修改过后的结果

mysql>describe test

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

| Field | Type | Null | Key | Default | Extra |

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

| t_id | int(11) | YES | | NULL | |

| t_name_new | var20) | YES | | NULL | |

| t_password | 32) | YES | | NULL | |

| t_birth | date | YES | | NULL | |

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

4 rows in set (0.00 sec)

至此,我们可以顺利的修改表中字段名称了。

关于MySQL中使用SQL语句对字段进行重命名,本文就介绍这么多,希望对大家有所帮助,谢谢!

在MySQL数据库修改字段名方法:

1、语句:alter table student change physics physisc char(10) not null。

2、其中char(10) not null是你physisc字段的create_definition。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10057620.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存