Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL – Find table from constraint name

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading