mysql determines whether a column exists

(*-*)浩
Release: 2019-05-11 09:55:09
Original
4932 people have browsed it

This article will use a stored procedure to determine whether a column (field) exists. Please see details.

Recommended courses:MySQL Tutorial.

mysql determines whether a column exists

Determine whether the field exists:

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();
Copy after login

mysql Determine whether the field exists. If it exists, modify the field:

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;
Copy after login

Determine through stored procedures Whether the field exists, if not, increase:

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;
Copy after login

The above is the detailed content of mysql determines whether a column exists. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!