【问题标题】:Can Mysql information_schema not be aligned with show create table?Mysql information_schema 不能和show create table 对齐吗?
【发布时间】:2018-08-28 02:54:43
【问题描述】:

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 可以和实际表不同吗? 我知道它是真实的,因为我试图创建一个密钥,但我得到了它已经存在的异常。

谢谢

【问题讨论】:

  • 尝试查询TABLE_CONSTRAINTS 。我相信KEY_COLUMN_USAGE 显示了解析查询时使用的索引。 “KEY_COLUMN_USAGE 表描述了哪些键列有约束。”它在文档中说不知道这是否是错误的..
  • @RaymondNijland 同样的事情......这不是很奇怪吗?谢谢
  • information_schema 永远不会与其他 SHOW 命令不同步,因为其中许多 SHOW 命令在内部作为针对 information_schema 的查询运行。

标签: mysql indexing information-schema


【解决方案1】:

你应该查询information_schema.statistics

例如

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;

【讨论】:

  • 那行得通。你能解释一下为什么不使用TABLE_CONSTRAINTSKEY_COLUMN_USAGE 表吗?谢谢
  • @Raymond-Nijland 暗示 referential_constraints 是关于外键的,key_column_usage 是关于有约束的列,因此您也可以看到 PK。统计信息告诉你所有的索引(记住 PK 是一个索引,FK 需要一个索引)。
猜你喜欢
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 2022-12-02
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多