【问题标题】:Make unique index on json column在 json 列上创建唯一索引
【发布时间】:2018-10-01 00:39:03
【问题描述】:

如何在mysql中的json类型列上创建唯一索引?例如,我有带有 slug 属性的翻译列。我需要语言中独一无二的 slug translations column image

CREATE TABLE `posts` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(10) unsigned NOT NULL, `root_id` int(10) unsigned NOT NULL, `translations` longtext COLLATE utf8mb4_unicode_ci NOT NULL, `published_from` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `published_till` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `display_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `images` longtext COLLATE utf8mb4_unicode_ci, `deleted_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `posts_user_id_foreign` (`user_id`), KEY `posts_root_id_foreign` (`root_id`), CONSTRAINT `posts_root_id_foreign` FOREIGN KEY (`root_id`) REFERENCES `roots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `posts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `admins` (`id`) ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

【问题讨论】:

  • 提供表结构SHOW CREATE TABLE [table] 更好的示例数据作为 ascii 数据表(不是图像)或提供 sqlfiddle.com 或 db-fiddle.com
  • 更新了我的问题

标签: mysql json indexing unique


【解决方案1】:

JSON 不能替代 RDBMS 的功能。

在插入行时,提取“slug 属性”并将其放入声明为UNIQUE 的单独列中。虽然需要做更多的工作,但从长远来看,它会更清洁、更高效。

对于您经常需要过滤或排序的任何其他列也是如此。 (不要对所有列都这样做。)

【讨论】:

    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 2016-01-15
    • 2017-02-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多