Mysql 使用存储过程添加新字段

本文介绍如何使用MySQL存储过程来安全地添加或更新数据库表中的字段,包括检查字段是否存在,以及如何进行相应的添加或删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- 1, 注意SQL 语句开始处不要空格
-- 2, 在使用 [--] 进行注释时,后面请加空格
USE `test`;

-- lastUpdateTime
drop procedure if exists schema_change;
delimiter ';;';
create procedure schema_change() begin
if exists (select * from information_schema.columns where table_name = 't_my_table' and column_name = 'lastUpdateTime') then
        alter table t_my_table drop column lastUpdateTime;
end if;
alter table t_my_table add column lastUpdateTime  datetime DEFAULT NULL;
end;;
delimiter ';';
call schema_change();
-- myScore 
drop procedure if exists schema_change;
delimiter ';;';
create procedure schema_change() begin
if exists (select * from information_schema.columns where table_name = 't_my_table' and column_name = 'myScore') then
        alter table t_my_table drop column myScore;
end if;
alter table t_my_table add column myScore int(11) DEFAULT '0';
end;;
delimiter ';';
call schema_change();
drop procedure if exists schema_change;

 

转载于:https://www.cnblogs.com/web1992/p/4812402.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值