本文匯總了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
希望本文所述示例能夠對大家有所幫助。
Key即鍵值,是關系模型理論中的一部份,比如有主鍵(Primary Key),外鍵(Foreign
Key)等,用於數據完整性檢否與唯一性約束等。而Index則處於實現層面,比如可以對表個的任意列建立索引,那麼當建立索引的列處於SQL語句中的Where條件中時,就可以得到快速的數據定位,從而快速檢索。至於Unique
Index,則只是屬於Index中的一種而已,建立了Unique Index表示此列數據不可重復,猜想MySQL對Unique
Index類型的索引可以做進一步特殊優化吧。
於是乎,在設計表的時候,Key只是要處於模型層面的,而當需要進行查詢優化,則對相關列建立索引即可。
另外,在MySQL中,對於一個Primary Key的列,MySQL已經自動對其建立了Unique Index,無需重復再在上面建立索引了。
用alter table語句:
完整用法:
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...
alter_specification:
table_option ...
| ADD [COLUMN] column_definition [FIRST | AFTER col_name ]
| ADD [COLUMN] (column_definition,...)
| ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
PRIMARY KEY [index_type] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)
| ADD [FULLTEXT|SPATIAL] [INDEX|KEY] [index_name] (index_col_name,...)
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
[reference_definition]
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]
| MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP {INDEX|KEY} index_name
| DROP FOREIGN KEY fk_symbol
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO] new_tbl_name
| ORDER BY col_name [, col_name] ...
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
刪除 主鍵 和外鍵約束:
ALTER TABLE mytablename
DROP PRIMARY KEY
DROP FOREIGN KEY fk_sym......余下全文>>