MySQL导出所有Index和约束的方法

时间:2021-05-24

本文汇总了MySQL导出所有Index 和 约束的方法,提供给大家以方便大家查询使用。具体如下:

1. 导出创建自增字段语句:

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. 导出所有索引:

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. 创建删除所有自增字段:

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. 删除库所有索引:

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

希望本文所述示例能够对大家有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章