mysql字段是否存在_mysql判断列是否存在
本篇文章將通過存儲過程來判斷列(字段)是否存在。請看詳情。
推薦課程: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() BEGIN
IF NOT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='component' AND COLUMN_NAME='PRINT_CHECK_STATUS') THEN
ALTER 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') THEN
ALTER 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') THEN
ALTER 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;
總結(jié)
以上是生活随笔為你收集整理的mysql字段是否存在_mysql判断列是否存在的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阐述oracle的体系结构,Oracle
- 下一篇: linux启动过程中内核拷贝,轻松识破l