Chetankumar Kaila
Chetankumar Kaila

Reputation: 319

How to create unique key using cassandra database

I am beginner of cassandra DB I want to create unique key like oracle in cassandra.

I searched a lot site but not able to get relevant answer.

is it possible to create unique key using cassandra ?

Upvotes: 3

Views: 1363

Answers (1)

Luke Tillman
Luke Tillman

Reputation: 1385

In Cassandra, the PRIMARY KEY definition of your table is used for uniqueness. For example:

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

Here, the userid column is the unique identifier. You can, of course, have multiple columns as part of your PRIMARY KEY definition as well. But a few things to keep in mind:

  • Primary key columns have other implications in Cassandra as well (beyond uniqueness). You'll want to read up on Partition Keys and Clustering Columns and how Cassandra uses it to organize data around the cluster and on disk.
  • Cassandra doesn't have or enforce constraints (for example, no foreign keys)
  • Cassandra doesn't do a read before a write (unless you're using the Lightweight Transactions feature), and so doing an INSERT or an UPDATE are functionally equivalent (i.e. an "upsert") and will overwrite data that already exists

If you're looking for a feature like a "unique constraint" or "unique index" in Oracle, you won't find it in Cassandra. There's a simple data modeling example available in the CQL docs and I also recommend checking out the data modeling course it links to if you're just getting started with Cassandra. Good luck!

Upvotes: 3

Related Questions