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

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...

Summary of some problems encountered when integrating echarts with vue.js

Preface I'm currently working on the data ana...

Centos7.3 automatically starts or executes specified commands when booting

In centos7, the permissions of the /etc/rc.d/rc.l...

Learn more about MySQL indexes

1. Indexing principle Indexes are used to quickly...

A brief discussion on this.$store.state.xx.xx in Vue

Table of contents Vue this.$store.state.xx.xx Get...

Cross-origin image resource permissions (CORS enabled image)

The HTML specification document introduces the cr...

Example of converting JS one-dimensional array into three-dimensional array

Today I saw a friend asking a question in the Q&a...

MySQL column to row conversion tips (share)

Preface: Because many business tables use design ...

Analysis of the Principle of MySQL Index Length Limit

This article mainly introduces the analysis of th...

Example of pre-rendering method for Vue single page application

Table of contents Preface vue-cli 2.0 version vue...

Detailed explanation of importing/exporting MySQL data in Docker container

Preface We all know that the import and export of...

Detailed tutorial on Docker pulling Oracle 11g image configuration

Without further ado Start recording docker pullin...