information_schema似乎缺少数据。 show create table
与information_schema不同。
当我跑:SHOW CREATE TABLE devicoserver.email_templates;
我得到4把钥匙:
CREATE TABLE `email_templates` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`subject` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`language_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `email_templates_language_id_foreign` (`language_id`),
KEY `email_templates_email_type_unique` (`email_type`),
KEY `email_templates_subject_unique` (`subject`),
CONSTRAINT `email_templates_language_id_foreign` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
但是,当我查询信息模式时,它表明只有2个键
select
CONSTRAINT_NAME,COLUMN_NAME FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = 'email_templates' and CONSTRAINT_SCHEMA = 'devicoserver';
PRIMARY
,id
email_templates_language_id_foreign
,language_id
这是否有意义,information_schema可以与actuall表不同吗? 我知道它的实际情况,因为我试图创建一个密钥,并且我已经存在它的异常。
由于
答案 0 :(得分:3)
您应该查询information_schema
。statistics
e.g。
SELECT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX)
FROM `information_schema`.`statistics`
WHERE table_schema = 'dbname'
AND TABLE_NAME = 'tablename'
GROUP BY INDEX_NAME
ORDER BY INDEX_NAME ASC;