SET statement¶
The SET statement modifies the properties of elements in a graph or temporary graph.
If a single SET statement sets the same property multiple times simultaneously, the system randomly selects one of the values.
Note
You can also use SET to perform assignments, aggregations, and function calls. For more information, see SET.
Note
An operation involving a non-default schema or an object in it requires access to the schema, either implicitly or through an explicit USAGE grant, as well as the privilege required for the operation. The schema access check and the privilege check for the requested operation are independent; satisfying one does not satisfy the other. Schema access control does not apply to temporary graphs or built-in procedures and functions. For more information, see Schema access control.
Prerequisites¶
- A graph or temporary graph is created. For more information, see CREATE GRAPH and CREATE TEMPORARY GRAPH.
- To modify the properties of elements in a graph, you must be granted to perform the action
UPDATE ANY GRAPHon the used schema or the actionUPDATEon the resource object that you want to modify. For more information, see Roles and privileges.
- To modify the properties of elements in a temporary graph, you must be the user with the role
ADMINor the owner of the temporary graph.
Limitations¶
- Only the node or edge retrieved by a unique node type or edge type can be modified. If the node type or edge type of the node or edge to be modified is not unique, the modification operation cannot be performed.
- Modifying the value of a property specified as the primary key or multiedge key is not supported.
Syntax¶
- set_statement
set_statement ::=
'SET' variable_reference '.' property_name '=' value_expression ( ',' variable_reference '.' property_name '=' value_expression )*
Parameters¶
variable_reference¶
The reference of the variable that specifies the graph element whose property is to be modified. variable_reference is a regular identifier.
property_name¶
The name of the property to be set. property_name is an identifier. For more information, see Keywords and identifiers.
value_expression¶
An expression whose value is used as the new value. For more information, see Value expressions.
Examples¶
The following examples use the graph movie and the graph type movie_type created in the example dataset to demonstrate how to use the SET statement.
It assumes that the movie graph is set as the current working graph by executing the SESSION SET GRAPH movie statement; otherwise, you need to specify the graph name at the beginning of the statement as USE movie. For more information, see USE.
Update a single property of a node¶
-
Query the actor named
Mark Hamill.MATCH (v@Actor{name:"Mark Hamill"}) RETURN vReturn:
+---------------------------------------------------------------------------------+ | v | +---------------------------------------------------------------------------------+ | (288449535447924737@Actor:Person{birthDate:1951-09-25,id:2,name:"Mark Hamill"}) | +---------------------------------------------------------------------------------+ -
Update the name
Mark HamilltoMark Hamill2.MATCH (v@Actor{name:"Mark Hamill"}) SET v.name="Mark Hamill2" -
Query the updated actor.
MATCH (v@Actor{name:"Mark Hamill2"}) RETURN vReturn:
+----------------------------------------------------------------------------------+ | v | +----------------------------------------------------------------------------------+ | (288449535447924737@Actor:Person{birthDate:1951-09-25,id:2,name:"Mark Hamill2"}) | +----------------------------------------------------------------------------------+
Update multiple properties of a node¶
Query the actor named Mark Hamill and update its name and birth date.
MATCH (v@Actor{name:"Mark Hamill"}) SET v.name="Mark Hamill2", v.birthDate=date("1951-09-26")
Update a single property of an edge¶
-
Query the edge between the user with ID
414and the movieStar Trek: The Motion Picture.MATCH(v1@`User`{id:414})-[e@Watch]->(v2@Movie{name:"Star Trek: The Motion Picture"}) RETURN eReturn:
+----------------------------------------------------------------------+ | e | +----------------------------------------------------------------------+ | (289068899796779009)-[0@Watch:Watch{rate:3.0}]->(289099230855823361) | +----------------------------------------------------------------------+ -
Update the property
rateof the edge to4.0.MATCH(v1@`User`{id:414})-[e@Watch]->(v2@Movie{name:"Star Trek: The Motion Picture"}) SET e.rate=4.0 -
Check the updated edge.
MATCH(v1@`User`{id:414})-[e@Watch]->(v2@Movie{name:"Star Trek: The Motion Picture"}) RETURN eReturn:
+----------------------------------------------------------------------+ | e | +----------------------------------------------------------------------+ | (289068899796779009)-[0@Watch:Watch{rate:4.0}]->(289099230855823361) | +----------------------------------------------------------------------+
Batch update nodes with a table variable¶
'''
TABLE t { id, new_name } =
( 10001, 'Alice_renamed' ),
( 10002, 'Bob_renamed' ),
( 10003, 'Charlie_renamed' )
USE movie
FOR re IN t
MATCH (a@Actor) WHERE a.id = re.id
SET a.name = re.new_name
'''
The preceding statement defines a table variable t and matches nodes of the node type Actor based on the field id in the table variable. It then updates the property name of the matched nodes based on the field new_name in the table variable.
For more information about batch operations by using table variables, see Define table variable.