Mysql中索引和約束的示例語句

外鍵

查詢一個表的主鍵是哪些表的外鍵

SELECT
	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 = '表名';

導出所有外鍵語句

SELECT
	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;

刪除所有外鍵語句

SELECT
	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;

自增

導出創建自增字段的語句

SELECT
	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;

創建刪除所有自增字段

SELECT
	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;

索引

導出所有索引

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 ))),
		CONCAT( '(`', COLUMN_NAME, '`)' ),
		';' 
) AS 'ADD_ALL_INDEX' 
FROM
	information_schema.STATISTICS 
WHERE
	TABLE_SCHEMA = 'mydbname' 
ORDER BY
	TABLE_NAME ASC,
	INDEX_NAME ASC;

刪除所有索引

SELECT
	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;

數據合並

在數據遷移合並的時候,比較棘手的是不同數據庫主鍵重復,那麼我們就要批量修改主鍵的值,為瞭避免重復我們可以把自增的數字改為字符串

步驟基本上有以下幾步

  • 取消主鍵自增
  • 刪除所有外鍵
  • 修改主鍵字段為varchar
  • 添加所有外鍵
  • 修改主鍵的值
  • 合並數據

修改主鍵值的時候要註意

如果包含id和pid這種自關聯的情況下是不能直接修改值的,就需要先刪除約束再添加。

比如

刪除自約束

ALTER TABLE `t_director` DROP FOREIGN KEY `fk_directorpid`;

修改值

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;

添加自約束

ALTER TABLE t_director ADD CONSTRAINT fk_directorpid FOREIGN KEY (directorpid) REFERENCES t_director(directorid) ON DELETE CASCADE ON UPDATE CASCADE;

註意

CONV(directorpid,10,36)後兩個參數為原數字進制和要轉換後的進制。

第一個參數隻要內容是數字就算類型為varchar也可以轉換。

以上就是Mysql中索引和約束的示例語句的詳細內容,更多關於MySQL 索引和約束的資料請關註WalkonNet其它相關文章!

推薦閱讀: