有没有办法对 dynamodb 中的主键以外的属性(字段)强制执行唯一约束 [英] Is there a way to enforce unique constraint on a property (field) other than the primary key in dynamodb

查看:18
本文介绍了有没有办法对 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 中的主键以外的属性(字段)强制执行唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆