如何在MySQL中更改列名

If you already created your MySQL database, and you decide after the fact that one of the columns is named incorrectly, you don't need to remove it and add a replacement; you can simply rename it.

如果您已经创建了MySQL数据库,并且在确定其中一列名称不正确之后做出决定,则无需删除它并添加替换项; 您可以简单地重命名它。

重命名数据库列 ( Renaming a Database Column )

You rename a column in MySQL using the ALTER TABLE and CHANGE commands together to change an existing column. For example, say the column is currently named Soda, but you decide that Beverage is a more appropriate title. The column is located on the table entitled Menu. Here is an example of how to change it:

您可以同时使用ALTER TABLE和CHANGE命令在MySQL中重命名一列,以更改现有列。 例如,假设该列当前命名为Soda ,但您认为Beverage是更合适的标题。 该列位于标题为Menu的表上 。 这是一个如何更改它的示例:


ALTER TABLE menu CHANGE soda beverage varchar(10) ;

In a generic form, where you substitute your terms, this is:

在通用形式中,用您替换术语的方式是:


ALTER TABLE tablename CHANGE oldname newname varchar(10) ;

关于VARCHAR ( About VARCHAR )

The VARCHAR(10) in the examples can change to be appropriate for your column. VARCHAR is a character string of variable length. The maximum length—in this example it is 10—indicates the maximum number of characters you want to store in the column. VARCHAR(25) could store up to 25 characters.

示例中的VARCHAR(10)可以更改为适合您的列。 VARCHAR是可变长度的字符串。 最大长度(在此示例中为10)表示要存储在列中的最大字符数。 VARCHAR(25)最多可以存储25个字符。

ALTER TABLE的其他用途 ( Other Uses for ALTER TABLE )

The ALTER TABLE command can also be used to add a new column to a table or to remove an entire column and all its data from a table. For example, to add a column use:

ALTER TABLE命令还可以用于向表中添加新列或从表中删除整个列及其所有数据。 例如,要添加列,请使用:


ALTER TABLE table_name
ADD column_name datatype

To delete a column, use:

要删除列,请使用:


ALTER TABLE table_name
DROP COLUMN column_name 

You can also make changes to a column's size and type in MySQL.

您还可以在MySQL中更改列的大小和类型

翻译自: https://www.thoughtco.com/change-column-name-in-mysql-2693874

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值