MySQL導出一切Index和束縛的辦法。本站提示廣大學習愛好者:(MySQL導出一切Index和束縛的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL導出一切Index和束縛的辦法正文
本文匯總了MySQL導出一切Index 和 束縛的辦法,供給給年夜家以便利年夜家查詢應用。詳細以下:
1. 導出創立自增字段語句:
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. 導出一切索引:
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 ', 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. 創立刪除一切自增字段:
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. 刪除庫一切索引:
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
願望本文所述示例可以或許對年夜家有所贊助。