|
2 | 2 | :page-role: cypher-25-only new-neo4j-2025.xx enterprise-edition |
3 | 3 | = Drop elements in a graph type |
4 | 4 |
|
| 5 | +The `ALTER CURRENT GRAPH TYPE DROP` command allows you to drop elements from a graph type. |
| 6 | + |
| 7 | +[NOTE] |
| 8 | +Dropping elements from a graph type the link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/database-administration/#access-control-database-administration-constraints[`DROP CONSTRAINT` privilege]. |
| 9 | + |
| 10 | +.Set graph type |
| 11 | +[source, cypher] |
| 12 | +---- |
| 13 | +ALTER CURRENT GRAPH TYPE SET { |
| 14 | + (p:Person => :Resident {name :: STRING, ssn :: INTEGER}) |
| 15 | + REQUIRE (p.name, p.ssn) IS KEY, |
| 16 | + (:Pet => :Resident&Animal {insuranceNumber :: INTEGER IS KEY, healthCertificate :: STRING IS UNIQUE, name :: STRING}), |
| 17 | + (:Robot => :Resident&Machine {application :: STRING NOT NULL, id :: INTEGER NOT NULL}), |
| 18 | + (:City => {name :: STRING NOT NULL, population :: INTEGER}), |
| 19 | + (:Resident)-[:LIVES_IN => {since:: ANY NOT NULL}]->(:City), |
| 20 | + CONSTRAINT company_name FOR (c:Company) REQUIRE c.name IS KEY, |
| 21 | + CONSTRAINT resident_address FOR (resident:Resident) REQUIRE resident.address :: STRING, |
| 22 | + CONSTRAINT ownership_id FOR ()-[owner:OWNER_OF]->() REQUIRE owner.ownershipId IS UNIQUE, |
| 23 | + (:Company => {name :: STRING, address :: STRING IS UNIQUE}), |
| 24 | + (:Person)-[:WORKS_FOR => {role :: STRING})->(:Company) |
| 25 | +} |
| 26 | +---- |
| 27 | + |
| 28 | +[[drop-element-types]] |
| 29 | +== Drop node and relationship element types |
| 30 | + |
| 31 | +To fully drop a node or relationship element type, it is only necessary to define its identifying node label/relationship type appended with `\=>`. |
| 32 | + |
| 33 | +.Drop node and relationship element types |
| 34 | +[source, cypher] |
| 35 | +---- |
| 36 | +ALTER CURRENT GRAPH TYPE DROP { |
| 37 | + (:Pet => ), |
| 38 | + ()-[:LIVES_IN => ]->() |
| 39 | +} |
| 40 | +---- |
| 41 | + |
| 42 | +You can also define the full element type when dropping an element type. |
| 43 | +Partial definitions, however, are not allowed. |
| 44 | + |
| 45 | +[cols="3*", options="header"] |
| 46 | +|=== |
| 47 | +| Allowed -- identifying node label/relationship type only |
| 48 | +| Allowed -- full definition |
| 49 | +| Not allowed -- partial definition |
| 50 | + |
| 51 | +a| |
| 52 | +[source] |
| 53 | +---- |
| 54 | +ALTER CURRENT GRAPH TYPE DROP { |
| 55 | + (:Pet => ) |
| 56 | +} |
| 57 | +---- |
| 58 | + |
| 59 | +a| |
| 60 | +[source] |
| 61 | +---- |
| 62 | +ALTER CURRENT GRAPH TYPE DROP { |
| 63 | + (:Pet => :Resident&Animal {insuranceNumber :: INTEGER IS KEY, healthCertificate :: STRING IS UNIQUE, name :: STRING}) |
| 64 | +} |
| 65 | +---- |
| 66 | + |
| 67 | +a| |
| 68 | +[source] |
| 69 | +---- |
| 70 | +ALTER CURRENT GRAPH TYPE DROP { |
| 71 | + (:Pet => :Resident&Animal) |
| 72 | +} |
| 73 | +---- |
| 74 | + |
| 75 | +|=== |
| 76 | + |
| 77 | +Dropping a node or relationship element type in this way can remove the following constraints: |
| 78 | + |
| 79 | +* Node label existence constraints |
| 80 | +* Source and target node label constraints |
| 81 | +* Property type and property existence constraints defined against a node or relationship element type. |
| 82 | + |
| 83 | +These constraints cannot be removed separately from the node or relationship element type they support. |
| 84 | + |
| 85 | +Not allowed -- drop a dependent constraint from a node or relationship element type |
| 86 | +[source, cypher, role=test-fail] |
| 87 | +---- |
| 88 | +ALTER CURRENT GRAPH TYPE DROP { |
| 89 | + CONSTRAINT constraint_22a8753a // <1> |
| 90 | +} |
| 91 | +---- |
| 92 | + |
| 93 | +<1> `constraint_22a8753a` is a property existence constraint defined on the `City` node element type (constraint names are returned by the xref:schema/graph-types/list-graph-types.adoc#graph-type-elements-in-show-constraints[`SHOW CONSTRAINTS`] command). |
| 94 | + |
| 95 | +*Removing a node or relationship element type does not, however, remove any property uniqueness or key constraints defined against a node or relationship element type.* |
| 96 | +*Rather, it leaves them as constraints on non-identifying node-labels and relationship types.* |
| 97 | +*These must therefore be removed separately, using their constraint name.* |
| 98 | +For more information, see xref:schema/graph-types/drop-graph-type-elements.adoc#drop-constraints-on-identifying-and-non-identifying-labels-and-types[Drop constraints on identifying and non-identifying node labels and relationship types] below. |
| 99 | + |
| 100 | +If the identifying label of a node element type is also constrained as a source/target node label in a relationship element type, then the node element type cannot be dropped without also dropping or first altering the relationship element type. |
| 101 | + |
| 102 | +.Not allowed -- drop a node element type whose identifying label is also the source/target node label in a relationship element type |
| 103 | +[source, cypher, role=test-fail] |
| 104 | +---- |
| 105 | +ALTER CURRENT GRAPH TYPE DROP { |
| 106 | + (:Person => ) |
| 107 | +} |
| 108 | +---- |
| 109 | + |
| 110 | +.Error message |
| 111 | +[source, error] |
| 112 | +---- |
| 113 | +22NCE: data exception - node element type in use. The node element type identified by the label `Person` is referenced in the graph type element '()-[:`WORKS_FOR` =>]->()' and cannot be dropped. |
| 114 | +---- |
5 | 115 |
|
6 | 116 | [[drop-constraints-on-identifying-and-non-identifying-labels-and-types]] |
7 | | -== Drop constraints on identifying and non-identifying node labels and relationship types |
| 117 | +== Drop constraints on identifying and non-identifying node labels and relationship types |
| 118 | + |
| 119 | +Key and property uniqueness constraints can only be dropped by using the `CONSTRAINT [name]` syntax in the `ALTER CURRENT GRAPH TYPE DROP` command, regardless if they were defined on an identifying or non-identifying label. |
| 120 | +Property existence and property type constraints defined on non-identifying node labels and relationship types are also dropped in this way. |
| 121 | +The names of constraints are returned by the xref:schema/constraints/list-constraints.adoc[`SHOW CONSTRAINTS`] command. |
| 122 | + |
| 123 | +.Drop explicitly named and generated name key and property uniqueness constraint |
| 124 | +[source, cypher] |
| 125 | +---- |
| 126 | +ALTER CURRENT GRAPH TYPE DROP { |
| 127 | + CONSTRAINT ownership_id, |
| 128 | + CONSTRAINT constraint_302a3693 |
| 129 | +} |
| 130 | +---- |
| 131 | + |
| 132 | +These constraints can also be dropped from the graph type using the xref:schema/constraints/drop-constraints.adoc[`DROP CONSTRAINT`] command. |
| 133 | + |
| 134 | +.Drop property type constraint on a non-identifying node label |
| 135 | +[source, cypher] |
| 136 | +---- |
| 137 | +DROP CONSTRAINT resident_address |
| 138 | +---- |
| 139 | + |
| 140 | +[[drop-full-graph-type]] |
| 141 | +== Drop a full graph type |
| 142 | + |
| 143 | +To drop a full graph type, use the `ALTER CURRENT GRAPH TYPE SET` command. |
| 144 | +This will replace the existing graph type with a new (possibly empty) graph type. |
| 145 | + |
| 146 | +.Drop full graph type and replace it with an empty graph type |
| 147 | +[source, cypher] |
| 148 | +---- |
| 149 | +ALTER CURRENT GRAPH TYPE SET { } |
| 150 | +---- |
| 151 | + |
| 152 | +.Drop full graph type and replace with a new graph type |
| 153 | +[source, cypher] |
| 154 | +---- |
| 155 | +ALTER CURRENT GRAPH TYPE SET { |
| 156 | + (n:Label => :LabelTwo {property :: STRING}) |
| 157 | +} |
| 158 | +---- |
0 commit comments