Mysql dynamically updates the database script example explanation

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows:

Dynamically delete indexes

DROP PROCEDURE IF EXISTS UPGRADE;
DELIMITER $$
CREATE PROCEDURE UPGRADE()
BEGIN
-- RESOURCE.AUDIO_ATTRIBUTE
 IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = 'RESOURCE' AND TABLE_NAME = 'AUDIO_ATTRIBUTE' AND INDEX_NAME = 'resource_publish_resource_id_index')
  THEN 
    ALTER TABLE `AUDIO_ATTRIBUTE` DROP INDEX resource_publish_resource_id_index;
 END IF;
END$$
DELIMITER ;
CALL UPGRADE();
DROP PROCEDURE IF EXISTS UPGRADE;

Dynamically adding fields

DROP PROCEDURE IF EXISTS UPGRADE;
DELIMITER $$
CREATE PROCEDURE UPGRADE()
BEGIN
-- HOMEWORK.HOMEWORK_QUESTION_GROUP.FROM_ID
 IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'HOMEWORK' AND TABLE_NAME = 'HOMEWORK_QUESTION_GROUP' AND COLUMN_NAME = 'FROM_ID')
  THEN 
    ALTER TABLE `HOMEWORK_QUESTION_GROUP` ADD COLUMN FROM_ID VARCHAR(50) NULL;
 END IF;
-- HOMEWORK.HOMEWORK_QUESTION_GROUP.QUESTION_TYPE
 IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'HOMEWORK' AND TABLE_NAME = 'HOMEWORK_QUESTION_GROUP' AND COLUMN_NAME = 'QUESTION_TYPE')
  THEN 
    ALTER TABLE `HOMEWORK_QUESTION_GROUP` ADD COLUMN QUESTION_TYPE VARCHAR(50) NULL;
 END IF;
--HOMEWORK.HOMEWORK_QUESTION_GROUP.DIFFICULTY
 IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'HOMEWORK' AND TABLE_NAME = 'HOMEWORK_QUESTION_GROUP' AND COLUMN_NAME = 'DIFFICULTY')
  THEN 
    ALTER TABLE `HOMEWORK_QUESTION_GROUP` ADD COLUMN DIFFICULTY VARCHAR(50) NULL;
 END IF;
END$$
DELIMITER ;
CALL UPGRADE();
DROP PROCEDURE IF EXISTS UPGRADE;

The other syntaxes are similar, mainly distinguishing between the usage of EXISTS and NOT EXISTS .

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Implementation code for accessing multiple databases through Spring Boot configuration of dynamic data sources
  • Detailed explanation of SpringBoot creating scheduled tasks (dynamic execution with database)
  • Example of MVC cross-database multi-table joint dynamic condition query function implemented in asp.net
  • Tutorial on dynamic SQL query on database in Java's MyBatis framework
  • Yii operation database to realize the method of dynamically obtaining table name
  • How to dynamically create Access database and table in C#
  • ext combobox dynamically loads database data (with front and back ends)
  • Ajax dynamic database loading example
  • c# asp .net method to dynamically create sql database table
  • JavaScript dynamically adds table data rows (ASP background database storage example)

<<:  How to install nginx on win10

>>:  Three examples of nodejs methods to obtain form data

Recommend

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

Detailed Example of CSS3 box-shadow Property

CSS3 -- Adding shadows (using box shadows) CSS3 -...

Introduction to Apache deployment of https in cryptography

Table of contents Purpose Experimental environmen...

Sample code for implementing image drawer effect with CSS3

As usual, let’s first post the picture effect: Th...

Detailed explanation of how to create an array in JavaScript

Table of contents Creating Arrays in JavaScript U...

Discussion on the problem of garbled characters in iframe page parameters

I encountered a very unusual parameter garbled pro...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

Linux kernel device driver character device driver notes

/******************** * Character device driver**...

Summarize the common application problems of XHTML code

<br />For some time, I found that many peopl...

View the command to modify the MySQL table structure

Brief description The editor often encounters som...

Incomplete solution for using input type=text value=str

I encountered a very strange problem today. Look a...

Common ways to optimize Docker image size

The Docker images we usually build are usually la...