UPDATE

UPDATE – update objects in a database

[ WITH with-item [, ...] ]

UPDATE selector-expr

[ FILTER filter-expr ]

SET shape ;

UPDATE changes the values of the specified links in all objects selected by update-selector-expr and, optionally, filtered by filter-expr.

WITH

Alias declarations.

The WITH clause allows specifying module aliases as well as expression aliases that can be referenced by the UPDATE statement. See WITH block for more information.

UPDATE selector-expr

An arbitrary expression returning a set of objects to be updated.

FILTER filter-expr

An expression of type bool used to filter the set of updated objects.

filter-expr is an expression that has a result of type bool. Only objects that satisfy the filter expression will be updated. See the description of the FILTER clause of the SELECT statement for more information.

SET shape

A shape expression with the new values for the links of the updated object. There are three possible assignment operations permitted within the SET shape:

SET { field := update-expr [, ...] }

SET { field += update-expr [, ...] }

SET { field -= update-expr [, ...] }

The most basic assignment is the :=, which just sets the field to the specified update-expr. The += and -= either add or remove the set of values specified by the update-expr from the current value of the field.

On successful completion, an UPDATE statement returns the set of updated objects.

Here are a couple of examples of the UPDATE statement with simple assignments using :=:

Copy
# update the user with the name 'Alice Smith'
WITH MODULE example
UPDATE User
FILTER .name = 'Alice Smith'
SET {
    name := 'Alice J. Smith'
};

# update all users whose name is 'Bob'
WITH MODULE example
UPDATE User
FILTER .name LIKE 'Bob%'
SET {
    name := User.name ++ '*'
};

For usage of += and -= consider the following Post type:

Copy
# ... Assume some User type is already defined
type Post {
    required property title -> str;
    required property body -> str;
    # A "tags" property containing a set of strings
    multi property tags -> str;
    link author -> User;
}

The following queries add or remove tags from some user’s posts:

Copy
WITH MODULE example
UPDATE Post
FILTER .author.name = 'Alice Smith'
SET {
    # add tags
    tags += {'example', 'edgeql'}
};

WITH MODULE example
UPDATE Post
FILTER .author.name = 'Alice Smith'
SET {
    # remove a tag, if it exist
    tags -= 'todo'
};

The statement FOR <x> IN <expr> allows to express certain bulk updates more clearly. See Usage of FOR statement for more details.

Light
Dark
System