如何使用cassandra数据库创建唯一键

How to create unique key using cassandra database

我是 cassandra DB 的初学者,我想在 cassandra 中创建像 oracle 一样的唯一密钥。

我搜索了很多网站,但没有得到相关的答案。

是否可以使用 cassandra 创建唯一密钥?

在 Cassandra 中,table 的 PRIMARY KEY 定义用于唯一性。例如:

CREATE TABLE users (
  userid uuid,
  firstname text,
  lastname text,
  email text,
  created_date timestamp,
  PRIMARY KEY (userid)
);

此处,userid 列是唯一标识符。当然,您也可以将多个列作为 PRIMARY KEY 定义的一部分。但有几点需要牢记:

  • 主键列在 Cassandra 中还有其他含义(除了唯一性之外)。您需要阅读分区键和集群列以及 Cassandra 如何使用它来围绕集群和磁盘组织数据。
  • Cassandra 没有或没有强制约束(例如,没有外键)
  • Cassandra 不会在写入之前进行读取(除非您使用的是轻量级事务功能),因此执行 INSERTUPDATE 在功能上是等效的(即 "upsert") 并且会覆盖已经存在的数据

如果您正在寻找 Oracle 中的 "unique constraint" 或 "unique index" 之类的功能,您将不会在 Cassandra 中找到它。 the CQL docs 中提供了一个简单的数据建模示例,如果您刚开始使用 Cassandra,我还建议您查看它链接到的数据建模课程。祝你好运!