Question

It could be kind of lame but in cassandra has the primary key to be unique? For example in the following table:

CREATE TABLE users (
  name text,
  surname text,
  age int,
  adress text,
  PRIMARY KEY(name, surname)
);

So if is it possible in my database to have 2 persons in my database with the same name and surname but different ages? Which means same primary key..

Était-ce utile?

La solution

Yes the primary key has to be unique. Otherwise there would be no way to know which row to return when you query with a duplicate key.

In your case you can have 2 rows with the same name or with the same surname but not both.

Autres conseils

By definition, the primary key has to be unique. But that doesn't mean you can't accomplish your goals. You just need to change your approach/terminology.

First of all, if you relax your goal of having the name+surname be a primary key, you can do the following:

CREATE TABLE users ( name text, surname text, age int, address text, PRIMARY KEY((name, surname),age) );
insert into users (name,surname,age,address) values ('name1','surname1',10,'address1');
insert into users (name,surname,age,address) values ('name1','surname1',30,'address2');
select * from users where name='name1' and surname='surname1';

 name  | surname  | age | address
-------+----------+-----+----------
 name1 | surname1 |  10 | address1
 name1 | surname1 |  30 | address2

If, on the other hand, you wanted to ensure that the address is shared as well, then you probably just want to store a collection of ages in the user record. That could be achieved by:

CREATE TABLE users2 ( name text, surname text, age set<int>, address text, PRIMARY KEY(name, surname) );
insert into users2 (name,surname,age,address) values ('name1','surname1',{10,30},'address2');
select * from users2 where name='name1' and surname='surname1';

 name  | surname  | address  | age
-------+----------+----------+----------
 name1 | surname1 | address2 | {10, 30}

So it comes back to what you actually need to accomplish. Hopefully the above examples give you some ideas.

The primary key is unique. With your data model, you can only have one age per (name, surname) combination.

Yes as mentioned in above comments you can have a composite key with name, surname, and age to achieve your goal but still, that won't solve the problem. Rather you can consider adding a new column userID and make that as the primary key. So even in case of name, surname and age duplicate, you don't have to revisit your data model.

CREATE TABLE users (
  userId int,
  name text,
  surname text,
  age int,
  adress text,
  PRIMARY KEY(userid)
);

I would state specifically that partition key should be unique.I could not get it in one place but from the following statements.

  • Cassandra needs all the partition key columns to be able to compute the hash that will allow it to locate the nodes containing the partition.

  • The partition key has a special use in Apache Cassandra beyond showing the uniqueness of the record in the database..

  • Please note that there will not be any error if you insert same partition key again and again as there is no constraint check.

  • Queries that you'll run equality searches on should be in a partition key.

References

https://www.datastax.com/dev/blog/a-deep-look-to-the-cql-where-clause

how Cassandra chooses the coordinator node and the replication nodes?

Insert query replaces rows having same data field in Cassandra clustering column

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top