如何在mysql中对json类型列建立唯一索引?例如,我有一个带有slug属性的translations列。我需要一个独特的slug之间的语言翻译栏图像 CREATE TABLE
posts(
idint(10) unsigned NOT NULL AUTO_INCREMENT,
user_idint(10) unsigned NOT NULL,
root_idint(10) unsigned NOT NULL,
translationslongtext COLLATE utf8mb4_unicode_ci NOT NULL,
published_fromtimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
published_tilltimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
display_datetimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
imageslongtext COLLATE utf8mb4_unicode_ci,
deleted_attimestamp NULL DEFAULT NULL,
created_attimestamp NULL DEFAULT NULL,
updated_attimestamp NULL DEFAULT NULL, PRIMARY KEY (
id), KEY
posts_user_id_foreign(
user_id), KEY
posts_root_id_foreign(
root_id), CONSTRAINT
posts_root_id_foreignFOREIGN KEY (
root_id) REFERENCES
roots(
id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT
posts_user_id_foreignFOREIGN KEY (
user_id) REFERENCES
admins(
id) ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
1条答案
按热度按时间8tntrjer1#
json不能替代rdbms的特性。
插入行时,提取“slug属性”并将其放入声明为
UNIQUE
. 虽然它需要更多的工作,但从长远来看,它会更干净、更有效率。对于您经常需要筛选或排序的任何其他列,也是如此(不要对所有列都这样做。)