How to export all indexes and constraints in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces how to export all indexes and constraints from MySQL. it is very useful. For more information about how to export all indexes and constraints from MySQL, see this article, it is provided for your convenience. The details are as follows:

1. export the statement for creating an auto-increment field:

SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'` ','MODIFY COLUMN `',COLUMN_NAME,'` ',IF(UPPER(DATA_TYPE) = 'INT',REPLACE(SUBSTRING_INDEX(UPPER(COLUMN_TYPE),')',1),'INT','INTEGER'),UPPER(COLUMN_TYPE)),') UNSIGNED NOT NULL AUTO_INCREMENT;')FROM information_schema.COLUMNSWHERE TABLE_SCHEMA = 'source_database_name' ANDEXTRA = UPPER('AUTO_INCREMENT')ORDER BY TABLE_NAME ASC

2. export all indexes:

SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'` ', 'ADD ',  IF(NON_UNIQUE = 1, CASE UPPER(INDEX_TYPE) WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX' WHEN 'SPATIAL' THEN 'SPATIAL INDEX' ELSE CONCAT('INDEX `',  INDEX_NAME,  '` USING ',  INDEX_TYPE )END,IF(UPPER(INDEX_NAME) = 'PRIMARY', CONCAT('PRIMARY KEY USING ', INDEX_TYPE ),CONCAT('UNIQUE INDEX `', INDEX_NAME, '` USING ', INDEX_TYPE))),'(', GROUP_CONCAT(DISTINCT CONCAT('`', COLUMN_NAME, '`') ORDER BY SEQ_IN_INDEX ASC SEPARATOR ', '), ');') AS 'Show_Add_Indexes'FROM information_schema.STATISTICSWHERE TABLE_SCHEMA = 'pbq'GROUP BY TABLE_NAME, INDEX_NAMEORDER BY TABLE_NAME ASC, INDEX_NAME ASC

3. create and delete all auto-increment fields:

SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'` ','MODIFY COLUMN `',COLUMN_NAME,'` ',IF(UPPER(DATA_TYPE) = 'INT',REPLACE(SUBSTRING_INDEX(UPPER(COLUMN_TYPE),')',1),'INT','INTEGER'),UPPER(COLUMN_TYPE)),') UNSIGNED NOT NULL;')FROM information_schema.COLUMNSWHERE TABLE_SCHEMA = 'destination_database_name' ANDEXTRA = UPPER('AUTO_INCREMENT')ORDER BY TABLE_NAME ASC

4. delete all indexes in the database:

SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'` ',GROUP_CONCAT(DISTINCTCONCAT('DROP ',IF(UPPER(INDEX_NAME) = 'PRIMARY','PRIMARY KEY',CONCAT('INDEX `', INDEX_NAME, '`')))SEPARATOR ', '),';')FROM information_schema.STATISTICSWHERE TABLE_SCHEMA = 'destination_database_name'GROUP BY TABLE_NAMEORDER BY TABLE_NAME ASC

I hope the examples described in this article will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.