Mysql export all index and constraint methods _mysql

Source: Internet
Author: User

This article summarizes the MySQL export all index and constraints of the method, provided to everyone to facilitate the query use. Specifically as follows:

1. Export Create a self-added field statement:

SELECT
CONCAT (
' 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. COLUMNS
WHERE table_schema = ' source_database_name ' and
EXTRA = UPPER (' auto_increment ') Order by
Table_ NAME ASC

2. Export All indexes:

SELECT
CONCAT (' 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 ', C14/>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. STATISTICS
WHERE table_schema = ' PBq '
GROUP by table_name, index_name order
by table_name ASC, index_name Asc

3. Create Delete all self-added fields:

SELECT
CONCAT (
' 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. COLUMNS
WHERE table_schema = ' destination_database_name ' and
EXTRA = UPPER (' auto_increment ') Order by
TABLE_NAME ASC

4. Delete all indexes for the library:

SELECT
CONCAT (
' ALTER TABLE ',
table_name,
', ',
group_concat (
DISTINCT
CONCAT (
' DROP ',
IF (UPPER (index_name) = ' PRIMARY ',
' PRIMARY KEY ', CONCAT
(' INDEX ', index_name, ' "))
SEPARATOR ', ')
, '
;
') From INFORMATION_SCHEMA. STATISTICS
WHERE table_schema = ' destination_database_name '
GROUP by table_name ORDER BY
TABLE_NAME ASC

I hope the examples described in this article will help you.

Related Article

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.