diff --git a/modules/ROOT/pages/access-control/database-administration.adoc b/modules/ROOT/pages/access-control/database-administration.adoc index e44d30f3c..3f4e6447e 100644 --- a/modules/ROOT/pages/access-control/database-administration.adoc +++ b/modules/ROOT/pages/access-control/database-administration.adoc @@ -681,8 +681,6 @@ For example, to grant the role `regularUsers` the ability to create indexes on t GRANT CREATE INDEX ON DATABASE neo4j TO regularUsers ---- -The `SHOW INDEXES` privilege only affects the xref::indexes-for-search-performance.adoc#administration-indexes-list-indexes[`SHOW INDEXES` command], and not the older procedures for listing indexes, such as `db.indexes`. - [[access-control-database-administration-constraints]] == The `CONSTRAINT MANAGEMENT` privileges @@ -743,8 +741,6 @@ For example, to grant the role `regularUsers` the ability to create constraints GRANT CREATE CONSTRAINT ON DATABASE neo4j TO regularUsers ---- -The `SHOW CONSTRAINTS` privilege only affects the xref::constraints/syntax.adoc#administration-constraints-syntax-list[`SHOW CONSTRAINTS` command], and not the older procedures for listing constraints, such as `db.constraints`. - [[access-control-database-administration-tokens]] == The `NAME MANAGEMENT` privileges diff --git a/modules/ROOT/pages/clauses/listing-procedures.adoc b/modules/ROOT/pages/clauses/listing-procedures.adoc index 5be57ef6d..f3540f541 100644 --- a/modules/ROOT/pages/clauses/listing-procedures.adoc +++ b/modules/ROOT/pages/clauses/listing-procedures.adoc @@ -213,7 +213,7 @@ SHOW PROCEDURES 4+d|Rows: 15 |=== -The above table only displays the first 15 results of the query. +The above table only displays the first 15 results of the query. For a full list of all built-in procedures in Neo4j, visit the {neo4j-docs-base-uri}/operations-manual/{page-version}/reference/procedures#/#_list_of_procedures[Operations Manual -> List of procedures]. == Listing procedures with filtering on output columns @@ -252,7 +252,7 @@ WHERE admin 2+d|Rows: 15 |=== -The above table only displays the first 15 results of the query. +The above table only displays the first 15 results of the query. For a full list of all procedures which require `admin` privileges in Neo4j, visit the {neo4j-docs-base-uri}/operations-manual/{page-version}/reference/procedures#/#_list_of_procedures[Operations Manual -> List of procedures]. == Listing procedures with other filtering @@ -353,7 +353,7 @@ SHOW PROCEDURES EXECUTABLE BY CURRENT USER YIELD * 4+d|Rows: 15 |=== -The above table only displays the first 15 results of the query. +The above table only displays the first 15 results of the query. Note that the two `roles` columns are empty due to missing the xref::access-control/dbms-administration.adoc#access-control-dbms-administration-role-management[`SHOW ROLE`] privilege. Also note that the following columns are not present in the table: `mode`, `worksOnSystem`, `signature`, `argumentDescription`, `returnDescription`, `admin`, and `options`. diff --git a/modules/ROOT/pages/clauses/merge.adoc b/modules/ROOT/pages/clauses/merge.adoc index 5a5f4d0e1..751bb34f7 100644 --- a/modules/ROOT/pages/clauses/merge.adoc +++ b/modules/ROOT/pages/clauses/merge.adoc @@ -26,11 +26,11 @@ Either the pattern already exists, or it needs to be created. ** xref::clauses/merge.adoc#merge-merge-on-an-undirected-relationship[Merge on an undirected relationship] ** xref::clauses/merge.adoc#merge-merge-on-a-relationship-between-two-existing-nodes[Merge on a relationship between two existing nodes] ** xref::clauses/merge.adoc#merge-merge-on-a-relationship-between-an-existing-node-and-a-merged-node-derived-from-a-node-property[Merge on a relationship between an existing node and a merged node derived from a node property] -* xref::clauses/merge.adoc#query-merge-using-unique-constraints[Using unique constraints with `MERGE`] -** xref::clauses/merge.adoc#merge-merge-using-unique-constraints-creates-a-new-node-if-no-node-is-found[Merge using unique constraints creates a new node if no node is found] -** xref::clauses/merge.adoc#merge-merge-using-unique-constraints-matches-an-existing-node[Merge using unique constraints matches an existing node] -** xref::clauses/merge.adoc#merge-merge-with-unique-constraints-and-partial-matches[Merge with unique constraints and partial matches] -** xref::clauses/merge.adoc#merge-merge-with-unique-constraints-and-conflicting-matches[Merge with unique constraints and conflicting matches] +* xref::clauses/merge.adoc#query-merge-using-unique-constraints[Using property uniqueness constraints with `MERGE`] +** xref::clauses/merge.adoc#merge-merge-using-unique-constraints-creates-a-new-node-if-no-node-is-found[Merge using property uniqueness constraints creates a new node if no node is found] +** xref::clauses/merge.adoc#merge-merge-using-unique-constraints-matches-an-existing-node[Merge using property uniqueness constraints matches an existing node] +** xref::clauses/merge.adoc#merge-merge-with-unique-constraints-and-partial-matches[Merge with property uniqueness constraints and partial matches] +** xref::clauses/merge.adoc#merge-merge-with-unique-constraints-and-conflicting-matches[Merge with property uniqueness constraints and conflicting matches] * xref::clauses/merge.adoc#merge-using-map-parameters-with-merge[Using map parameters with `MERGE`] [[query-merge-introduction]] @@ -55,8 +55,8 @@ If partial matches are needed, this can be accomplished by splitting a pattern u [IMPORTANT] ==== Under concurrent updates, `MERGE` only guarantees existence of the `MERGE` pattern, but not uniqueness. -To guarantee uniqueness of nodes with certain properties, a xref::constraints/index.adoc[unique constraint] should be used. -See xref::clauses/merge.adoc#query-merge-using-unique-constraints[Using unique constraints with `MERGE`] to see how `MERGE` can be used in combination with a unique constraint. +To guarantee uniqueness of nodes with certain properties, a xref::constraints/index.adoc[property uniqueness constraint] should be used. +See xref::clauses/merge.adoc#query-merge-using-unique-constraints[Using property uniqueness constraints with `MERGE`] to see how `MERGE` can be used in combination with a property uniqueness constraint. ==== As with `MATCH`, `MERGE` can match multiple occurrences of a pattern. @@ -489,15 +489,15 @@ Labels added: 5 [[query-merge-using-unique-constraints]] -== Using unique constraints with `MERGE` +== Using property uniqueness constraints with `MERGE` -Cypher prevents getting conflicting results from `MERGE` when using patterns that involve unique constraints. +Cypher prevents getting conflicting results from `MERGE` when using patterns that involve property uniqueness constraints. In this case, there must be at most one node that matches that pattern. -For example, given two unique constraints on `:Person(id)` and `:Person(ssn)`, a query such as `MERGE (n:Person {id: 12, ssn: 437})` will fail, if there are two different nodes (one with `id` 12 and one with `ssn` 437) or if there is only one node with only one of the properties. +For example, given two property uniqueness constraints on `:Person(id)` and `:Person(ssn)`, a query such as `MERGE (n:Person {id: 12, ssn: 437})` will fail, if there are two different nodes (one with `id` 12 and one with `ssn` 437), or if there is only one node with only one of the properties. In other words, there must be exactly one node that matches the pattern, or no matching nodes. -Note that the following examples assume the existence of unique constraints that have been created using: +Note that the following examples assume the existence of property uniqueness constraints that have been created using: [source, cypher, indent=0] ---- @@ -507,9 +507,9 @@ CREATE CONSTRAINT FOR (n:Person) REQUIRE n.role IS UNIQUE; [[merge-merge-using-unique-constraints-creates-a-new-node-if-no-node-is-found]] -=== Merge using unique constraints creates a new node if no node is found +=== Merge using property uniqueness constraints creates a new node if no node is found -Merge using unique constraints creates a new node if no node is found. +Merge using property uniqueness constraints creates a new node if no node is found. .Query [source, cypher, indent=0] @@ -534,9 +534,9 @@ Labels added: 1 [[merge-merge-using-unique-constraints-matches-an-existing-node]] -=== Merge using unique constraints matches an existing node +=== Merge using property uniqueness constraints matches an existing node -Merge using unique constraints matches an existing node. +Merge using property uniqueness constraints matches an existing node. .Query [source, cypher, indent=0] @@ -557,9 +557,9 @@ The `'oliver'` node already exists, so `MERGE` just matches it. [[merge-merge-with-unique-constraints-and-partial-matches]] -=== Merge with unique constraints and partial matches +=== Merge with property uniqueness constraints and partial matches -Merge using unique constraints fails when finding partial matches. +Merge using property uniqueness constraints fails when finding partial matches. .Query [source, cypher, indent=0] @@ -588,9 +588,9 @@ SET michael.role = 'Gordon Gekko' [[merge-merge-with-unique-constraints-and-conflicting-matches]] -=== Merge with unique constraints and conflicting matches +=== Merge with property uniqueness constraints and conflicting matches -Merge using unique constraints fails when finding conflicting matches. +Merge using property uniqueness constraints fails when finding conflicting matches. .Query [source, cypher, indent=0] diff --git a/modules/ROOT/pages/constraints/examples.adoc b/modules/ROOT/pages/constraints/examples.adoc index 74fed31b0..0a440f566 100644 --- a/modules/ROOT/pages/constraints/examples.adoc +++ b/modules/ROOT/pages/constraints/examples.adoc @@ -1,6 +1,6 @@ :description: Examples of how to manage constraints used for ensuring data integrity. -[[administration-constraints-examples]] +[[constraints-examples]] = Examples [abstract] @@ -9,25 +9,26 @@ Examples of how to manage constraints used for ensuring data integrity. -- -[[administration-constraints-unique-nodes]] -== Unique node property constraints +[[constraints-examples-node-uniqueness]] +== Node property uniqueness constraints -* xref::constraints/examples.adoc#administration-constraints-create-a-unique-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-unique-constraint-only-if-it-does-not-already-exist[] -* xref::constraints/examples.adoc#administration-constraints-create-a-unique-constraint-with-specified-index-provider[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-an-already-existing-unique-property-constraint[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-unique-property-constraint-on-same-schema-as-existing-index[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-complies-with-unique-property-constraints[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-violates-a-unique-property-constraint[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-unique-property-constraint-due-to-conflicting-nodes[] +A node property uniqueness constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique when existing. + +* xref::constraints/examples.adoc#constraints-create-a-node-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-uniqueness-constraint-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-a-node-uniqueness-constraint-with-index-provider[] +* xref::constraints/examples.adoc#constraints-create-an-already-existing-node-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-uniqueness-constraint-on-same-schema-as-existing-index[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-complies-with-a-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-violates-a-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-uniqueness-constraint-due-to-conflicting-nodes[] [discrete] -[[administration-constraints-create-a-unique-constraint]] -=== Create a unique constraint +[[constraints-create-a-node-uniqueness-constraint]] +=== Create a node property uniqueness constraint -When creating a unique constraint, a name can be provided. -The constraint ensures that your database will never contain more than one node with a specific label and one property value. +When creating a property uniqueness constraint, a name can be provided. .+CREATE CONSTRAINT+ @@ -49,20 +50,20 @@ FOR (book:Book) REQUIRE book.isbn IS UNIQUE Unique constraints added: 1 ---- +[NOTE] +==== +The statistics will be updated to say `Node uniqueness constraints` in Neo4j version 6.0. +==== + ====== [discrete] -[[administration-constraints-create-a-unique-constraint-only-if-it-does-not-already-exist]] -=== Create a unique constraint only if it does not already exist - -If it is not known whether a constraint exists or not, add `IF NOT EXISTS` to ensure it does. -The uniqueness constraint ensures that your database will never contain more than one node with a specific label and one property value. +[[constraints-create-a-node-uniqueness-constraint-if-not-exist]] +=== Handling existing constraints when creating a constraint -[NOTE] -==== -No constraint will be created if any other constraint with the given name or another uniqueness constraint on the same schema already exists. -==== +Creating an already existing constraint will fail. To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node property uniqueness constraint on the same schema already exists. .+CREATE CONSTRAINT+ @@ -75,7 +76,7 @@ CREATE CONSTRAINT constraint_name IF NOT EXISTS FOR (book:Book) REQUIRE book.isbn IS UNIQUE ---- -Assuming no constraint with the given name or other uniqueness constraint on the same schema exists: +Assuming no constraint with the given name or other node property uniqueness constraint on the same schema exists: .Result [queryresult] @@ -86,18 +87,23 @@ Assuming no constraint with the given name or other uniqueness constraint on the Unique constraints added: 1 ---- +[NOTE] +==== +The statistics will be updated to say `Node uniqueness constraints` in Neo4j version 6.0. +==== + ====== [discrete] -[[administration-constraints-create-a-unique-constraint-with-specified-index-provider]] -=== Create a unique constraint with specified index provider +[[constraints-create-a-node-uniqueness-constraint-with-index-provider]] +=== Specifying an index provider when creating a constraint -To create a unique constraint with a specific index provider for the backing index, the `OPTIONS` clause is used. +To create a property uniqueness constraint with a specific index provider for the backing index, the `OPTIONS` clause is used. The index type of the backing index is set with the `indexProvider` option. -Valid values for the index provider are: +The only valid value for the index provider is: * `range-1.0` label:default[] @@ -126,20 +132,28 @@ OPTIONS { Unique constraints added: 1 ---- +[NOTE] +==== +The statistics will be updated to say `Node uniqueness constraints` in Neo4j version 6.0. +==== + ====== +There is no valid index configuration values for the constraint-backing range indexes. + [discrete] -[[administration-constraints-failure-to-create-an-already-existing-unique-property-constraint]] -=== Failure to create an already existing unique property constraint +[[constraints-create-an-already-existing-node-uniqueness-constraint]] +=== Creating an already existing constraint will fail .+CREATE CONSTRAINT+ ====== -Create a unique property constraint on the property `title` on nodes with the `Book` label, when that constraint already exists. +Create a property uniqueness constraint on the property `title` on nodes with the `Book` label, when that constraint already exists. //// +Set-up to get expected behavior: CREATE CONSTRAINT preExistingUnique FOR (book:Book) REQUIRE book.title IS UNIQUE //// @@ -158,20 +172,26 @@ Constraint already exists: Constraint( id=4, name='preExistingUnique', type='UNIQUENESS', schema=(:Book {title}), ownedIndex=3 ) ---- +[NOTE] +==== +The constraint type will be updated to say `NODE_UNIQUENESS` in Neo4j version 6.0. +==== + ====== [discrete] -[[administration-constraints-failure-to-create-a-unique-property-constraint-on-same-schema-as-existing-index]] -=== Failure to create a unique property constraint on same schema as existing index +[[constraints-create-a-node-uniqueness-constraint-on-same-schema-as-existing-index]] +=== Creating a constraint on the same schema as an existing index will fail .+CREATE CONSTRAINT+ ====== -Create a unique property constraint on the property `wordCount` on nodes with the `Book` label, when an index already exists on that label and property combination. +Create a property uniqueness constraint on the property `wordCount` on nodes with the `Book` label, when an index already exists on that label and property combination. //// +Set-up to get expected behavior: CREATE INDEX FOR (book:Book) ON (book.wordCount) //// @@ -194,8 +214,8 @@ A constraint cannot be created until the index has been dropped. [discrete] -[[administration-constraints-create-a-node-that-complies-with-unique-property-constraints]] -=== Create a node that complies with unique property constraints +[[constraints-create-a-node-that-complies-with-a-uniqueness-constraint]] +=== Creating a node that complies with an existing constraint .+CREATE NODE+ @@ -204,6 +224,7 @@ A constraint cannot be created until the index has been dropped. Create a `Book` node with an `isbn` that is not already in the database. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS UNIQUE //// @@ -228,8 +249,8 @@ Labels added: 1 [discrete] -[[administration-constraints-create-a-node-that-violates-a-unique-property-constraint]] -=== Create a node that violates a unique property constraint +[[constraints-create-a-node-that-violates-a-uniqueness-constraint]] +=== Creating a node that violates an existing constraint will fail .+CREATE NODE+ @@ -238,6 +259,7 @@ Labels added: 1 Create a `Book` node with an `isbn` that is already used in the database. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS UNIQUE CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'}) //// @@ -260,16 +282,17 @@ Node(0) already exists with label `Book` and property `isbn` = '1449356265' [discrete] -[[administration-constraints-failure-to-create-a-unique-property-constraint-due-to-conflicting-nodes]] -=== Failure to create a unique property constraint due to conflicting nodes +[[constraints-fail-to-create-a-uniqueness-constraint-due-to-conflicting-nodes]] +=== Creating a constraint when there exist conflicting nodes will fail .+CREATE CONSTRAINT+ ====== -Create a unique property constraint on the property `isbn` on nodes with the `Book` label when there are two nodes with the same `isbn`. +Create a property uniqueness constraint on the property `isbn` on nodes with the `Book` label when there are two nodes with the same `isbn`. //// +Set-up to get expected behavior: CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'}) CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'}) //// @@ -281,39 +304,333 @@ CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS UNIQUE ---- In this case the constraint can not be created because it is violated by existing data. -You may choose to use xref::indexes-for-search-performance.adoc[] instead or remove the offending nodes and then re-apply the constraint. +Either use xref::indexes-for-search-performance.adoc[] instead, or remove the offending nodes and then re-apply the constraint. .Error message [source, "error message", role="noheader"] ---- -Unable to create Constraint( name='constraint_62365a16', type='UNIQUENESS', -schema=(:Book {isbn}) ): +Unable to create Constraint( name='constraint_62365a16', type='UNIQUENESS', schema=(:Book {isbn}) ): Both Node(0) and Node(1) have the label `Book` and property `isbn` = '1449356265' ---- +[NOTE] +==== +The constraint type will be updated to say `NODE_UNIQUENESS` in Neo4j version 6.0. +==== + +====== + + +[[constraints-examples-relationship-uniqueness]] +== Relationship property uniqueness constraints + +A relationship property uniqueness constraint ensures that all relationships with a particular relationship type have a set of defined properties whose combined value is unique when existing. + +* xref::constraints/examples.adoc#constraints-create-a-relationship-uniqueness-constraints[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-uniqueness-constraints-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-uniqueness-constraints-with-index-provider[] +* xref::constraints/examples.adoc#constraints-create-an-already-existing-relationship-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-uniqueness-constraint-on-same-schema-as-existing-index[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-complies-with-a-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-violates-a-uniqueness-constraint[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-uniqueness-constraint-due-to-conflicting-relationships[] + + +[discrete] +[[constraints-create-a-relationship-uniqueness-constraints]] +=== Create a relationship property uniqueness constraint + +When creating a property uniqueness constraint, a name can be provided. + + +.+CREATE CONSTRAINT+ +====== + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_name +FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationship uniqueness constraints added: 1 +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-uniqueness-constraints-if-not-exist]] +=== Handling existing constraints when creating a constraint + +Creating an already existing constraint will fail. +To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another relationship property uniqueness constraint on the same schema already exists. + + +.+CREATE CONSTRAINT+ +====== + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_name IF NOT EXISTS +FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +---- + +Assuming no constraint with the given name or other relationship property uniqueness constraint on the same schema exists: + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationship uniqueness constraints added: 1 +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-uniqueness-constraints-with-index-provider]] +=== Specifying an index provider when creating a constraint + +To create a property uniqueness constraint with a specific index provider for the backing index, the `OPTIONS` clause is used. + +The index type of the backing index is set with the `indexProvider` option. + +The only valid value for the index provider is: + +* `range-1.0` label:default[] + +// Only one valid value exists for the index provider in Neo4j 5.0 + + +.+CREATE CONSTRAINT+ +====== + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_with_options +FOR ()-[friend:FRIENDS_WITH]-() REQUIRE (friend.nickname, friend.since) IS UNIQUE +OPTIONS { + indexProvider: 'range-1.0', +} +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationship uniqueness constraints added: 1 +---- + +====== + +There are no valid index configuration values for the constraint-backing range indexes. + + +[discrete] +[[constraints-create-an-already-existing-relationship-uniqueness-constraint]] +=== Creating an already existing constraint will fail + + +.+CREATE CONSTRAINT+ +====== + +Create a property uniqueness constraint on the property `nickname` on relationships with the `FRIENDS_WITH` relationship type, when that constraint already exists. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT preExistingUnique FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +---- + +In this case, the constraint cannot be created because it already exists. + +.Error message +[source, "error message", role="noheader"] +---- +Constraint already exists: +Constraint( id=4, name='preExistingUnique', type='RELATIONSHIP_UNIQUENESS', schema=()-[:FRIENDS_WITH {nickname}]-(), ownedIndex=3 ) +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-uniqueness-constraint-on-same-schema-as-existing-index]] +=== Creating a constraint on the same schema as an existing index will fail + + +.+CREATE CONSTRAINT+ +====== + +Create a property uniqueness constraint on the property `nickname` on relationships with the `FRIENDS_WITH` relationship type, when an index already exists on that relationship type and property combination. + +//// +Set-up to get expected behavior: +CREATE INDEX FOR ()-[friend:FRIENDS_WITH]-() ON (friend.nickname) +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +---- + +In this case, the constraint cannot be created because there already exists an index covering that schema. + +.Error message +[source, "error message", role="noheader"] +---- +There already exists an index ()-[:FRIENDS_WITH {nickname}]-(). +A constraint cannot be created until the index has been dropped. +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-that-complies-with-a-uniqueness-constraint]] +=== Creating a relationship that complies with an existing constraint + + +.+CREATE RELATIONSHIP+ +====== + +Create a `FRIENDS_WITH` relationship with an `nickname` that is not already in the database. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +//// + +.Query +[source, cypher, indent=0] +---- +CREATE (:Person {name: 'Josefin'})-[:FRIENDS_WITH {nickname: 'Mimi'}]->(:Person {name: 'Emilia'}) +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Nodes created: 2 +Relationships created: 1 +Properties set: 3 +Labels added: 2 +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-that-violates-a-uniqueness-constraint]] +=== Creating a relationship that violates an existing constraint will fail + + +.+CREATE RELATIONSHIP+ +====== + +Create a `FRIENDS_WITH` relationship with an `nickname` that is already used in the database. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +CREATE (:Person {name: 'Emma'}), (:Person {name: 'Josefin'})-[:FRIENDS_WITH {nickname: 'Mimi'}]->(:Person {name: 'Emilia'}) +//// + +.Query +[source, cypher, indent=0] +---- +MATCH (emma:Person {name: 'Emma'}), (emilia:Person {name: 'Emilia'}) +CREATE (emma)-[:FRIENDS_WITH {nickname: 'Mimi'}]->(emilia) +---- + +In this case, the relationship is not created in the graph. + +.Error message +[source, "error message", role="noheader"] +---- +Relationship(0) already exists with type `FRIENDS_WITH` and property `nickname` = 'Mimi' +---- + +====== + + +[discrete] +[[constraints-fail-to-create-a-uniqueness-constraint-due-to-conflicting-relationships]] +=== Creating a constraint when there exist conflicting relationships will fail + + +.+CREATE CONSTRAINT+ +====== + +Create a property uniqueness constraint on the property `nickname` on relationships with the `FRIENDS_WITH` relationship type when there are two relationships with the same `nickname`. + +//// +Set-up to get expected behavior: +CREATE (emma:Person {name: 'Emma'}), (josefin:Person {name: 'Josefin'}), (emilia:Person {name: 'Emilia'}) +CREATE (josefin)-[:FRIENDS_WITH {nickname: 'Mimi'}]->(emilia), (emma)-[:FRIENDS_WITH {nickname: 'Mimi'}]->(emilia) +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT friends FOR ()-[friend:FRIENDS_WITH]-() REQUIRE friend.nickname IS UNIQUE +---- + +In this case, the constraint cannot be created because it is violated by existing data. +Either use xref::indexes-for-search-performance.adoc[] instead, or remove the offending relationships and then re-apply the constraint. + +.Error message +[source, "error message", role="noheader"] +---- +Unable to create Constraint( name='friends', type='RELATIONSHIP_UNIQUENESS', schema=()-[:FRIENDS_WITH {nickname}]-() ): +Both Relationship(0) and Relationship(1) have the type `FRIENDS_WITH` and property `nickname` = 'Mimi' +---- + ====== [role=enterprise-edition] -[[administration-constraints-prop-exist-nodes]] +[[constraints-examples-node-property-existence]] == Node property existence constraints -* xref::constraints/examples.adoc#administration-constraints-create-a-node-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-property-existence-constraint-only-if-it-does-not-already-exist[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-an-already-existing-node-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-complies-with-property-existence-constraints[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-violates-a-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-removing-an-existence-constrained-node-property[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-node-property-existence-constraint-due-to-existing-node[] -//* xref::constraints/examples.adoc# +A node property existence constraint ensures that all nodes with a certain label have a certain property. + +* xref::constraints/examples.adoc#constraints-create-a-node-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-property-existence-constraint-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-an-already-existing-node-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-complies-with-a-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-violates-a-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-removing-an-existence-constrained-node-property[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-property-existence-constraint-due-to-existing-node[] [discrete] -[[administration-constraints-create-a-node-property-existence-constraint]] +[[constraints-create-a-node-property-existence-constraint]] === Create a node property existence constraint When creating a node property existence constraint, a name can be provided. -The constraint ensures that all nodes with a certain label have a certain property. .+CREATE CONSTRAINT+ @@ -335,21 +652,29 @@ FOR (book:Book) REQUIRE book.isbn IS NOT NULL Property existence constraints added: 1 ---- +[NOTE] +==== +The statistics for property existence constraints will be split between nodes and relationships in Neo4j version 6.0. +For the node property existence constraints, they will say `Node property existence constraints`. +==== + ====== + [discrete] -[[administration-constraints-create-a-node-property-existence-constraint-only-if-it-does-not-already-exist]] -=== Create a node property existence constraint only if it does not already exist +[[constraints-create-a-node-property-existence-constraint-if-not-exist]] +=== Handling existing constraints when creating a constraint -If it is not known whether a constraint exists or not, add `IF NOT EXISTS` to ensure it does. -The node property existence constraint ensures that all nodes with a certain label have a certain property. -No constraint will be created if any other constraint with the given name or another node property existence constraint on the same schema already exists. +Creating an already existing constraint will fail. +To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node property existence constraint on the same schema already existed. .+CREATE CONSTRAINT+ ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT constraint_name FOR (book:Book) REQUIRE book.isbn IS UNIQUE //// @@ -374,8 +699,8 @@ Assuming a constraint with the name `constraint_name` already existed: [discrete] -[[administration-constraints-failure-to-create-an-already-existing-node-property-existence-constraint]] -=== Failure to create an already existing node property existence constraint +[[constraints-create-an-already-existing-node-property-existence-constraint]] +=== Creating an already existing constraint will fail .+CREATE CONSTRAINT+ @@ -384,6 +709,7 @@ Assuming a constraint with the name `constraint_name` already existed: Create a node property existence constraint on the property `title` on nodes with the `Book` label, when that constraint already exists. //// +Set-up to get expected behavior: CREATE CONSTRAINT preExistingNodePropExist FOR (book:Book) REQUIRE book.title IS NOT NULL //// @@ -407,8 +733,8 @@ Constraint( id=3, name='preExistingNodePropExist', type='NODE PROPERTY EXISTENCE [discrete] -[[administration-constraints-create-a-node-that-complies-with-property-existence-constraints]] -=== Create a node that complies with property existence constraints +[[constraints-create-a-node-that-complies-with-a-property-existence-constraint]] +=== Creating a node that complies with an existing constraint .+CREATE NODE+ @@ -417,6 +743,7 @@ Constraint( id=3, name='preExistingNodePropExist', type='NODE PROPERTY EXISTENCE Create a `Book` node with an `isbn` property. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS NOT NULL //// @@ -441,8 +768,8 @@ Labels added: 1 [discrete] -[[administration-constraints-create-a-node-that-violates-a-property-existence-constraint]] -=== Create a node that violates a property existence constraint +[[constraints-create-a-node-that-violates-a-property-existence-constraint]] +=== Creating a node that violates an existing constraint will fail .+CREATE NODE+ @@ -451,6 +778,7 @@ Labels added: 1 Trying to create a `Book` node without an `isbn` property, given a property existence constraint on `:Book(isbn)`. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS NOT NULL //// @@ -472,8 +800,8 @@ Node(0) with label `Book` must have the property `isbn` [discrete] -[[administration-constraints-removing-an-existence-constrained-node-property]] -=== Removing an existence constrained node property +[[constraints-removing-an-existence-constrained-node-property]] +=== Removing an existence constrained node property will fail .+REMOVE PROPERTY+ @@ -482,6 +810,7 @@ Node(0) with label `Book` must have the property `isbn` Trying to remove the `isbn` property from an existing node `book`, given a property existence constraint on `:Book(isbn)`. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS NOT NULL CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'}) //// @@ -505,8 +834,8 @@ Node(0) with label `Book` must have the property `isbn` [discrete] -[[administration-constraints-failure-to-create-a-node-property-existence-constraint-due-to-existing-node]] -=== Failure to create a node property existence constraint due to existing node +[[constraints-fail-to-create-a-property-existence-constraint-due-to-existing-node]] +=== Creating a constraint when there exist conflicting nodes will fail .+CREATE CONSTRAINT+ @@ -515,6 +844,7 @@ Node(0) with label `Book` must have the property `isbn` Create a constraint on the property `isbn` on nodes with the `Book` label when there already exists a node without an `isbn`. //// +Set-up to get expected behavior: CREATE (book:Book {title: 'Graph Databases'}) //// @@ -524,13 +854,13 @@ CREATE (book:Book {title: 'Graph Databases'}) CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS NOT NULL ---- -In this case the constraint can't be created because it is violated by existing data. We may choose to remove the offending nodes and then re-apply the constraint. +In this case the constraint can't be created because it is violated by existing data. +Remove the offending nodes and then re-apply the constraint. .Error message [source, "error message", role="noheader"] ---- -Unable to create Constraint( type='NODE PROPERTY EXISTENCE', schema=(:Book -{isbn}) ): +Unable to create Constraint( type='NODE PROPERTY EXISTENCE', schema=(:Book {isbn}) ): Node(0) with label `Book` must have the property `isbn` ---- @@ -538,24 +868,25 @@ Node(0) with label `Book` must have the property `isbn` [role=enterprise-edition] -[[administration-constraints-prop-exist-rels]] +[[constraints-examples-relationship-property-existence]] == Relationship property existence constraints -* xref::constraints/examples.adoc#administration-constraints-create-a-relationship-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-relationship-property-existence-constraint-only-if-it-does-not-already-exist[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-an-already-existing-relationship-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-relationship-that-complies-with-property-existence-constraints[] -* xref::constraints/examples.adoc#administration-constraints-create-a-relationship-that-violates-a-property-existence-constraint[] -* xref::constraints/examples.adoc#administration-constraints-removing-an-existence-constrained-relationship-property[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-relationship-property-existence-constraint-due-to-existing-relationship[] +A relationship property existence constraint ensures that all relationships with a certain type have a certain property. + +* xref::constraints/examples.adoc#constraints-create-a-relationship-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-property-existence-constraint-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-an-already-existing-relationship-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-complies-with-a-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-violates-a-property-existence-constraint[] +* xref::constraints/examples.adoc#constraints-removing-an-existence-constrained-relationship-property[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-property-existence-constraint-due-to-existing-relationship[] [discrete] -[[administration-constraints-create-a-relationship-property-existence-constraint]] +[[constraints-create-a-relationship-property-existence-constraint]] === Create a relationship property existence constraint When creating a relationship property existence constraint, a name can be provided. -The constraint ensures all relationships with a certain type have a certain property. .+CREATE CONSTRAINT+ @@ -577,22 +908,29 @@ FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL Property existence constraints added: 1 ---- +[NOTE] +==== +The statistics for property existence constraints will be split between nodes and relationships in Neo4j version 6.0. +For the relationship property existence constraints, they will say `Relationship property existence constraints`. +==== + ====== [discrete] -[[administration-constraints-create-a-relationship-property-existence-constraint-only-if-it-does-not-already-exist]] -=== Create a relationship property existence constraint only if it does not already exist +[[constraints-create-a-relationship-property-existence-constraint-if-not-exist]] +=== Handling existing constraints when creating a constraint -If it is not known whether a constraint exists or not, add `IF NOT EXISTS` to ensure it does. -The relationship property existence constraint ensures all relationships with a certain type have a certain property. -No constraint will be created if any other constraint with the given name or another relationship property existence constraint on the same schema already exists. +Creating an already existing constraint will fail. +To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another relationship property existence constraint on the same schema already existed. .+CREATE CONSTRAINT+ ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT constraint_name FOR (book:Book) REQUIRE book.isbn IS NOT NULL //// @@ -617,8 +955,8 @@ Assuming a constraint with the name `constraint_name` already existed: [discrete] -[[administration-constraints-failure-to-create-an-already-existing-relationship-property-existence-constraint]] -=== Failure to create an already existing relationship property existence constraint +[[constraints-create-an-already-existing-relationship-property-existence-constraint]] +=== Creating an already existing constraint will fail .+CREATE CONSTRAINT+ @@ -627,6 +965,7 @@ Assuming a constraint with the name `constraint_name` already existed: Create a named relationship property existence constraint on the property `week` on relationships with the `LIKED` type, when a constraint with the given name already exists. //// +Set-up to get expected behavior: CREATE CONSTRAINT relPropExist FOR ()-[like:LIKED]-() REQUIRE like.since IS NOT NULL //// @@ -649,8 +988,8 @@ There already exists a constraint called 'relPropExist'. [discrete] -[[administration-constraints-create-a-relationship-that-complies-with-property-existence-constraints]] -=== Create a relationship that complies with property existence constraints +[[constraints-create-a-relationship-that-complies-with-a-property-existence-constraint]] +=== Creating a relationship that complies with an existing constraint .+CREATE RELATIONSHIP+ @@ -659,6 +998,7 @@ There already exists a constraint called 'relPropExist'. Create a `LIKED` relationship with a `day` property. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL //// @@ -684,8 +1024,8 @@ Labels added: 2 [discrete] -[[administration-constraints-create-a-relationship-that-violates-a-property-existence-constraint]] -=== Create a relationship that violates a property existence constraint +[[constraints-create-a-relationship-that-violates-a-property-existence-constraint]] +=== Creating a relationship that violates an existing constraint will fail .+CREATE RELATIONSHIP+ @@ -694,6 +1034,7 @@ Labels added: 2 Trying to create a `LIKED` relationship without a `day` property, given a property existence constraint `:LIKED(day)`. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL //// @@ -715,8 +1056,8 @@ Relationship(0) with type `LIKED` must have the property `day` [discrete] -[[administration-constraints-removing-an-existence-constrained-relationship-property]] -=== Removing an existence constrained relationship property +[[constraints-removing-an-existence-constrained-relationship-property]] +=== Removing an existence constrained relationship property will fail .+REMOVE PROPERTY+ @@ -725,6 +1066,7 @@ Relationship(0) with type `LIKED` must have the property `day` Trying to remove the `day` property from an existing relationship `like` of type `LIKED`, given a property existence constraint `:LIKED(day)`. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL CREATE (user:User)-[like:LIKED {day: 'yesterday'}]->(book:Book) //// @@ -747,8 +1089,8 @@ Relationship(0) with type `LIKED` must have the property `day` [discrete] -[[administration-constraints-failure-to-create-a-relationship-property-existence-constraint-due-to-existing-relationship]] -=== Failure to create a relationship property existence constraint due to existing relationship +[[constraints-fail-to-create-a-property-existence-constraint-due-to-existing-relationship]] +=== Creating a constraint when there exist conflicting relationships will fail .+CREATE CONSTRAINT+ @@ -757,6 +1099,7 @@ Relationship(0) with type `LIKED` must have the property `day` Create a constraint on the property `day` on relationships with the `LIKED` type when there already exists a relationship without a property named `day`. //// +Set-up to get expected behavior: CREATE (user:User)-[like:LIKED]->(book:Book) //// @@ -766,13 +1109,13 @@ CREATE (user:User)-[like:LIKED]->(book:Book) CREATE CONSTRAINT FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL ---- -In this case the constraint can not be created because it is violated by existing data. We may choose to remove the offending relationships and then re-apply the constraint. +In this case the constraint can not be created because it is violated by existing data. +Remove the offending relationships and then re-apply the constraint. .Error message [source, "error message", role="noheader"] ---- -Unable to create Constraint( type='RELATIONSHIP PROPERTY EXISTENCE', -schema=-[:LIKED {day}]- ): +Unable to create Constraint( type='RELATIONSHIP PROPERTY EXISTENCE', schema=-[:LIKED {day}]- ): Relationship(0) with type `LIKED` must have the property `day` ---- @@ -780,26 +1123,27 @@ Relationship(0) with type `LIKED` must have the property `day` [role=enterprise-edition] -[[administration-constraints-node-key]] +[[constraints-examples-node-key]] == Node key constraints -* xref::constraints/examples.adoc#administration-constraints-create-a-node-key-constraint[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-key-constraint-only-if-it-does-not-already-exist[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-key-constraint-with-specified-index-provider[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-node-key-constraint-when-a-unique-property-constraint-exists-on-the-same-schema[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-node-key-constraint-with-the-same-name-as-existing-index[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-complies-with-node-key-constraints[] -* xref::constraints/examples.adoc#administration-constraints-create-a-node-that-violates-a-node-key-constraint[] -* xref::constraints/examples.adoc#administration-constraints-removing-a-node-key-constrained-property[] -* xref::constraints/examples.adoc#administration-constraints-failure-to-create-a-node-key-constraint-due-to-existing-node[] +A node key constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique and all properties in the set are present. + +* xref::constraints/examples.adoc#constraints-create-a-node-key-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-key-constraint-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-a-node-key-constraint-with-index-provider[] +* xref::constraints/examples.adoc#constraints-node-key-and-uniqueness-constraint-on-the-same-schema[] +* xref::constraints/examples.adoc#constraints-create-a-node-key-constraint-with-the-same-name-as-existing-index[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-complies-with-a-node-key-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-node-that-violates-a-node-key-constraint[] +* xref::constraints/examples.adoc#constraints-removing-a-node-key-constrained-property[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-node-key-constraint-due-to-existing-node[] [discrete] -[[administration-constraints-create-a-node-key-constraint]] +[[constraints-create-a-node-key-constraint]] === Create a node key constraint When creating a node key constraint, a name can be provided. -The constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique and all properties in the set are present. .+CREATE CONSTRAINT+ @@ -825,18 +1169,19 @@ Node key constraints added: 1 [discrete] -[[administration-constraints-create-a-node-key-constraint-only-if-it-does-not-already-exist]] -=== Create a node key constraint only if it does not already exist +[[constraints-create-a-node-key-constraint-if-not-exist]] +=== Handling existing constraints when creating a constraint -If it is not known whether a constraint exists or not, add `IF NOT EXISTS` to ensure it does. -The node key constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique and all properties in the set are present. -No constraint will be created if any other constraint with the given name or another node key constraint on the same schema already exists. +Creating an already existing constraint will fail. +To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node key constraint on the same schema already exists. .+CREATE CONSTRAINT+ ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY //// @@ -861,14 +1206,14 @@ Assuming a node key constraint on `(:Person {firstname, surname})` already exist [discrete] -[[administration-constraints-create-a-node-key-constraint-with-specified-index-provider]] -=== Create a node key constraint with specified index provider +[[constraints-create-a-node-key-constraint-with-index-provider]] +=== Specifying an index provider when creating a constraint To create a node key constraint with a specific index provider for the backing index, the `OPTIONS` clause is used. The index type of the backing index is set with the `indexProvider` option. -Valid values for the index provider are: +The only valid value for the index provider is: * `range-1.0` label:default[] @@ -897,18 +1242,21 @@ Node key constraints added: 1 ====== +There is no valid index configuration values for the constraint-backing range indexes. + [discrete] -[[administration-constraints-failure-to-create-a-node-key-constraint-when-a-unique-property-constraint-exists-on-the-same-schema]] -=== Failure to create a node key constraint when a unique property constraint exists on the same schema +[[constraints-node-key-and-uniqueness-constraint-on-the-same-schema]] +=== Node key and property uniqueness constraints are not allowed on the same schema .+CREATE CONSTRAINT+ ====== -Create a node key constraint on the properties `firstname` and `age` on nodes with the `Person` label, when a unique property constraint already exists on the same label and property combination. +Create a node key constraint on the properties `firstname` and `age` on nodes with the `Person` label, when a property uniqueness constraint already exists on the same label and property combination. //// +Set-up to get expected behavior: CREATE CONSTRAINT preExistingUnique FOR (p:Person) REQUIRE (p.firstname, p.age) IS UNIQUE //// @@ -931,8 +1279,8 @@ Constraint( id=4, name='preExistingUnique', type='UNIQUENESS', schema=(:Person { [discrete] -[[administration-constraints-failure-to-create-a-node-key-constraint-with-the-same-name-as-existing-index]] -=== Failure to create a node key constraint with the same name as existing index +[[constraints-create-a-node-key-constraint-with-the-same-name-as-existing-index]] +=== Creating a constraint on same name as an existing index will fail .+CREATE CONSTRAINT+ @@ -941,6 +1289,7 @@ Constraint( id=4, name='preExistingUnique', type='UNIQUENESS', schema=(:Person { Create a named node key constraint on the property `title` on nodes with the `Book` label, when an index already exists with the given name. //// +Set-up to get expected behavior: CREATE INDEX bookTitle FOR (book:ComicBook) ON (book.title) //// @@ -963,8 +1312,8 @@ There already exists an index called 'bookTitle'. [discrete] -[[administration-constraints-create-a-node-that-complies-with-node-key-constraints]] -=== Create a node that complies with node key constraints +[[constraints-create-a-node-that-complies-with-a-node-key-constraint]] +=== Creating a node that complies with an existing constraint .+CREATE NODE+ @@ -973,6 +1322,7 @@ There already exists an index called 'bookTitle'. Create a `Person` node with both a `firstname` and `surname` property. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY //// @@ -997,8 +1347,8 @@ Labels added: 1 [discrete] -[[administration-constraints-create-a-node-that-violates-a-node-key-constraint]] -=== Create a node that violates a node key constraint +[[constraints-create-a-node-that-violates-a-node-key-constraint]] +=== Creating a node that violates an existing constraint will fail .+CREATE NODE+ @@ -1007,6 +1357,7 @@ Labels added: 1 Trying to create a `Person` node without a `surname` property, given a node key constraint on `:Person(firstname, surname)`, will fail. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY //// @@ -1021,15 +1372,15 @@ In this case the node is not created in the graph. .Error message [source, "error message", role="noheader"] ---- -Node(0) with label `Person` must have the properties (firstname, surname) +Node(0) with label `Person` must have the properties (`firstname`, `surname`) ---- ====== [discrete] -[[administration-constraints-removing-a-node-key-constrained-property]] -=== Removing a +NODE KEY+-constrained property +[[constraints-removing-a-node-key-constrained-property]] +=== Removing a +NODE KEY+-constrained property will fail .+REMOVE PROPERTY+ @@ -1038,6 +1389,7 @@ Node(0) with label `Person` must have the properties (firstname, surname) Trying to remove the `surname` property from an existing node `Person`, given a `NODE KEY` constraint on `:Person(firstname, surname)`. //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY CREATE (p:Person {firstname: 'John', surname: 'Wood', age: 55}) //// @@ -1053,15 +1405,15 @@ In this case the property is not removed. .Error message [source, "error message", role="noheader"] ---- -Node(0) with label `Person` must have the properties (firstname, surname) +Node(0) with label `Person` must have the properties (`firstname`, `surname`) ---- ====== [discrete] -[[administration-constraints-failure-to-create-a-node-key-constraint-due-to-existing-node]] -=== Failure to create a node key constraint due to existing node +[[constraints-fail-to-create-a-node-key-constraint-due-to-existing-node]] +=== Creating a constraint when there exist conflicting node will fail .+CREATE CONSTRAINT+ @@ -1070,6 +1422,7 @@ Node(0) with label `Person` must have the properties (firstname, surname) Trying to create a node key constraint on the property `surname` on nodes with the `Person` label will fail when a node without a `surname` already exists in the database. //// +Set-up to get expected behavior: CREATE (p:Person {firstname: 'John', age: 55}) //// @@ -1080,39 +1433,369 @@ CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY ---- In this case the node key constraint can not be created because it is violated by existing data. -We may choose to remove the offending nodes and then re-apply the constraint. +Either use xref::indexes-for-search-performance.adoc[] instead, or remove the offending nodes and then re-apply the constraint. + +.Error message +[source, "error message", role="noheader"] +---- +Unable to create Constraint( type='NODE KEY', schema=(:Person {firstname, surname}) ): +Node(0) with label `Person` must have the properties (`firstname`, `surname`) +---- + +====== + + +[role=enterprise-edition] +[[constraints-examples-relationship-key]] +== Relationship key constraints + +A relationship key constraint ensures that all relationships with a particular relationship type have a set of defined properties whose combined value is unique. +It also ensures that all properties in the set are present. + +* xref::constraints/examples.adoc#constraints-create-a-relationship-key-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-key-constraint-if-not-exist[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-key-constraint-with-index-provider[] +* xref::constraints/examples.adoc#constraints-relationship-key-and-uniqueness-constraint-on-the-same-schema[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-key-constraint-with-the-same-name-as-existing-index[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-complies-with-a-relationship-key-constraint[] +* xref::constraints/examples.adoc#constraints-create-a-relationship-that-violates-a-relationship-key-constraint[] +* xref::constraints/examples.adoc#constraints-removing-a-relationship-key-constrained-property[] +* xref::constraints/examples.adoc#constraints-fail-to-create-a-relationship-key-constraint-due-to-existing-relationship[] + + +[discrete] +[[constraints-create-a-relationship-key-constraint]] +=== Create a relationship key constraint + +When creating a relationship key constraint, a name can be provided. + + +.+CREATE CONSTRAINT+ +====== + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_name +FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS RELATIONSHIP KEY +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationship key constraints added: 1 +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-key-constraint-if-not-exist]] +=== Handling existing constraints when creating a constraint + +Creating an already existing constraint will fail. +To avoid such an error, `IF NOT EXISTS` can be added to the `CREATE` command. +This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another relationship key constraint on the same schema already exists. + + +.+CREATE CONSTRAINT+ +====== + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS RELATIONSHIP KEY +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_name IF NOT EXISTS +FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS RELATIONSHIP KEY +---- + +Assuming a relationship key constraint on `()-[:ROAD {startPoint, endPoint}]-()` already existed: + +.Result +[queryresult] +---- ++--------------------------------------------+ +| No data returned, and nothing was changed. | ++--------------------------------------------+ +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-key-constraint-with-index-provider]] +=== Specifying an index provider when creating a constraint + +To create a relationship key constraint with a specific index provider for the backing index, the `OPTIONS` clause is used. + +The index type of the backing index is set with the `indexProvider` option. + +The only valid value for the index provider is: + +* `range-1.0` label:default[] + + +.+CREATE CONSTRAINT+ +====== + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT constraint_with_provider +FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS REL KEY +OPTIONS { + indexProvider: 'range-1.0' +} +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationship key constraints added: 1 +---- + +====== + +There is no valid index configuration values for the constraint-backing range indexes. + + +[discrete] +[[constraints-relationship-key-and-uniqueness-constraint-on-the-same-schema]] +=== Relationship key and property uniqueness constraints are not allowed on the same schema + + +.+CREATE CONSTRAINT+ +====== + +Create a relationship key constraint on the properties `startPoint` and `endPoint` on relationships with the `ROAD` relationship type, when a property uniqueness constraint already exists on the same relationship type and property combination. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT preExistingUnique FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS UNIQUE +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS REL KEY +---- + +In this case, the constraint cannot be created because there already exists a conflicting constraint on that relationship type and property combination. + +.Error message +[source, "error message", role="noheader"] +---- +Constraint already exists: +Constraint( id=4, name='preExistingUnique', type='RELATIONSHIP_UNIQUENESS', schema=()-[:ROAD {startPoint, endPoint}]-(), ownedIndex=3 ) +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-key-constraint-with-the-same-name-as-existing-index]] +=== Creating a constraint on same name as an existing index will fail + + +.+CREATE CONSTRAINT+ +====== + +Create a named relationship key constraint on the property `coordinates` on relationships with the `INTERSECTION` relationship type, when an index already exists with the given name. + +//// +Set-up to get expected behavior: +CREATE INDEX intersections FOR ()-[intersect:Roundabout]-() ON (intersect.coordinates) +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT intersections +FOR ()-[r:INTERSECTION]-() REQUIRE (r.coordinates) IS REL KEY +---- + +In this case, the constraint cannot be created because there already exists an index with the given name. .Error message [source, "error message", role="noheader"] ---- -Unable to create Constraint( type='NODE PROPERTY EXISTENCE', schema=(:Person -{firstname, surname}) ): -Node(0) with label `Person` must have the properties (firstname, surname) +There already exists an index called 'intersections'. ---- ====== -[[administration-constraints-drop-constraint]] +[discrete] +[[constraints-create-a-relationship-that-complies-with-a-relationship-key-constraint]] +=== Creating a relationship that complies with an existing constraint + + +.+CREATE RELATIONSHIP+ +====== + +Create a `ROAD` relationship with both a `startPoint` and `endPoint` property. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS REL KEY +CREATE (:Intersection {name: 'a', coordinates: point({x: 1, y:2})}), (:Intersection {name: 'b', coordinates: point({x: 2, y:5})}) +//// + +.Query +[source, cypher, indent=0] +---- +MATCH (a:Intersection {name: 'a'}), (b:Intersection {name: 'b'}) +CREATE (a)-[:ROAD {startPoint: a.coordinates, endPoint: b.coordinates}]->(b) +---- + +.Result +[queryresult] +---- ++-------------------+ +| No data returned. | ++-------------------+ +Relationships created: 1 +Properties set: 2 +---- + +====== + + +[discrete] +[[constraints-create-a-relationship-that-violates-a-relationship-key-constraint]] +=== Creating a relationship that violates an existing constraint will fail + + +.+CREATE RELATIONSHIP+ +====== + +Trying to create a `INTERSECTION` relationship without a `coordinates` property, given a relationship key constraint on `:INTERSECTION(coordinates)`, will fail. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[r:INTERSECTION]-() REQUIRE (r.coordinates) IS REL KEY +CREATE (:Road {name: 'a'}), (:Road {name: 'b'}) +//// + +.Query +[source, cypher, indent=0] +---- +MATCH (a:Road {name: 'a'}), (b:Road {name: 'b'}) +CREATE (a)-[:INTERSECTION]->(b) +---- + +In this case, the relationship is not created in the graph. + +.Error message +[source, "error message", role="noheader"] +---- +Relationship(0) with type `INTERSECTION` must have the property `coordinates` +---- + +====== + + +[discrete] +[[constraints-removing-a-relationship-key-constrained-property]] +=== Removing a +RELATIONSHIP KEY+-constrained property will fail + + +.+REMOVE PROPERTY+ +====== + +Trying to remove the `endPoint` property from an existing relationship `ROAD`, given a `RELATIONSHIP KEY` constraint on `:ROAD(startPoint, endPoint)`. + +//// +Set-up to get expected behavior: +CREATE CONSTRAINT FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS REL KEY +CREATE (a:Intersection {name: 'a', coordinates: point({x: 1, y:2})}), (b:Intersection {name: 'b', coordinates: point({x: 2, y:5})}) +CREATE (a)-[:ROAD {startPoint: a.coordinates, endPoint: b.coordinates}]->(b) +//// + +.Query +[source, cypher, indent=0] +---- +MATCH ()-[r:ROAD {startPoint: point({x: 1, y:2}), endPoint: point({x: 2, y:5})}]->() REMOVE r.endPoint +---- + +In this case, the property is not removed. + +.Error message +[source, "error message", role="noheader"] +---- +Relationship(0) with type `ROAD` must have the properties (`startPoint`, `endPoint`) +---- + +====== + + +[discrete] +[[constraints-fail-to-create-a-relationship-key-constraint-due-to-existing-relationship]] +=== Creating a constraint when there exist conflicting relationships will fail + + +.+CREATE CONSTRAINT+ +====== + +Trying to create a relationship key constraint on the property `coordinates` on relationships with the `INTERSECTION` relationship type will fail when two relationships with identical `coordinates` already exists in the database. + +//// +Set-up to get expected behavior: +CREATE (a:Road {name: 'a'}), (b:Road {name: 'b'}) +CREATE (a)-[:INTERSECTION {coordinates: point({x:1, y:2})}]->(b) +CREATE (a)<-[:INTERSECTION {coordinates: point({x:1, y:2})}]-(b) +//// + +.Query +[source, cypher, indent=0] +---- +CREATE CONSTRAINT intersectionConstraint FOR ()-[r:INTERSECTION]-() REQUIRE (r.coordinates) IS REL KEY +---- + +In this case, the relationship key constraint cannot be created because it is violated by existing data. +Either use xref::indexes-for-search-performance.adoc[] instead, or remove the offending relationships and then re-apply the constraint. + +.Error message +[source, "error message", role="noheader"] +---- +Unable to create Constraint( name='intersectionConstraint', type='RELATIONSHIP KEY', schema=()-[:INTERSECTION {coordinates}]-() ): +Both Relationship(0) and Relationship(1) have the type `INTERSECTION` and property `coordinates` = {geometry: {type: "Point", coordinates: [1.0, 2.0], crs: {type: link, properties: {href: "http://spatialreference.org/ref/sr-org/7203/", code: 7203}}}} +---- + +====== + + +[[constraints-examples-drop-constraint]] == Drop a constraint by name -* xref::constraints/examples.adoc#administration-constraints-drop-a-constraint[] -* xref::constraints/examples.adoc#administration-constraints-drop-a-non-existing-constraint[] +* xref::constraints/examples.adoc#constraints-drop-a-constraint[] +* xref::constraints/examples.adoc#constraints-drop-a-non-existing-constraint[] [discrete] -[[administration-constraints-drop-a-constraint]] +[[constraints-drop-a-constraint]] === Drop a constraint A constraint can be dropped using the name with the `DROP CONSTRAINT constraint_name` command. -It is the same command for unique property, property existence and node key constraints. -The name of the constraint can be found using the xref::constraints/syntax.adoc#administration-constraints-syntax-list[`SHOW CONSTRAINTS` command], given in the output column `name`. +It is the same command for uniqueness, property existence, and node/relationship key constraints. +The name of the constraint can be found using the xref::constraints/syntax.adoc#constraints-syntax-list[`SHOW CONSTRAINTS` command], given in the output column `name`. .+DROP CONSTRAINT+ ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT constraint_name FOR (n:Person) REQUIRE (n.name) IS NOT NULL //// @@ -1135,11 +1818,11 @@ Named constraints removed: 1 [discrete] -[[administration-constraints-drop-a-non-existing-constraint]] +[[constraints-drop-a-non-existing-constraint]] === Drop a non-existing constraint If it is uncertain if any constraint with a given name exists and you want to drop it if it does but not get an error should it not, use `IF EXISTS`. -It is the same command for unique property, property existence and node key constraints. +It is the same command for uniqueness, property existence, and node/relationship key constraints. .+DROP CONSTRAINT+ ====== @@ -1161,15 +1844,15 @@ DROP CONSTRAINT missing_constraint_name IF EXISTS ====== -[[administration-constraints-list-constraint]] +[[constraints-examples-list-constraint]] == Listing constraints -* xref::constraints/examples.adoc#administration-constraints-listing-all-constraints[] -* xref::constraints/examples.adoc#administration-constraints-listing-constraints-with-filtering[] +* xref::constraints/examples.adoc#constraints-listing-all-constraints[] +* xref::constraints/examples.adoc#constraints-listing-constraints-with-filtering[] [discrete] -[[administration-constraints-listing-all-constraints]] +[[constraints-listing-all-constraints]] === Listing all constraints To list all constraints with the default output columns, the `SHOW CONSTRAINTS` command can be used. @@ -1178,7 +1861,7 @@ If all columns are required, use `SHOW CONSTRAINTS YIELD *`. [NOTE] ==== One of the output columns from `SHOW CONSTRAINTS` is the name of the constraint. -This can be used to drop the constraint with the xref::constraints/syntax.adoc#administration-constraints-syntax-drop[`DROP CONSTRAINT` command]. +This can be used to drop the constraint with the xref::constraints/syntax.adoc#constraints-syntax-drop[`DROP CONSTRAINT` command]. ==== @@ -1186,7 +1869,9 @@ This can be used to drop the constraint with the xref::constraints/syntax.adoc#a ====== //// -CREATE CONSTRAINT constraint_1bc95fcb FOR (n:Book) REQUIRE (n.isbn) IS UNIQUE +Set-up to get expected behavior: +CREATE CONSTRAINT isbnConstraint FOR (n:Book) REQUIRE (n.isbn) IS UNIQUE +CREATE CONSTRAINT roadConstraint FOR ()-[r:ROAD]-() REQUIRE (r.startPoint, r.endPoint) IS UNIQUE //// .Query @@ -1197,24 +1882,31 @@ SHOW CONSTRAINTS [queryresult] ---- -+-------------------------------------------------------------------------------------------------------------+ -| id | name | type | entityType | labelsOrTypes | properties | ownedIndex | -+-------------------------------------------------------------------------------------------------------------+ -| 4 | "constraint_1bc95fcb" | "UNIQUENESS" | "NODE" | ["Book"] | ["isbn"] | "constraint_1bc95fcb" | -+-------------------------------------------------------------------------------------------------------------+ -1 row ++------------------------------------------------------------------------------------------------------------------------------------+ +| id | name | type | entityType | labelsOrTypes | properties | ownedIndex | ++------------------------------------------------------------------------------------------------------------------------------------+ +| 4 | "isbnConstraint" | "UNIQUENESS" | "NODE" | ["Book"] | ["isbn"] | "isbnConstraint" | +| 6 | "roadConstraint" | "RELATIONSHIP_UNIQUENESS" | "RELATIONSHIP" | ["ROAD"] | ["startPoint", "endPoint"] | "roadConstraint" | ++------------------------------------------------------------------------------------------------------------------------------------+ +2 rows ---- +[NOTE] +==== +The `type` column returns `UNIQUENESS` for the node property uniqueness constraint and `RELATIONSHIP_UNIQUENESS` for the relationship property uniqueness constraint. +The `type` for node property uniqueness constraint will be updated to `NODE_UNIQUENESS` in Neo4j version 6.0. +==== + ====== [discrete] -[[administration-constraints-listing-constraints-with-filtering]] +[[constraints-listing-constraints-with-filtering]] === Listing constraints with filtering One way of filtering the output from `SHOW CONSTRAINTS` by constraint type is the use of type keywords, -listed in xref::constraints/syntax.adoc#administration-constraints-syntax-list[Syntax for listing constraints]. -For example, to show only unique node property constraints, use `SHOW UNIQUE CONSTRAINTS`. +listed in the xref::constraints/syntax.adoc#constraints-syntax-list-type-filter[syntax for listing constraints type filter table]. +For example, to show only property uniqueness constraints, use `SHOW UNIQUENESS CONSTRAINTS`. Another more flexible way of filtering the output is to use the `WHERE` clause. An example is to only show constraints on relationships. @@ -1223,6 +1915,7 @@ An example is to only show constraints on relationships. ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (n:Book) REQUIRE (n.isbn) IS UNIQUE CREATE CONSTRAINT FOR (book:Book) REQUIRE book.title IS NOT NULL CREATE CONSTRAINT `constraint_f076a74d` FOR ()-[r:KNOWS]-() REQUIRE r.since IS NOT NULL diff --git a/modules/ROOT/pages/constraints/index.adoc b/modules/ROOT/pages/constraints/index.adoc index 48a149bdf..472dc110e 100644 --- a/modules/ROOT/pages/constraints/index.adoc +++ b/modules/ROOT/pages/constraints/index.adoc @@ -1,6 +1,6 @@ :description: This section explains how to manage constraints used for ensuring data integrity. -[[administration-constraints]] +[[constraints]] = Constraints [abstract] @@ -14,9 +14,14 @@ This section explains how to manage constraints used for ensuring data integrity The following constraint types are available: *Unique node property constraints*:: -Unique property constraints ensure that property values are unique for all nodes with a specific label. -For unique property constraints on multiple properties, the combination of the property values is unique. -Unique constraints do not require all nodes to have a unique value for the properties listed -- nodes without all properties are not subject to this rule. +Unique node property constraints, or node property uniqueness constraints, ensure that property values are unique for all nodes with a specific label. +For property uniqueness constraints on multiple properties, the combination of the property values is unique. +Node property uniqueness constraints do not require all nodes to have a unique value for the properties listed (nodes without all properties are not subject to this rule). + +*Unique relationship property constraints*:: +Unique relationship property constraints, or relationship property uniqueness constraints, ensure that property values are unique for all relationships with a specific type. +For property uniqueness constraints on multiple properties, the combination of the property values is unique. +Relationship property uniqueness constraints do not require all relationships to have a unique value for the properties listed (relationships without all properties are not subject to this rule). *Node property existence constraints* label:enterprise-edition[]:: Node property existence constraints ensure that a property exists for all nodes with a specific label. @@ -24,7 +29,7 @@ Queries that try to create new nodes of the specified label, but without this pr The same is true for queries that try to remove the mandatory property. *Relationship property existence constraints* label:enterprise-edition[]:: -Property existence constraints ensure that a property exists for all relationships with a specific type. +Relationship property existence constraints ensure that a property exists for all relationships with a specific type. All queries that try to create relationships of the specified type, but without this property, will fail. The same is true for queries that try to remove the mandatory property. @@ -42,10 +47,24 @@ Queries attempting to do any of the following will fail: * Remove one of the mandatory properties. * Update the properties so that the combination of property values is no longer unique. +*Relationship key constraints* label:enterprise-edition[]:: +Relationship key constraints ensure that, for a given type and set of properties: ++ +[lowerroman] +. All the properties exist on all the relationships with that type. +. The combination of the property values is unique. + ++ +Queries attempting to do any of the following will fail: + +* Create new relationships without all the properties or where the combination of property values is not unique. +* Remove one of the mandatory properties. +* Update the properties so that the combination of property values is no longer unique. + [NOTE] ==== -Node key constraints, node property existence constraints and relationship property existence constraints are only available in Neo4j Enterprise Edition. +Node key constraints, relationship key constraints, node property existence constraints, and relationship property existence constraints are only available in Neo4j Enterprise Edition. Databases containing one of these constraint types cannot be opened using Neo4j Community Edition. ==== @@ -54,15 +73,15 @@ Databases containing one of these constraint types cannot be opened using Neo4j Creating a constraint has the following implications on indexes: -* Adding a node key or unique property constraint on a single property also adds an index on that property and therefore, an index of the same index type, label, and property combination cannot be added separately. -* Adding a node key or unique property constraint for a set of properties also adds an index on those properties and therefore, an index of the same index type, label, and properties combination cannot be added separately. +* Adding a node key, relationship key, or property uniqueness constraint on a single property also adds an index on that property, and therefore, an index of the same index type, label/relationship type, and property combination cannot be added separately. +* Adding a node key, relationship key, or property uniqueness constraint for a set of properties also adds an index on those properties, and therefore, an index of the same index type, label/relationship type, and properties combination cannot be added separately. * Cypher will use these indexes for lookups just like other indexes. Refer to xref::indexes-for-search-performance.adoc[] for more details on indexes. -* If a node key or unique property constraint is dropped and the backing index is still required, the index need to be created explicitly. +* If a node key, relationship key, or property uniqueness constraint is dropped and the backing index is still required, the index need to be created explicitly. Additionally, the following is true for constraints: -* A given label can have multiple constraints, and unique and property existence constraints can be combined on the same property. +* A given label or relationship type can have multiple constraints, and uniqueness and property existence constraints can be combined on the same property. * Adding constraints is an atomic operation that can take a while -- all existing data has to be scanned before Neo4j DBMS can turn the constraint 'on'. * Best practice is to give the constraint a name when it is created. If the constraint is not explicitly named, it will get an auto-generated name. diff --git a/modules/ROOT/pages/constraints/syntax.adoc b/modules/ROOT/pages/constraints/syntax.adoc index 5a759d93f..4ca00d0dd 100644 --- a/modules/ROOT/pages/constraints/syntax.adoc +++ b/modules/ROOT/pages/constraints/syntax.adoc @@ -1,6 +1,6 @@ :description: Syntax for how to manage constraints used for ensuring data integrity. -[[administration-constraints-syntax]] +[[constraints-syntax]] = Syntax :check-mark: icon:check[] @@ -9,17 +9,18 @@ The syntax descriptions use xref:access-control/index.adoc#access-control-syntax[the style] from access control. ==== -[[administration-constraints-syntax-create]] +[[constraints-syntax-create]] == Syntax for creating constraints Best practice when creating a constraint is to give the constraint a name. This name must be unique among both indexes and constraints. If a name is not explicitly given, a unique name will be auto-generated. -The create constraint command is optionally idempotent. This means its default behavior is to throw an error if an attempt is made to create the same constraint twice. +The `CREATE CONSTRAINT` command is optionally idempotent. +This means its default behavior is to throw an error if an attempt is made to create the same constraint twice. With the `IF NOT EXISTS` flag, no error is thrown and nothing happens should a constraint with the same name or same schema and constraint type already exist. It may still throw an error if conflicting data, indexes, or constraints exist. -Examples of this are nodes with missing properties, indexes with the same name, or constraints with same schema but a different constraint type. +Examples of this are nodes with missing properties, indexes with the same name, or constraints with same schema but a different conflicting constraint type. For constraints that are backed by an index, the index provider for the backing index can be specified using the `OPTIONS` clause. Only one valid value exists for the index provider, `range-1.0`, which is the default value. @@ -30,17 +31,17 @@ There is no supported index configuration for range indexes. Creating a constraint requires the xref::access-control/database-administration.adoc#access-control-database-administration-constraints[`CREATE CONSTRAINT` privilege]. ==== -[[administration-constraints-syntax-create-unique]] +[[constraints-syntax-create-node-unique]] [discrete] -=== Create a unique node property constraint +=== Create a node property uniqueness constraint -This command creates a uniqueness constraint on nodes with the specified label and properties. +This command creates a property uniqueness constraint on nodes with the specified label and properties. [source, syntax, role="noheader", indent=0] ---- CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] FOR (n:LabelName) -REQUIRE n.propertyName IS UNIQUE +REQUIRE n.propertyName IS [NODE] UNIQUE [OPTIONS "{" option: value[, ...] "}"] ---- @@ -48,14 +49,39 @@ REQUIRE n.propertyName IS UNIQUE ---- CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] FOR (n:LabelName) -REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS UNIQUE +REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] UNIQUE [OPTIONS "{" option: value[, ...] "}"] ---- Index provider can be specified using the `OPTIONS` clause. -[[administration-constraints-syntax-create-node-exists]] +[[constraints-syntax-create-rel-unique]] +[discrete] +=== Create a relationship property uniqueness constraint + +This command creates a property uniqueness constraint on relationships with the specified relationship type and properties. + +[source, syntax, role="noheader", indent=0] +---- +CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] +FOR ()-"["r:RELATIONSHIP_TYPE"]"-() +REQUIRE r.propertyName IS [REL[ATIONSHIP]] UNIQUE +[OPTIONS "{" option: value[, ...] "}"] +---- + +[source, syntax, role="noheader", indent=0] +---- +CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] +FOR ()-"["r:RELATIONSHIP_TYPE"]"-() +REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] UNIQUE +[OPTIONS "{" option: value[, ...] "}"] +---- + +Index provider can be specified using the `OPTIONS` clause. + + +[[constraints-syntax-create-node-exists]] [discrete] === Create a node property existence constraint label:enterprise-edition[] @@ -75,7 +101,7 @@ There are no supported `OPTIONS` values for existence constraints, but an empty ==== -[[administration-constraints-syntax-create-rel-exists]] +[[constraints-syntax-create-rel-exists]] [discrete] === Create a relationship property existence constraint label:enterprise-edition[] @@ -95,7 +121,7 @@ There are no supported `OPTIONS` values for existence constraints, but an empty ==== -[[administration-constraints-syntax-create-node-key]] +[[constraints-syntax-create-node-key]] [discrete] === Create a node key constraint label:enterprise-edition[] @@ -105,7 +131,7 @@ This command creates a node key constraint on nodes with the specified label and ---- CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] FOR (n:LabelName) -REQUIRE n.propertyName IS NODE KEY +REQUIRE n.propertyName IS [NODE] KEY [OPTIONS "{" option: value[, ...] "}"] ---- @@ -113,14 +139,39 @@ REQUIRE n.propertyName IS NODE KEY ---- CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] FOR (n:LabelName) -REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS NODE KEY +REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] KEY [OPTIONS "{" option: value[, ...] "}"] ---- Index provider can be specified using the `OPTIONS` clause. -[[administration-constraints-syntax-drop]] +[[constraints-syntax-create-rel-key]] +[discrete] +=== Create a relationship key constraint label:enterprise-edition[] + +This command creates a relationship key constraint on relationships with the specified relationship type and properties. + +[source, syntax, role="noheader", indent=0] +---- +CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] +FOR ()-"["r:RELATIONSHIP_TYPE"]"-() +REQUIRE r.propertyName IS [REL[ATIONSHIP]] KEY +[OPTIONS "{" option: value[, ...] "}"] +---- + +[source, syntax, role="noheader", indent=0] +---- +CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS] +FOR ()-"["r:RELATIONSHIP_TYPE"]"-() +REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] KEY +[OPTIONS "{" option: value[, ...] "}"] +---- + +Index provider can be specified using the `OPTIONS` clause. + + +[[constraints-syntax-drop]] == Syntax for dropping constraints Dropping a constraint is done by specifying the name of the constraint. @@ -139,22 +190,32 @@ Dropping a constraint requires the xref::access-control/database-administration. ==== -[[administration-constraints-syntax-list]] +[[constraints-syntax-list]] == Syntax for listing constraints List constraints in the database, either all or filtered on constraint type. -This requires thexref::access-control/database-administration.adoc#access-control-database-administration-constraints[`SHOW CONSTRAINT`] privilege. + +[NOTE] +==== +Listing constraints requires the xref::access-control/database-administration.adoc#access-control-database-administration-constraints[`SHOW CONSTRAINTS` privilege]. +==== The simple version of the command allows for a `WHERE` clause and will give back the default set of output columns: [source, syntax, role="noheader", indent=0] ---- -SHOW [ALL - |UNIQUE +SHOW [ + ALL + |NODE UNIQUE[NESS] + |REL[ATIONSHIP] UNIQUE[NESS] + |UNIQUE[NESS] |NODE [PROPERTY] EXIST[ENCE] |REL[ATIONSHIP] [PROPERTY] EXIST[ENCE] |[PROPERTY] EXIST[ENCE] - |NODE KEY] CONSTRAINT[S] + |NODE KEY + |REL[ATIONSHIP] KEY + |KEY +] CONSTRAINT[S] [WHERE expression] ---- @@ -162,18 +223,66 @@ To get the full set of output columns, a yield clause is needed: [source, syntax, role="noheader", indent=0] ---- -SHOW [ALL - |UNIQUE +SHOW [ + ALL + |NODE UNIQUE[NESS] + |REL[ATIONSHIP] UNIQUE[NESS] + |UNIQUE[NESS] |NODE [PROPERTY] EXIST[ENCE] |REL[ATIONSHIP] [PROPERTY] EXIST[ENCE] |[PROPERTY] EXIST[ENCE] - |NODE KEY] CONSTRAINT[S] + |NODE KEY + |REL[ATIONSHIP] KEY + |KEY +] CONSTRAINT[S] YIELD { * | field[, ...] } [ORDER BY field[, ...]] [SKIP n] [LIMIT n] [WHERE expression] [RETURN field[, ...] [ORDER BY field[, ...]] [SKIP n] [LIMIT n]] ---- +The type filtering keywords filters the returned constraints on constraint type: + +[[constraints-syntax-list-type-filter]] +.Type filters +[options="header", width="100%", cols="4m,6a"] +|=== +| Filter | Description + +|ALL +| Returns all constraints, no filtering on constraint type. +This is the default if none is given. + +|NODE UNIQUE[NESS] +| Returns the node property uniqueness constraints. + +|REL[ATIONSHIP] UNIQUE[NESS] +| Returns the relationship property uniqueness constraints. + +|UNIQUE[NESS] +| Returns all property uniqueness constraints, for both nodes and relationships. + +|NODE [PROPERTY] EXIST[ENCE] +| Returns the node property existence constraints. + +|REL[ATIONSHIP] [PROPERTY] EXIST[ENCE] +| Returns the relationship property existence constraints. + +|[PROPERTY] EXIST[ENCE] +| Returns all property existence constraints, for both nodes and relationships. + +|NODE KEY +| Returns the node key constraints. + +|REL[ATIONSHIP] KEY +| Returns the relationship key constraints. + +|KEY +| Returns all node and relationship key constraints. + +|=== + + The returned columns from the show command is: .Listing constraints output @@ -188,7 +297,7 @@ The returned columns from the show command is: | Name of the constraint (explicitly set by the user or automatically assigned). label:default-output[] | type -| The ConstraintType of this constraint (`UNIQUENESS`, `NODE_PROPERTY_EXISTENCE`, `NODE_KEY`, or `RELATIONSHIP_PROPERTY_EXISTENCE`). label:default-output[] +| The ConstraintType of this constraint (`UNIQUENESS` (node uniqueness), `RELATIONSHIP_UNIQUENESS`, `NODE_PROPERTY_EXISTENCE`, `RELATIONSHIP_PROPERTY_EXISTENCE`, `NODE_KEY`, or `RELATIONSHIP_KEY`). label:default-output[] | entityType | Type of entities this constraint represents (nodes or relationship). label:default-output[] @@ -210,8 +319,3 @@ The returned columns from the show command is: |=== -[NOTE] -==== -Listing constraints requires the xref::access-control/database-administration.adoc#access-control-database-administration-constraints[`SHOW CONSTRAINTS` privilege]. -==== - diff --git a/modules/ROOT/pages/deprecations-additions-removals-compatibility.adoc b/modules/ROOT/pages/deprecations-additions-removals-compatibility.adoc index 649634b25..0e48d7ebd 100644 --- a/modules/ROOT/pages/deprecations-additions-removals-compatibility.adoc +++ b/modules/ROOT/pages/deprecations-additions-removals-compatibility.adoc @@ -12,6 +12,29 @@ Replacement syntax for deprecated and removed features are also indicated. [[cypher-deprecations-additions-removals-5.3]] == Version 5.3 +=== Deprecated features + +[cols="2", options="header"] +|=== +| Feature +| Details + +a| +//not sure what category this should be, it is more information about a coming breaking change than actual deprecation +label:returnValues[] +label:deprecated[] +[source, cypher, role="noheader"] +---- +SHOW NODE UNIQUENESS CONSTRAINTS YIELD type +---- +a| + +The current constraint type for node property uniqueness constraints, `UNIQUENESS`, will be updated to `NODE_UNIQUENESS` in Neo4j version 6.0. + +This will also be reflected in updates to some error messages and query statistics. + +|=== + === Updated features [cols="2", options="header"] @@ -62,6 +85,52 @@ a| A `COUNT` subquery now supports any non-writing query. For example, it now supports `UNION` and `CALL` clauses. +|=== + +=== New features + +[cols="2", options="header"] +|=== +| Feature +| Details + +a| +label:functionality[] +label:new[] +[source, cypher, role="noheader"] +---- +CREATE CONSTRAINT name FOR ()-[r:TYPE]-() REQUIRE r.prop IS UNIQUE + +CREATE CONSTRAINT name FOR ()-[r:TYPE]-() REQUIRE r.prop IS RELATIONSHIP KEY +---- +a| + +Added relationship xref:constraints/syntax.adoc#constraints-syntax-create-rel-key[key] and xref:constraints/syntax.adoc#constraints-syntax-create-rel-unique[uniqueness] constraints. + +a| +label:functionality[] +label:new[] +[source, cypher, role="noheader"] +---- +SHOW NODE UNIQUE[NESS] CONSTRAINTS + +SHOW REL[ATIONSHIP] UNIQUE[NESS] CONSTRAINTS + +SHOW UNIQUE[NESS] CONSTRAINTS + +SHOW REL[ATIONSHIP] KEY CONSTRAINTS + +SHOW KEY CONSTRAINTS +---- +a| + +Added filtering for the new constraint types to `SHOW CONSTRAINTS`. +Includes filtering for the node part, relationship part, or both parts of each type (`NODE KEY` filtering already exists previously). + +The existing `UNIQUE` filter will now return both node and relationship property uniqueness constraints. +All property uniqueness constraint type filters now allow both `UNIQUE` and `UNIQUENESS` keywords. + + |=== [[cypher-deprecations-additions-removals-5.2]] @@ -404,7 +473,7 @@ CREATE CONSTRAINT OPTIONS "{" btree-option: btree-value[, ...] "}" ---- a| -Node key and uniqueness constraints backed by B-tree indexes are removed. +Node key and property uniqueness constraints backed by B-tree indexes are removed. Replaced by: [source, cypher, role="noheader"] @@ -1389,7 +1458,7 @@ CREATE CONSTRAINT OPTIONS "{" btree-option: btree-value[, ...] "}" ---- a| -Node key and uniqueness constraints with B-tree options are deprecated. +Node key and property uniqueness constraints with B-tree options are deprecated. Replaced by: [source, cypher, role="noheader"] @@ -1501,7 +1570,7 @@ REQUIRE (n.propertyName_1, …, n.propertyName_n) IS UNIQUE [OPTIONS "{" option: value[, ...] "}"] ---- a| -Unique property constraints now allow multiple properties, ensuring that the combination of property values are unique. +Property uniqueness constraints now allow multiple properties, ensuring that the combination of property values are unique. a| label:functionality[] @@ -1514,7 +1583,7 @@ ON (n:LabelName) ASSERT (n.propertyName_1, …, n.propertyName_n) IS UNIQUE ---- a| -Unique property constraints now allow multiple properties. +Property uniqueness constraints now allow multiple properties. Replaced by: [source, cypher, role="noheader"] @@ -1581,7 +1650,7 @@ CREATE CONSTRAINT OPTIONS "{" indexProvider: 'range-1.0' "}" ---- a| -Allows creating node key and uniqueness constraints backed by range indexes by providing the range index provider in the `OPTIONS` map. +Allows creating node key and property uniqueness constraints backed by range indexes by providing the range index provider in the `OPTIONS` map. a| @@ -2526,7 +2595,7 @@ label:new[] CREATE CONSTRAINT ... IS UNIQUE [OPTIONS {...}] ---- a| -Allows setting index provider and index configuration for the backing index when creating a uniqueness constraint. +Allows setting index provider and index configuration for the backing index when creating a property uniqueness constraint. a| label:syntax[] @@ -3192,7 +3261,7 @@ label:new[] DROP CONSTRAINT name ---- a| -xref:constraints/syntax.adoc#administration-constraints-syntax-drop[New command] for dropping a constraint by name, no matter the type. +xref:constraints/syntax.adoc#constraints-syntax-drop[New command] for dropping a constraint by name, no matter the type. a| @@ -3304,7 +3373,7 @@ An example of this is `CALL db.index.explicit.searchNodes('my_index','email:me*' | `MATCH (n)-[x:A\|:B\|:C*]-() RETURN n` | Syntax | Deprecated | Replaced by `MATCH (n)-[x:A\|B\|C*]-() RETURN n` | link:/docs/java-reference/5/extending-neo4j/aggregation-functions#extending-neo4j-aggregation-functions[User-defined aggregation functions] | Functionality | Added | | xref:indexes-for-search-performance.adoc[Composite indexes] | Index | Added | -| xref:constraints/examples.adoc#administration-constraints-node-key[Node Key] | Index | Added | Neo4j Enterprise Edition only +| xref:constraints/examples.adoc#constraints-examples-node-key[Node Key] | Index | Added | Neo4j Enterprise Edition only | `CYPHER runtime=compiled` (Compiled runtime) | Functionality | Added | Neo4j Enterprise Edition only | xref:functions/list.adoc#functions-reverse-list[reverse()] | Function | Extended | Now also allows a list as input | xref:functions/aggregating.adoc#functions-max[max()], xref:functions/aggregating.adoc#functions-min[min()] | Function | Extended | Now also supports aggregation over a set containing both strings and numbers diff --git a/modules/ROOT/pages/execution-plans/operator-summary.adoc b/modules/ROOT/pages/execution-plans/operator-summary.adoc index 23a525efd..06762ad46 100644 --- a/modules/ROOT/pages/execution-plans/operator-summary.adoc +++ b/modules/ROOT/pages/execution-plans/operator-summary.adoc @@ -53,13 +53,13 @@ Tests for the absence of a pattern predicate. | | xref::execution-plans/operators.adoc#query-plan-assert-same-node[AssertSameNode] -| Used to ensure that no unique constraints are violated. +| Used to ensure that no property uniqueness constraints are violated. | | | | xref::execution-plans/operators.adoc#query-plan-asserting-multi-node-index-seek[AssertingMultiNodeIndexSeek] -| Used to ensure that no unique constraints are violated. +| Used to ensure that no property uniqueness constraints are violated. | | | @@ -88,26 +88,8 @@ Tests for the absence of a pattern predicate. | label:yes[] | -| xref::execution-plans/operators.adoc#query-plan-create-node-key-constraint[CreateNodeKeyConstraint] -| Creates a node key constraint on a set of properties for all nodes with a certain label. -| -| label:yes[] -| - -| xref::execution-plans/operators.adoc#query-plan-create-node-property-existence-constraint[CreateNodePropertyExistenceConstraint] -| Creates an existence constraint on a property for all nodes with a certain label. -| -| label:yes[] -| - -| xref::execution-plans/operators.adoc#query-plan-create-relationship-property-existence-constraint[CreateRelationshipPropertyExistenceConstraint] -| Creates an existence constraint on a property for all relationships of a certain type. -| -| label:yes[] -| - -| xref::execution-plans/operators.adoc#query-plan-create-unique-constraint[CreateUniqueConstraint] -| Creates a unique constraint on a set of properties for all nodes with a certain label. +| xref::execution-plans/operators.adoc#query-plan-create-constraint[CreateConstraint] +| Creates a constraint for either nodes or relationships. | | label:yes[] | diff --git a/modules/ROOT/pages/execution-plans/operators.adoc b/modules/ROOT/pages/execution-plans/operators.adoc index 9dcfaf2d2..cd9c4f9e1 100644 --- a/modules/ROOT/pages/execution-plans/operators.adoc +++ b/modules/ROOT/pages/execution-plans/operators.adoc @@ -1141,9 +1141,9 @@ Total database accesses: 0, total allocated memory: 184 == Asserting Multi Node Index Seek // AssertingMultiNodeIndexSeek -The `AssertingMultiNodeIndexSeek` operator is used to ensure that no unique constraints are violated. +The `AssertingMultiNodeIndexSeek` operator is used to ensure that no property uniqueness constraints are violated. The example looks for the presence of a team with the supplied name and id, and if one does not exist, it will be created. -Owing to the existence of two unique constraints on `:Team(name)` and `:Team(id)`, any node that would be found by the `UniqueIndexSeek` must be the very same node, or the constraints would be violated. +Owing to the existence of two property uniqueness constraints on `:Team(name)` and `:Team(id)`, any node that would be found by the `UniqueIndexSeek` operator must be the very same node or the constraints would be violated. .AssertingMultiNodeIndexSeek @@ -2635,9 +2635,9 @@ Total database accesses: 487, total allocated memory: 5256 == Assert Same Node // AssertSameNode -The `AssertSameNode` operator is used to ensure that no unique constraints are violated in the slotted and interpreted runtime. +The `AssertSameNode` operator is used to ensure that no property uniqueness constraints are violated in the slotted and interpreted runtime. The example looks for the presence of a team with the supplied name and id, and if one does not exist, it will be created. -Owing to the existence of two unique constraints on `:Team(name)` and `:Team(id)`, any node that would be found by the `UniqueIndexSeek` must be the very same node, or the constraints would be violated. +Owing to the existence of two property uniqueness constraints on `:Team(name)` and `:Team(id)`, any node that would be found by the `UniqueIndexSeek` operator must be the very same node or the constraints would be violated. .AssertSameNode @@ -4879,15 +4879,23 @@ Total database accesses: 106, total allocated memory: 184 ====== -[[query-plan-create-unique-constraint]] -== Create Unique Constraint -// CreateUniqueConstraint +[[query-plan-create-constraint]] +== Create Constraint +// CreateConstraint -The `CreateUniqueConstraint` operator creates a unique constraint on a set of properties for all nodes having a certain label. -The following query will create a unique constraint with the name `uniqueness` on the `name` property of nodes with the `Country` label. +The `CreateConstraint` operator creates a constraint. -.CreateUniqueConstraint +This constraint can have any of the available constraint types: + +* Property uniqueness constraints +* Property existence constraints label:enterprise-edition[] +* Node or relationship key constraints label:enterprise-edition[] + +The following query will create a property uniqueness constraint with the name `uniqueness` on the `name` property of nodes with the `Country` label. + + +.CreateConstraint ====== .Query @@ -4925,7 +4933,7 @@ Total database accesses: ? To not get an error creating the same constraint twice, we use the `DoNothingIfExists` operator for constraints. This will make sure no other constraint with the given name or another constraint of the same type and schema already exists before the specific `CreateConstraint` operator creates the constraint. If it finds a constraint with the given name or with the same type and schema it will stop the execution and no new constraint is created. -The following query will create a unique constraint with the name `uniqueness` on the `name` property of nodes with the `Country` label only if no constraint named `uniqueness` or unique constraint on `+(:Country {name})+` already exists. +The following query will create a property uniqueness constraint with the name `uniqueness` on the `name` property of nodes with the `Country` label only if no constraint named `uniqueness` or property uniqueness constraint on `+(:Country {name})+` already exists. .DoNothingIfExists(CONSTRAINT) @@ -4960,125 +4968,6 @@ Total database accesses: ? ====== - -[[query-plan-create-node-property-existence-constraint]] -== Create Node Property Existence Constraint -// CreateNodePropertyExistenceConstraint - -The `CreateNodePropertyExistenceConstraint` operator creates an existence constraint with the name `existence` on a property for all nodes having a certain label. -This will only appear in Enterprise Edition. - - -.CreateNodePropertyExistenceConstraint -====== - -.Query -[source, cypher, role="noplay"] ----- -CREATE CONSTRAINT existence -FOR (p:Person) REQUIRE p.name IS NOT NULL ----- - -.Query Plan -[source, query plan, subs="attributes+", role="noheader"] ----- -Planner ADMINISTRATION - -Runtime SCHEMA - -Runtime version {neo4j-version-minor} - -+-------------------+------------------------------------------------------------------+ -| Operator | Details | -+-------------------+------------------------------------------------------------------+ -| +CreateConstraint | CONSTRAINT existence FOR (p:Person) REQUIRE (p.name) IS NOT NULL | -+-------------------+------------------------------------------------------------------+ - -Total database accesses: ? ----- - -====== - - -[[query-plan-create-node-key-constraint]] -== Create Node Key Constraint -// CreateNodeKeyConstraint - -The `CreateNodeKeyConstraint` operator creates a node key constraint with the name `node_key` which ensures -that all nodes with a particular label have a set of defined properties whose combined value is unique, and where all properties in the set are present. -This will only appear in Enterprise Edition. - - -.CreateNodeKeyConstraint -====== - -.Query -[source, cypher, role="noplay"] ----- -CREATE CONSTRAINT node_key -FOR (e:Employee) REQUIRE (e.firstname, e.surname) IS NODE KEY ----- - -.Query Plan -[source, query plan, subs="attributes+", role="noheader"] ----- -Planner ADMINISTRATION - -Runtime SCHEMA - -Runtime version {neo4j-version-minor} - -+-------------------+-----------------------------------------------------------------------------------+ -| Operator | Details | -+-------------------+-----------------------------------------------------------------------------------+ -| +CreateConstraint | CONSTRAINT node_key FOR (e:Employee) REQUIRE (e.firstname, e.surname) IS NODE KEY | -+-------------------+-----------------------------------------------------------------------------------+ - -Total database accesses: ? ----- - -====== - - -[[query-plan-create-relationship-property-existence-constraint]] -== Create Relationship Property Existence Constraint -// CreateRelationshipPropertyExistenceConstraint - -The `CreateRelationshipPropertyExistenceConstraint` operator creates an existence constraint with the name `existence` on a property for all relationships of a certain type. -This will only appear in Enterprise Edition. - - -.CreateRelationshipPropertyExistenceConstraint -====== - -.Query -[source, cypher, role="noplay"] ----- -CREATE CONSTRAINT existence -FOR ()-[l:LIKED]-() REQUIRE l.when IS NOT NULL ----- - -.Query Plan -[source, query plan, subs="attributes+", role="noheader"] ----- -Planner ADMINISTRATION - -Runtime SCHEMA - -Runtime version {neo4j-version-minor} - -+-------------------+-----------------------------------------------------------------------+ -| Operator | Details | -+-------------------+-----------------------------------------------------------------------+ -| +CreateConstraint | CONSTRAINT existence FOR ()-[l:LIKED]-() REQUIRE (l.when) IS NOT NULL | -+-------------------+-----------------------------------------------------------------------+ - -Total database accesses: ? ----- - -====== - - [[query-plan-drop-constraint]] == Drop Constraint // DropConstraint diff --git a/modules/ROOT/pages/indexes-for-search-performance.adoc b/modules/ROOT/pages/indexes-for-search-performance.adoc index bea730a12..59a2511ff 100644 --- a/modules/ROOT/pages/indexes-for-search-performance.adoc +++ b/modules/ROOT/pages/indexes-for-search-performance.adoc @@ -441,6 +441,7 @@ Note that the index is not immediately available, but is created in the backgrou ====== //// +Set-up to get expected behavior: CREATE (_0:`Person` {`age`:35, `country`:"UK", `firstname`:"John", `highScore`:54321, `middlename`:"Ron", `name`:"john", `surname`:"Smith"}) CREATE (_1:`Person` {`age`:40, `country`:"Sweden", `firstname`:"Andy", `highScore`:12345, `middlename`:"Mark", `name`:"andy", `surname`:"Jones"}) //// @@ -486,6 +487,7 @@ Note that the index is not immediately available, but is created in the backgrou ====== //// +Set-up to get expected behavior: CREATE (_0:`Person` {`age`:35, `country`:"UK", `firstname`:"John", `highScore`:54321, `middlename`:"Ron", `name`:"john", `surname`:"Smith"}) CREATE (_1:`Person` {`age`:40, `country`:"Sweden", `firstname`:"Andy", `highScore`:12345, `middlename`:"Mark", `name`:"andy", `surname`:"Jones"}) CREATE (_0)-[:`KNOWS` {`lastMet`:2021, `lastMetIn`:"Stockholm", `metIn`:"Malmo", `since`:1992}]->(_1) @@ -525,6 +527,7 @@ If it is not known whether an index exists or not, add `IF NOT EXISTS` to ensure ====== //// +Set-up to get expected behavior: CREATE RANGE index `node_range_index_name` for (n:`Person`) ON (n.`surname`) CREATE (_0:`Person` {`age`:35, `country`:"UK", `firstname`:"John", `highScore`:54321, `middlename`:"Ron", `name`:"john", `surname`:"Smith"}) @@ -610,6 +613,7 @@ Note that the composite index is not immediately available, but is created in th The following statement will create a named composite range index on all nodes labeled with `Person` and which have both an `age` and `country` property: //// +Set-up to get expected behavior: CREATE (_0:`Person` {`age`:35, `country`:"UK", `firstname`:"John", `highScore`:54321, `middlename`:"Ron", `name`:"john", `surname`:"Smith"}) CREATE (_1:`Person` {`age`:40, `country`:"Sweden", `firstname`:"Andy", `highScore`:12345, `middlename`:"Mark", `name`:"andy", `surname`:"Jones"}) ---- @@ -659,6 +663,7 @@ Note that the composite index is not immediately available, but is created in th The following statement will create a named composite range index on all relationships labeled with `PURCHASED` and which have both a `date` and `amount` property: //// +Set-up to get expected behavior: CREATE (_0:`Person` {`age`:35, `country`:"UK", `firstname`:"John", `highScore`:54321, `middlename`:"Ron", `name`:"john", `surname`:"Smith"}) CREATE (_1:`Person` {`age`:40, `country`:"Sweden", `firstname`:"Andy", `highScore`:12345, `middlename`:"Mark", `name`:"andy", `surname`:"Jones"}) CREATE (_1)-[:`KNOWS`]->(_0) @@ -708,6 +713,7 @@ The index is not immediately available, but is created in the background. ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -760,6 +766,7 @@ The index is not immediately available, but is created in the background. ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -810,6 +817,7 @@ Only one valid value exists for the index provider, `token-lookup-1.0`, which is // hence the `node label lookup index` and `relationship type lookup index` variations above. //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -943,6 +951,7 @@ If it is not known whether an index exists or not, add `IF NOT EXISTS` to ensure ====== //// +Set-up to get expected behavior: CREATE POINT index for (n:`Person`) ON (n.`sublocation`) //// @@ -1124,6 +1133,7 @@ The index is not immediately available, but is created in the background. ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1176,6 +1186,7 @@ The index is not immediately available, but is created in the background. ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1220,6 +1231,7 @@ If it is not known whether an index exists or not, add `IF NOT EXISTS` to ensure ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1260,6 +1272,7 @@ The valid values for the index provider are `text-2.0` and `text-1.0` (deprecate ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1296,6 +1309,7 @@ Create an index on the property `title` on nodes with the `Book` label, when tha ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1331,6 +1345,7 @@ Create a named index on the property `numberOfPages` on nodes with the `Book` la ====== //// +Set-up to get expected behavior: CREATE (n0:Label1:Label2 {prop1: 3, prop2: 'Green') CREATE (n1:Label1:Label3 {prop1: 5, prop2: 'Pink') CREATE (n2:Label1 {prop1: 7, prop2: 'Blue') @@ -1366,6 +1381,7 @@ Create an index on the property `isbn` on nodes with the `Book` label, when an i ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT FOR (book:Book) REQUIRE (book.isbn) IS UNIQUE //// @@ -1397,6 +1413,7 @@ Create a named index on the property `numberOfPages` on nodes with the `Book` la ====== //// +Set-up to get expected behavior: CREATE CONSTRAINT bookRecommendations FOR (book:Book) REQUIRE (book.recommend) IS NOT NULL //// @@ -1499,6 +1516,7 @@ If all columns are required, use `SHOW INDEXES YIELD *`. ====== //// +Set-up to get expected behavior: CREATE RANGE INDEX `index_664b28a2` for (n:`Person`) ON (n.`middlename`); CREATE RANGE INDEX `index_58a1c03e` for (n:`Person`) ON (n.`location`); CREATE RANGE INDEX `index_8a688dca` for (n:`Person`) ON (n.`highScore`); @@ -1561,6 +1579,7 @@ An example is to only show indexes not belonging to constraints. ====== //// +Set-up to get expected behavior: CREATE RANGE INDEX `index_664b28a2` for (n:`Person`) ON (n.`middlename`); CREATE RANGE INDEX `index_8a688dca` for (n:`Person`) ON (n.`highScore`); CREATE RANGE INDEX `index_b87724c3` for (n:`Person`) ON (n.`firstname`); @@ -1628,6 +1647,7 @@ The name of the index can be found using the xref::indexes-for-search-performanc ====== //// +Set-up to get expected behavior: CREATE index `index_example` for (n:`Example`) ON (n.`example`); //// diff --git a/modules/ROOT/pages/introduction/index.adoc b/modules/ROOT/pages/introduction/index.adoc index 928be4699..2ae29a020 100644 --- a/modules/ROOT/pages/introduction/index.adoc +++ b/modules/ROOT/pages/introduction/index.adoc @@ -53,7 +53,8 @@ And these are examples of clauses that are used to update the graph: * `SET` (and `REMOVE`): Set values to properties and add labels on nodes using `SET` and use `REMOVE` to remove them. -* `MERGE`: Match existing or create new nodes and patterns. This is especially useful together with unique constraints. +* `MERGE`: Match existing or create new nodes and patterns. +This is especially useful together with property uniqueness constraints. .Cypher Query diff --git a/modules/ROOT/pages/introduction/neo4j-databases-graphs.adoc b/modules/ROOT/pages/introduction/neo4j-databases-graphs.adoc index 9cbf5fd18..fa3dce9f6 100644 --- a/modules/ROOT/pages/introduction/neo4j-databases-graphs.adoc +++ b/modules/ROOT/pages/introduction/neo4j-databases-graphs.adoc @@ -75,9 +75,15 @@ All users have full access rights. | Constraints a| -xref::constraints/examples.adoc#administration-constraints-prop-exist-nodes[Existence constraints], xref::constraints/examples.adoc#administration-constraints-unique-nodes[uniqueness constraints], and xref::constraints/examples.adoc#administration-constraints-node-key[`NODE KEY` constraints]. +All constraints: +xref::constraints/examples.adoc#constraints-examples-node-property-existence[node existence constraints], +xref::constraints/examples.adoc#constraints-examples-relationship-property-existence[relationship existence constraints], +xref::constraints/examples.adoc#constraints-examples-node-uniqueness[node property uniqueness constraints], +xref::constraints/examples.adoc#constraints-examples-relationship-uniqueness[relationship property uniqueness constraints], +xref::constraints/examples.adoc#constraints-examples-node-key[node key constraints], and +xref::constraints/examples.adoc#constraints-examples-relationship-key[relationship key constraints]. a| -Only xref::constraints/examples.adoc#administration-constraints-unique-nodes[uniqueness constraints]. +Only xref::constraints/examples.adoc#constraints-examples-node-uniqueness[node] and xref::constraints/examples.adoc#constraints-examples-relationship-uniqueness[relationship] property uniqueness constraints. |=== diff --git a/modules/ROOT/pages/keyword-glossary.adoc b/modules/ROOT/pages/keyword-glossary.adoc index e0f8cf603..238a96168 100644 --- a/modules/ROOT/pages/keyword-glossary.adoc +++ b/modules/ROOT/pages/keyword-glossary.adoc @@ -42,22 +42,30 @@ Typically used when modifying or importing large amounts of data. | Writing | Create nodes and relationships. -| xref::constraints/syntax.adoc#administration-constraints-syntax-create-node-exists[CREATE CONSTRAINT [existence\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE n.property IS NOT NULL [OPTIONS {}\]] +| xref::constraints/syntax.adoc#constraints-syntax-create-node-exists[CREATE CONSTRAINT [existence\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE n.property IS NOT NULL [OPTIONS {}\]] | Schema | Create a constraint ensuring that all nodes with a particular label have a certain property. -| xref::constraints/syntax.adoc#administration-constraints-syntax-create-node-key[CREATE CONSTRAINT [node_key\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE (n.prop1[, ..., n.propN\]) IS NODE KEY [OPTIONS {optionKey: optionValue[, ...\]}\]] +| xref::constraints/syntax.adoc#constraints-syntax-create-rel-exists[CREATE CONSTRAINT [existence\] [IF NOT EXISTS\] FOR ()-"["r:REL_TYPE"\]"-() REQUIRE r.property IS NOT NULL [OPTIONS {}\]] +| Schema +| Create a constraint that ensures all relationships with a particular type have a certain property. + +| xref::constraints/syntax.adoc#constraints-syntax-create-node-key[CREATE CONSTRAINT [node_key\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE (n.prop1[, ..., n.propN\]) IS [NODE\] KEY [OPTIONS {optionKey: optionValue[, ...\]}\]] | Schema | Create a constraint that ensures all nodes with a particular label have all the specified properties and that the combination of property values is unique; i.e. ensures existence and uniqueness. -| xref::constraints/syntax.adoc#administration-constraints-syntax-create-rel-exists[CREATE CONSTRAINT [existence\] [IF NOT EXISTS\] FOR ()-"["r:REL_TYPE"\]"-() REQUIRE r.property IS NOT NULL [OPTIONS {}\]] +| xref::constraints/syntax.adoc#constraints-syntax-create-rel-key[CREATE CONSTRAINT [rel_key\] [IF NOT EXISTS\] FOR ()-"["r:REL_TYPE"\]"-() REQUIRE (r.prop1[, ..., r.propN\]) IS [REL[ATIONSHIP\]\] KEY [OPTIONS {optionKey: optionValue[, ...\]}\]] | Schema -| Create a constraint that ensures all relationships with a particular type have a certain property. +| Create a constraint that ensures all relationships with a particular type have all the specified properties and that the combination of property values is unique; i.e. ensures existence and uniqueness. -| xref::constraints/syntax.adoc#administration-constraints-syntax-create-unique[CREATE CONSTRAINT [uniqueness\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE (n.prop1[, ..., n.propN\]) IS UNIQUE [OPTIONS {optionKey: optionValue[, ...\]}\]] +| xref::constraints/syntax.adoc#constraints-syntax-create-node-unique[CREATE CONSTRAINT [uniqueness\] [IF NOT EXISTS\] FOR (n:Label) REQUIRE (n.prop1[, ..., n.propN\]) IS [NODE\] UNIQUE [OPTIONS {optionKey: optionValue[, ...\]}\]] | Schema | Create a constraint that ensures the uniqueness of the combination of node label and property values for a particular property key combination across all nodes. +| xref::constraints/syntax.adoc#constraints-syntax-create-rel-unique[CREATE CONSTRAINT [uniqueness\] [IF NOT EXISTS\] FOR ()-"["r:REL_TYPE"\]"-() REQUIRE (r.prop1[, ..., r.propN\]) IS [REL[ATIONSHIP\]\] UNIQUE [OPTIONS {optionKey: optionValue[, ...\]}\]] +| Schema +| Create a constraint that ensures the uniqueness of the combination of relationship type and property values for a particular property key combination across all relationships. + | xref::indexes-for-full-text-search.adoc[CREATE FULLTEXT INDEX [name\] [IF NOT EXISTS\] FOR (n:Label["\|" ... "\|" LabelN\]) ON EACH "[" n.property[, ..., n.propertyN\] "\]" [OPTIONS {optionKey: optionValue[, ...\]}\]] | Schema | Create a fulltext index on nodes. @@ -168,7 +176,7 @@ Either the pattern already exists, or it needs to be created. | Writing | Update labels on nodes and properties on nodes and relationships. -| xref::constraints/syntax.adoc#administration-constraints-syntax-list[SHOW [ALL\|UNIQUE\|NODE [PROPERTY\] EXIST[ENCE\]\|REL[ATIONSHIP\] [PROPERTY\] EXIST[ENCE\]\|[PROPERTY\] EXIST[ENCE\]\|NODE KEY\] CONSTRAINT[S\]] +| xref::constraints/syntax.adoc#constraints-syntax-list[SHOW [ALL\|UNIQUE\|NODE [PROPERTY\] EXIST[ENCE\]\|REL[ATIONSHIP\] [PROPERTY\] EXIST[ENCE\]\|[PROPERTY\] EXIST[ENCE\]\|NODE KEY\] CONSTRAINT[S\]] | Schema | List constraints in the database, either all or filtered on type.