MySQL - 从约束名称中查找 table
MySQL - Find table from constraint name
我弹出一个错误,引发如下错误:
IntegrityError: (999, "Duplicate entry 'XXXXX' for key 'constraint_name_here_uniq'")
所以我有了约束名称,有没有一种简单的方法可以找出 table 列在 mysql 命令行中引用的内容?这是一个非常大的数据库,我尝试用 SHOW CREATE TABLE
搜索了几个 table 没有运气,我也尝试了 DESC <constraint name>
但那也没有用。
这应该有效:
select *
from information_schema.KEY_COLUMN_USAGE
where CONSTRAINT_NAME ='constraint_name_here_uniq';
示例:
mysql> use information_schema;
Database changed
mysql> select * from KEY_COLUMN_USAGE where CONSTRAINT_NAME ='user_has_notification_types_user_idx' \G
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: user_id
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*************************** 2. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: notification_type
ORDINAL_POSITION: 2
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
2 rows in set (1.70 sec)
并且 table 使用索引:
mysql> use kanboard;
Database changed
mysql> show create table user_has_notification_types;
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_has_notification_types | CREATE TABLE `user_has_notification_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`notification_type` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_has_notification_types_user_idx` (`user_id`,`notification_type`),
CONSTRAINT `user_has_notification_types_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
我弹出一个错误,引发如下错误:
IntegrityError: (999, "Duplicate entry 'XXXXX' for key 'constraint_name_here_uniq'")
所以我有了约束名称,有没有一种简单的方法可以找出 table 列在 mysql 命令行中引用的内容?这是一个非常大的数据库,我尝试用 SHOW CREATE TABLE
搜索了几个 table 没有运气,我也尝试了 DESC <constraint name>
但那也没有用。
这应该有效:
select *
from information_schema.KEY_COLUMN_USAGE
where CONSTRAINT_NAME ='constraint_name_here_uniq';
示例:
mysql> use information_schema;
Database changed
mysql> select * from KEY_COLUMN_USAGE where CONSTRAINT_NAME ='user_has_notification_types_user_idx' \G
*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: user_id
ORDINAL_POSITION: 1
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
*************************** 2. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: kanboard
CONSTRAINT_NAME: user_has_notification_types_user_idx
TABLE_CATALOG: def
TABLE_SCHEMA: kanboard
TABLE_NAME: user_has_notification_types
COLUMN_NAME: notification_type
ORDINAL_POSITION: 2
POSITION_IN_UNIQUE_CONSTRAINT: NULL
REFERENCED_TABLE_SCHEMA: NULL
REFERENCED_TABLE_NAME: NULL
REFERENCED_COLUMN_NAME: NULL
2 rows in set (1.70 sec)
并且 table 使用索引:
mysql> use kanboard;
Database changed
mysql> show create table user_has_notification_types;
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_has_notification_types | CREATE TABLE `user_has_notification_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`notification_type` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_has_notification_types_user_idx` (`user_id`,`notification_type`),
CONSTRAINT `user_has_notification_types_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)