Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql uses stored procedures to determine whether a column (field) exists as a tutorial

Shulou Source: shulou.com Published: 2022-06-01 18:34:55 09月28日 Update

Below to bring you mysql through stored procedures to determine whether the column (field) exists tutorial-related content, I believe you must have seen similar articles. What's the difference between what we bring to everyone? Let's take a look at the body. I'm sure you'll get something after reading mysql to determine whether a column (field) exists through a stored procedure.

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 'whether to upload or not'; END IF; END// DELIMITER; CALL schema_change ()

Mysql determines whether the field exists, and if so, modify the field:

DROP PROCEDURE IF EXISTS proc_tempPro;if (@ count > 0) THEN alter table table name change column `old column name ``new column name `varchar (30) comment 'field description'; end if;end;call proc_tempPro;DROP PROCEDURE IF EXISTS proc_tempPro

Determine whether the field exists through the stored procedure. If it does not exist, it will be added:

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 there 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

For the above tutorial on mysql using stored procedures to determine whether a column (field) exists, do you think it is what you want? If you want to know more about it, you can continue to follow our industry information section.

Tags: Fields procedures storage tutorials different above below content articles more sections text industry information parts about Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Redmi MySQL MariaDB