MySQL How to add a column in a table?

本文介绍了如何使用SQL中的ALTERTABLE语句来修改表结构,特别是如何添加新的列到现有表中。提供了详细的语法说明及示例,包括将新列添加到表尾部、指定列之后或作为第一列的方法。

ALTER TABLE statement can change the structure of a table.You can use it to add, modify, or drop/delete columns in a table.

note:To use ALTER TABLE, you need ALTER, CREATE, and INSERT privileges for the table.

syntax

ALTER TABLE tbl_name
[alter_specification [, alter_specification] …]
[partition_options]

Add column in table

syntax


To add a column in a table, the ALTER TABLE syntax in SQL is:

ALTER TABLE tbl_name
ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]

example

ALTER TABLE test ADD test_column VARCHAR(60)

This example will add a column named test_column to the end of the test table.

If you want to insert the new column after a specific column,such as name,use this statement:

ALTER TABLE test ADD test_column VARCHAR(60) AFTER name

If you want the new column to be first, use this statement:

ALTER TABLE test ADD test_column VARCHAR(60) FIRST;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值