有没有办法对 dynamodb 中的主键以外的属性(字段)强制执行唯一约束

编程入门 行业动态 更新时间:2024-10-10 06:21:44
本文介绍了有没有办法对 dynamodb 中的主键以外的属性(字段)强制执行唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 dynamodb 中,如果您想在主键以外的字段中强制执行唯一性(例如您有一个用户表并希望用户的电子邮件地址唯一,而主键是一个用户 ID,它是一个数字)有没有办法除了扫描表格以查看电子邮件是否已被使用?

In dynamodb, if you want to enforce uniqueness in a field other than the primary key (like were you have a users table and want unique email addresses for users while primary key is a userid which is a number) is there a way other thans scanning the table to see if the email is already in use?

推荐答案

简短回答:否.

DynamoDB 是一个键:值存储.它非常擅长快速检索/保存项目,因为它做了一些妥协.这是您必须自己处理的约束.

DynamoDB is a key:value store. It is very good at quickly retrieving/saving Items because it does a couple of compromise. This is a constraint you have to handle yourself.

尽管如此,根据您的实际模型,在您hash_key 时使用此字段或考虑使用range_key

Nonethess, depending on your actual model, it might be a good idea to use this field as you hash_key or consider using a range_key

如果这是不可能的,我建议您对数据进行反规范化.您目前有以下情况:

If this is not possible, I advise you to de-normalize your data. You currently have something like:

UserTable

  • hash_key:user_id
  • 电子邮件
  • ...

为确保唯一性,请使用此架构添加一个新表:

To ensure unicity, add a new table with this schema:

EmailUser

  • hash_key:电子邮件
  • user_id

要确保电子邮件是唯一的,只需在之前向 EmailUser 发出 GetItem.

To make sure an e-mail is unique, just issue a GetItem to EmailUser before.

这种反规范化在 No-SQL 数据库中很常见.

This kind of de-normalization is quite common with No-SQL databases.

更多推荐

有没有办法对 dynamodb 中的主键以外的属性(字段)强制执行唯一约束

本文发布于:2023-11-12 02:21:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   字段   强制执行   主键   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!