0

If I get it correctly, this only enforces an existence constraint, not a uniqueness:

CREATE CONSTRAINT ON (c:City)
ASSERT exists (c.name);

To make it unique, I would use a unique property constraint using the following syntax:

CREATE CONSTRAINT ON (c:City)
ASSERT c.name IS UNIQUE

Can I somehow make it unique and enforce existence at the same time?

If I run:

CREATE CONSTRAINT ON (c:City)
ASSERT exists (c.name) IS UNIQUE;

I get an error:

Query failed: line 2:24 mismatched input 'IS' expecting {<EOF>, ';'}
KWriter
  • 1,024
  • 4
  • 22
  • 1
    You could try creating 2 separate constraints (one UNIQUE, and one EXISTS). I am not sure if memgraph allows that. – cybersam Mar 22 '23 at 22:15

1 Answers1

1

I can't comment due to my low karma, but @cybersam is correct. If you want to make it clear that you're enforcing both existence and uniqueness, you can use a combination of ASSERT EXISTS and ASSERT c.property IS UNIQUE. This syntax is not supported by Cypher in a single statement.

To achieve both constraints, you will need to create two separate queries. One for existence and one for uniqueness.

Frist query:

CREATE CONSTRAINT ON (c:City)
ASSERT EXISTS (c.name);

Second query:

CREATE CONSTRAINT ON (c:City)
ASSERT c.name IS UNIQUE;
Moraltox
  • 537
  • 1
  • 7