Constraints

Hello guys!

I hope you guys are doing well today. I am new to NEO4j and need some suggestion/advice.

I successfully imported 3 different CSV files as A,B,C and now trying to create CONSTRAINT for all.

From my RDBMS, "reg_uid" (integer) is the primary key for all three tables (A,B,C).

If A,B,C has reg_uid on NEO4j, how do I make reg_uid as CONSTRAINT for A,B, and C?

Thank you so much.

Hello plee,
nice to meet you.

This is the reference you'll need, from the official docs: Neo4j's Cypher 4.0 Docs - Constraints

The correct syntax should be something like that, if I've understand well what you need:

CREATE CONSTRAINT ON (a:A) ASSERT a.reg_uid IS UNIQUE;
CREATE CONSTRAINT ON (b:B) ASSERT b.reg_uid IS UNIQUE;
CREATE CONSTRAINT ON (c:C) ASSERT c.reg_uid IS UNIQUE;

This create constraint without a name, but you can create named constraint

CREATE CONSTRAINT uniqueA ON (a:A) ASSERT a.reg_uid IS UNIQUE;
CREATE CONSTRAINT uniqueB ON (b:B) ASSERT b.reg_uid IS UNIQUE;
CREATE CONSTRAINT uniqueC ON (c:C) ASSERT c.reg_uid IS UNIQUE;

If you're using Enterprise Edition, maybe you want to make reg_uid a NODE KEY, so that a node of this type could not be created without it. This is the way for that:

CREATE CONSTRAINT nodeA ON (a:A) ASSERT a.reg_uid IS NODE KEY;
CREATE CONSTRAINT nodeB ON (b:B) ASSERT b.reg_uid IS NODE KEY;
CREATE CONSTRAINT nodeC ON (c:C) ASSERT c.reg_uid IS NODE KEY;

If you want to drop them, this is the way:

DROP CONSTRAINT uniqueA;
DROP CONSTRAINT uniqueB;
DROP CONSTRAINT uniqueC;
DROP CONSTRAINT nodeA;
DROP CONSTRAINT nodeB;
DROP CONSTRAINT nodeC;

Hope this will help you!

1 Like