PROFESSOR: In this section, we're
going to be talking about updating data, deleting data,
and we're going to learn a few new commands
and functions within Neo4j.
In this first section, we'll be talking
about updating existing nodes and relationships,
using new keywords to find data, deleting nodes, detaching
relationships, and detaching and deleting nodes.
In this first video, we'll go over
some of the capabilities of the MERGE statement in preparation
of the rest of the video.
And then we'll look at the commands of MERGE,
the conditions that happen when you create a new node,
as well as the conditions that happen when
you match an existing node.
We'll also use MERGE where we could use MATCH
and SET to change attributes.
To review, we have a basic data model
set up with actors and movies.
Our goal was to solve the six degrees of Kevin Bacon problem.
This first MERGE statement will find an actor with the ID1
and will show its information.
In the Neo4j web browser, we can type
merge parentheses lowercase A as in Alias, actor as the label.
We can specify the ID of 1 to filter, and we can return a.
When we execute this statement, we
should receive one node representing Kevin Bacon.
We can look in the rows, the texts to show more raw results.
We can show all the attributes for this Kevin Bacon actor.
We can also use MERGE to create new nodes.
Here we'll create a new actor node with the ID of 99
and we'll return it.
Again, we'll type merge, a, an Alias actor, ID of 99,
and will return a.
We know this data doesn't exist since we only set up--
we know this data doesn't exist because we only set up actors 1
through 7 or so.
So when we execute this, it will create a new node
with no attributes, oops, because we only
supplied the ID parameter or the ID attribute
in the MERGE statement.
What we should have done to create a new actor
node with more attributes is specify those attributes
within the MERGE statement.
Here we're going to make an actor with the ID of 99
and the name of George George.
When we execute that Cipher statement,
we'll see the new actor created with the ID of 99.
Now the problem is we ran the merge twice with an ID of 99.
So if we run it again without the name,
this should match on all nodes with the ID of 99,
and in this case, we will get our two actor nodes.
The MERGE statement can also act like an insert or update
statement or construct within traditional database systems.
Using the MERGE statement, you can
create a condition called ON CREATE
and you can execute or update specific values.
You can also set the condition for ON MATCH where
you can do something else.
In this case, we'll create a new actor with the ID of 98.
When it's created, we'll set the name to Mark Hamill
and we'll add a counter field.
We haven't done this yet, but we'll add a counter field of 0.
If we rerun this MERGE statement and the condition of ID 99
is already met, we'll execute the statements ON MATCH section
where we'll set the counter to the counter plus 1,
effectively incrementing the counter every time
we merge on this node.
Let's try it out.
You'll see here we can type this MERGE statement,
merge, actor ID 98.
ON CREATE, we'll set the name to Mark Hamill with a counter of 0.
ON MATCH, we'll set the counter to counter plus 1
and we'll return the actor node.
Here we'll execute it and we should see our new node.
If we look at the raw results we'll see a counter of 0.
So using the browser.
We can click on the Cipher statement again.
Press it again, our counter will increment.
And every time we execute this since we're
matching on the ID of 98, our counter increments.
So this shows the two conditions we
have used in the MERGE statement ON CREATE and ON MATCH.