OrientDB – Update Record

Update Record command is used to modify the value of a particular record. SET is the basic command to update a particular field value.

The following statement is the basic syntax of the Update command.

UPDATE <class>|cluster:<cluster>|<recordID> 
   [SET|INCREMENT|ADD|REMOVE|PUT <field-name> = <field-value>[,]*] |[CONTENT| MERGE <JSON>] 
   [UPSERT] 
   [RETURN <returning> [<returning-expression>]] 
   [WHERE <conditions>] 
   [LOCK default|record] 
   [LIMIT <max-records>] [TIMEOUT <timeout>] 

Following are the details about the options in the above syntax.

SET βˆ’ Defines the field to update.

INCREMENT βˆ’ Increments the specified field value by the given value.

ADD βˆ’ Adds the new item in the collection fields.

REMOVE βˆ’ Removes an item from the collection field.

PUT βˆ’ Puts an entry into map field.

CONTENT βˆ’ Replaces the record content with JSON document content.

MERGE βˆ’ Merges the record content with a JSON document.

LOCK βˆ’ Specifies how to lock the records between load and update. We have two options to specify Default and Record.

UPSERT βˆ’ Updates a record if it exists or inserts a new record if it doesn’t. It helps in executing a single query in the place of executing two queries.

RETURN βˆ’ Specifies an expression to return instead of the number of records.

LIMIT βˆ’ Defines the maximum number of records to update.

TIMEOUT βˆ’ Defines the time you want to allow the update run before it times out.

Example

Let us consider the same Customer table that we have used in the previous chapter.

Sr.No.NameAge
1Satish25
2Krishna26
3Kiran29
4Javeed21
5Raja29

Try the following query to update the age of a customer β€˜Raja’.

Orientdb {db = demo}> UPDATE Customer SET age = 28 WHERE name = 'Raja'

If the above query is executed successfully, you will get the following output.

Updated 1 record(s) in 0.008000 sec(s).

To check the record of Customer table you can use the following query.

orientdb {db = demo}> SELECT FROM Customer 

If the above query is executed successfully, you will get the following output.

----+-----+--------+----+-------+---- 
#   |@RID |@CLASS  |id  |name   |age  
----+-----+--------+----+-------+---- 
0   |#11:0|Customer|1   |satish |25   
1   |#11:1|Customer|2   |krishna|26   
2   |#11:2|Customer|3   |kiran  |29 
3   |#11:3|Customer|4   |javeed |21 
4   |#11:4|Customer|5   |raja   |28   
----+-----+--------+----+-------+---- 

Leave a Reply