MySQL向表中添加列方法实例

作者:okokabcd 时间:2024-01-20 19:08:37 

我们使用alter table add column语句向现有表中添加新列。

简介

alter table table_name
add [column] column_name column_definition [first|after existing_column];

说明:

  • alter table子句后指定表名;

  • column关键字是可选的,可以省略它;

  • 可以通过first关键字将新列添加为表的第一列,也可以使用after existing_column子句在现有列之后添加新列,如果没有明确指定会将其添加为最后一列;

若要向表中添加两个或更多列,使用下面语法:

alter table table_name
add [column] column_name column_definition [first|after existing_column],
add [column] column_name column_definition [first|after existing_column],
...;

举例

创建一个表

create database test;
use test;

create table if not exists vendor (
id int auto_increment primary key,
 name varchar(255)
);

添加新列并指定位置

alter table vendor
add column phone varchar(15) after name;

添加新列但不指定新列位置

alter table vendor
add column vendor_group int not null;

插入记录

insert into vendor(name, phone, vendor_group)
values('IBM', '(408)-298-2987', 1);
insert into vendor(name, phone, vendor_group)
values('Microsoft', '(408)-298-2988', 1);

同时添加两列

alter table vendor
add column email varchar(100) not null,
add column hourly_rate decimal(10, 2) not null;

注意:email和hourly_rate两列都是not null,但是vendor表已经有数据了,在这种情况下,MySQL将使用这些新列的默认值。

检查vendor表中的数据

select id, name, phone, vendor_group, email, hourly_rate
from vendor;

查询结果:

+----+-----------+----------------+--------------+-------+-------------+
| id | name      | phone          | vendor_group | email | hourly_rate |
+----+-----------+----------------+--------------+-------+-------------+
|  1 | IBM       | (408)-298-2987 |            1 |       |        0.00 |
|  2 | Microsoft | (408)-298-2988 |            1 |       |        0.00 |
+----+-----------+----------------+--------------+-------+-------------+
2 rows in set (0.00 sec)

email列中填充了空值,而不是NULL值,hourly_rate列填充了0.00

添加表中已存在的列

MySQL将发生错误

alter table vendor
add column vendor_group int not null;

操作结果:

ERROR 1060 (42S21): Duplicate column name 'vendor_group'

检查表中是否已存在列

对于几列的表,很容易看到哪些列已经存在,如果有一个饮食数百列的大表,那就比较费劲了

select if(count(*) = 1, 'Exist', 'Not Exist') as result
from information_schema.columns
where table_schema = 'test'
and table_name = 'vendor'
and column_name = 'phone';

查询结果:

+--------+
| result |
+--------+
| Exist  |
+--------+
1 row in set (0.00 sec)

在where子句中,我们传递了三个参数:表模式或数据库,表名和列名。我们使用if函数来返回列是否存在。

在表中已有字段后添加列名

alter table 表名 add column 列名 类型 after 已有列名 comment '注释信息';
alter table students add column class varchar(40) after student_name   comment '学生班级';

删除列

alter table 表名 drop column 列名;
alter table students drop column grade;

参考

https://www.begtut.com/mysql/mysql-add-column.html

来源:https://blog.csdn.net/ln_ydc/article/details/127560296

标签:mysql,表,添加列
0
投稿

猜你喜欢

  • PHP+jQuery+Ajax实现多图片上传效果

    2024-05-22 10:05:59
  • mysql update语句的用法详解

    2024-01-22 13:12:46
  • 浅谈php自定义错误日志

    2023-11-14 10:59:40
  • ASP使用MYSQL数据库全攻略

    2009-11-08 18:27:00
  • python 递归遍历文件夹,并打印满足条件的文件路径实例

    2023-08-07 07:22:48
  • 在登录触发器错误情况下连接SQL Server的方法

    2024-01-25 19:37:51
  • 浅谈Python脚本开头及导包注释自动添加方法

    2021-04-22 06:21:22
  • python subprocess pipe 实时输出日志的操作

    2022-10-07 00:39:51
  • PyTorch中model.zero_grad()和optimizer.zero_grad()用法

    2023-09-15 20:13:57
  • python中csv文件的若干读写方法小结

    2021-04-07 11:46:03
  • 分析语音数据增强及python实现

    2021-02-02 07:25:43
  • Python编程生成随机用户名及密码的方法示例

    2021-12-14 13:12:17
  • 新Orcas语言特性:扩展方法

    2007-09-23 12:49:00
  • Python进行数据提取的方法总结

    2022-06-23 18:02:45
  • ACCESS转SQL Server2000需要注意的问题

    2007-11-18 15:25:00
  • 在Python的setuptools框架下生成egg的教程

    2021-12-06 13:46:33
  • 网站防止采集方法全攻略

    2007-09-05 19:57:00
  • python去除字符串中的空格、特殊字符和指定字符的三种方法

    2022-06-12 20:21:31
  • 利用numba让python速度提升百倍

    2022-06-12 14:31:02
  • Python基于TCP实现会聊天的小机器人功能示例

    2022-11-08 13:03:33
  • asp之家 网络编程 m.aspxhome.com