Home > Software design >  MySQL - Find table from constraint name
MySQL - Find table from constraint name

Time:12-30

I have an error popping up that throws an error like:

IntegrityError: (999, "Duplicate entry 'XXXXX' for key 'constraint_name_here_uniq'")

So I have the constraint name, is there an easy way to find out what table, columns are referenced in the mysql command line? It's a very large database and tried poking around a few tables with SHOW CREATE TABLE with no luck, I also tried DESC <constraint name> but that didn't work either.

CodePudding user response:

This should work:

 select *  
 from  information_schema.KEY_COLUMN_USAGE 
 where CONSTRAINT_NAME ='constraint_name_here_uniq';

Example:

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)

And the table using the index:

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)
  • Related