您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql判断列是否存在

2024/4/11 0:56:20发布14次查看
本篇文章将通过存储过程来判断列(字段)是否存在。请看详情。
推荐课程:mysql教程。
判断字段是否存在:
drop procedure if exists schema_change;  delimiter //create procedure schema_change() begin declare  currentdatabase varchar(100);select database() into currentdatabase;if not exists (select * from information_schema.columns where table_schema=currentdatabase and table_name = 'rtc_order' and column_name = 'ifupsend') then      alter table rtc_order    add column `ifupsend` bit  not null  default 0 comment '是否上传 是否上传';end if;  end//  delimiter ;  call schema_change();
mysql 判断字段否存在,如果存在就修改字段:
drop procedure if exists proc_temppro;if(@count>0) then  alter table 表名 change column `旧列名` `新列名` varchar(30) comment '字段说明';end if;end;call proc_temppro;drop procedure if exists proc_temppro;
通过存储过程判断字段是否存在,不存在则增加:
drop procedure if exists pro_addcolumn;create procedure pro_addcolumn() beginif not exists(select 1 from information_schema.columns where table_name='component' and column_name='print_check_status') thenalter table component add print_check_status int(10) default 0;end if;if not exists(select 1 from information_schema.columns where table_name='component' and column_name='print_check_time') thenalter table component add print_check_time datetime null;end if;if not exists(select 1 from information_schema.columns where table_schema=podcloud and table_name='component' and column_name='print_check_back_reason') thenalter table component add print_check_back_reason varchar(500) default null;end if;end;call pro_addcolumn;drop procedure pro_addcolumn;------------------------------------------------------------------------------------------------drop procedure if exists pro_addindex;   delimiter; create procedure pro_addindex() begin if not exists (select * from information_schema.statistics where table_schema=currentdatabase and table_name = 'rtc_phototype' and index_name = 'index_name') then       alter table `rtc_phototype` add index index_name ( `imgtype` );  end if;   end; delimiter;    call pro_addindex(); drop procedure pro_addindex;
以上就是mysql判断列是否存在的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product