Foreign KeysQuery which tables the primary key of a table is the foreign key ofSELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = 'mydbname' AND REFERENCED_TABLE_NAME = 'Table name'; Export all foreign key statementsSELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' ADD CONSTRAINT ', CONSTRAINT_NAME, ' FOREIGN KEY (', COLUMN_NAME, ') REFERENCES ', REFERENCED_TABLE_NAME, '(', REFERENCED_COLUMN_NAME, ') ON DELETE CASCADE ON UPDATE CASCADE;') FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = 'mydbname' AND REFERENCED_TABLE_NAME IS NOT NULL; Delete all foreign key statementsSELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';') FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = 'mydbname' AND REFERENCED_TABLE_NAME IS NOT NULL; Auto-incrementExport the statement for creating an auto-increment fieldSELECT CONCAT( 'ALTER TABLE `', TABLE_NAME, '` ', 'MODIFY COLUMN `', COLUMN_NAME, '` ', UPPER( COLUMN_TYPE ), ' NOT NULL AUTO_INCREMENT COMMENT "',COLUMN_COMMENT,'";' ) as 'ADD_AUTO_INCREMENT' FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mydbname' AND EXTRA = UPPER( 'AUTO_INCREMENT' ) ORDER BY TABLE_NAME ASC; Create and delete all auto-increment fieldsSELECT CONCAT( 'ALTER TABLE `', TABLE_NAME, '` ', 'MODIFY COLUMN `', COLUMN_NAME, '` ', UPPER( COLUMN_TYPE ), ' NOT NULL;' ) as 'DELETE_AUTO_INCREMENT' FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mydbname' AND EXTRA = UPPER( 'AUTO_INCREMENT' ) ORDER BY TABLE_NAME ASC; indexExport all indexesSELECT 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 ))), CONCAT( '(`', COLUMN_NAME, '`)' ), ';' ) AS 'ADD_ALL_INDEX' FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'mydbname' ORDER BY TABLE_NAME ASC, INDEX_NAME ASC; Delete all indexesSELECT CONCAT( 'ALTER TABLE `', TABLE_NAME, '` ', CONCAT( 'DROP ', IF ( UPPER( INDEX_NAME ) = 'PRIMARY', 'PRIMARY KEY', CONCAT( 'INDEX `', INDEX_NAME, '`' ))), ';' ) AS 'DELETE_ALL_INDEX' FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'mydbname' ORDER BY TABLE_NAME ASC; Data MergeWhen migrating and merging data, the more difficult thing is that the primary keys of different databases are repeated, so we have to modify the primary key values in batches. In order to avoid duplication, we can change the auto-increment number to a string The steps are basically as follows
Be careful when modifying primary key values
for example Deleting Self Constraints ALTER TABLE `t_director` DROP FOREIGN KEY `fk_directorpid`; Modify value update t_director set directorid=directorid+100000000; update t_director set directorid=CONV(directorid,10,36); update t_director set directorpid=directorpid+100000000 WHERE directorpid is not null; update t_director set directorpid=CONV(directorpid,10,36) WHERE directorpid is not null; Adding Self Constraints ALTER TABLE t_director ADD CONSTRAINT fk_directorpid FOREIGN KEY (directorpid) REFERENCES t_director(directorid) ON DELETE CASCADE ON UPDATE CASCADE; Notice
The above is the details of the sample statements for indexes and constraints in Mysql. For more information about MySQL indexes and constraints, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Docker modifies the configuration information of an unstarted container
>>: Some questions about hyperlinks
1. Add PRIMARY KEY (primary key index) mysql>A...
Preface We may have heard of the concept of rowid...
Effect screenshots: Implementation code: Copy code...
EXPLAIN shows how MySQL uses indexes to process s...
When switching users in the docker container, it ...
Directly code: select 'bigint unsigned' a...
Preface Forgotten passwords are a problem we ofte...
Socket option function Function: Methods used to ...
Table of contents 1. Bootstrap Grid Layout 2. Ver...
This article shares the specific code of jQuery t...
CSS scroll bar style modification code .scroll::-...
Copy code The code is as follows: <!DOCTYPE ht...
Phenomenon The system could compile the Linux sys...
ins and del were introduced in HTML 4.0 to help au...
Effect display: Environment preparation controlle...