sqlcassandraforeign-keysforeign-key-relationshipcql

How to create foreign key in Cassandra CQL


I'm trying to replicate a SQL database in Cassandra, but, while I had no problem creating the tables, I found that I cannot find an example easy to understand that shows how I can create foreign keys in Cassandra.

So, If I have this in SQL:

CREATE TABLE COOP_USUARIO (
 CI                   VARCHAR2 (13 BYTE) NOT NULL ,
 CUENTA               VARCHAR2 (20 BYTE) NOT NULL ,
 NOMBRE               VARCHAR2 (50 BYTE) NOT NULL ,
 EMAIL                VARCHAR2 (255 BYTE) NOT NULL ,
 DIRECCION            VARCHAR2 (255 BYTE) ,
 CIUDAD               NUMBER NOT NULL ,
 TELEFONO             VARCHAR2 (10 BYTE) NOT NULL ,
 TIPO_PERSONA         NUMBER (1) NOT NULL ,
);
 CREATE UNIQUE INDEX COOP_USUARIO_PK ON COOP_USUARIO(
 CI ASC
);
ALTER TABLE COOP_USUARIO ADD CONSTRAINT COOP_USUARIO_PK PRIMARY KEY ( CI ) ;

CREATE TABLE COOP_CIUDADES
(
 ID        NUMBER NOT NULL ,
 NOMBRE    VARCHAR2 (25 BYTE) NOT NULL ,
 PROVINCIA NUMBER NOT NULL
) ;
CREATE UNIQUE INDEX COOP_CIUDADES_PK ON COOP_CIUDADES
(
  ID ASC
);
ALTER TABLE COOP_CIUDADES ADD CONSTRAINT COOP_CIUDADES_PK PRIMARY KEY ( ID ) ;


ALTER TABLE COOP_USUARIO ADD CONSTRAINT COOP_USUARIO_CIUDADES_FK FOREIGN KEY ( CIUDAD ) REFERENCES COOP_CIUDADES ( ID ) ;

What is the Cassndra CQL code for the same purpose?


Solution

  • Simple answer is: There is no CQL code for the same purpose.

    CQL does not have a concept of foreign keys or any concept of constraints between tables in the same way that you can't do joins between tables.

    If you need a constraint between tables then you would need to handle this in code.