< Return to Video

Learning Neo4j Graphs and Cypher : Update/Delete | packtpub.com

  • 0:00 - 0:08
  • 0:08 - 0:09
    PROFESSOR: In this
    section, we're
  • 0:09 - 0:13
    going to be talking about
    updating data, deleting data,
  • 0:13 - 0:15
    and we're going to
    learn a few new commands
  • 0:15 - 0:17
    and functions within Neo4j.
  • 0:17 - 0:19
    In this first section,
    we'll be talking
  • 0:19 - 0:23
    about updating existing
    nodes and relationships,
  • 0:23 - 0:27
    using new keywords to find
    data, deleting nodes, detaching
  • 0:27 - 0:31
    relationships, and detaching
    and deleting nodes.
  • 0:31 - 0:33
    In this first
    video, we'll go over
  • 0:33 - 0:37
    some of the capabilities of the
    MERGE statement in preparation
  • 0:37 - 0:38
    of the rest of the video.
  • 0:38 - 0:41
    And then we'll look at
    the commands of MERGE,
  • 0:41 - 0:44
    the conditions that happen
    when you create a new node,
  • 0:44 - 0:47
    as well as the conditions
    that happen when
  • 0:47 - 0:49
    you match an existing node.
  • 0:49 - 0:53
    We'll also use MERGE
    where we could use MATCH
  • 0:53 - 0:56
    and SET to change attributes.
  • 0:56 - 0:59
    To review, we have
    a basic data model
  • 0:59 - 1:01
    set up with actors and movies.
  • 1:01 - 1:06
    Our goal was to solve the six
    degrees of Kevin Bacon problem.
  • 1:06 - 1:10
    This first MERGE statement
    will find an actor with the ID1
  • 1:10 - 1:12
    and will show its information.
  • 1:12 - 1:15
    In the Neo4j web
    browser, we can type
  • 1:15 - 1:24
    merge parentheses lowercase A
    as in Alias, actor as the label.
  • 1:24 - 1:31
    We can specify the ID of 1 to
    filter, and we can return a.
  • 1:31 - 1:33
    When we execute
    this statement, we
  • 1:33 - 1:38
    should receive one node
    representing Kevin Bacon.
  • 1:38 - 1:42
    We can look in the rows, the
    texts to show more raw results.
  • 1:42 - 1:46
    We can show all the attributes
    for this Kevin Bacon actor.
  • 1:46 - 1:51
    We can also use MERGE
    to create new nodes.
  • 1:51 - 1:54
    Here we'll create a new
    actor node with the ID of 99
  • 1:54 - 1:55
    and we'll return it.
  • 1:55 - 2:02
    Again, we'll type merge, a,
    an Alias actor, ID of 99,
  • 2:02 - 2:03
    and will return a.
  • 2:03 - 2:08
    We know this data doesn't
    exist since we only set up--
  • 2:08 - 2:11
    we know this data doesn't exist
    because we only set up actors 1
  • 2:11 - 2:12
    through 7 or so.
  • 2:12 - 2:16
    So when we execute this,
    it will create a new node
  • 2:16 - 2:19
    with no attributes,
    oops, because we only
  • 2:19 - 2:24
    supplied the ID parameter
    or the ID attribute
  • 2:24 - 2:25
    in the MERGE statement.
  • 2:25 - 2:29
    What we should have done
    to create a new actor
  • 2:29 - 2:32
    node with more attributes
    is specify those attributes
  • 2:32 - 2:34
    within the MERGE statement.
  • 2:34 - 2:37
    Here we're going to make
    an actor with the ID of 99
  • 2:37 - 2:40
    and the name of George George.
  • 2:40 - 2:43
    When we execute that
    Cipher statement,
  • 2:43 - 2:48
    we'll see the new actor
    created with the ID of 99.
  • 2:48 - 2:54
    Now the problem is we ran the
    merge twice with an ID of 99.
  • 2:54 - 2:56
    So if we run it again
    without the name,
  • 2:56 - 3:00
    this should match on all
    nodes with the ID of 99,
  • 3:00 - 3:06
    and in this case, we will
    get our two actor nodes.
  • 3:06 - 3:10
    The MERGE statement can also
    act like an insert or update
  • 3:10 - 3:14
    statement or construct within
    traditional database systems.
  • 3:14 - 3:16
    Using the MERGE
    statement, you can
  • 3:16 - 3:19
    create a condition
    called ON CREATE
  • 3:19 - 3:22
    and you can execute or
    update specific values.
  • 3:22 - 3:25
    You can also set the
    condition for ON MATCH where
  • 3:25 - 3:27
    you can do something else.
  • 3:27 - 3:31
    In this case, we'll create a
    new actor with the ID of 98.
  • 3:31 - 3:35
    When it's created, we'll
    set the name to Mark Hamill
  • 3:35 - 3:37
    and we'll add a counter field.
  • 3:37 - 3:40
    We haven't done this yet, but
    we'll add a counter field of 0.
  • 3:40 - 3:47
    If we rerun this MERGE statement
    and the condition of ID 99
  • 3:47 - 3:51
    is already met, we'll execute
    the statements ON MATCH section
  • 3:51 - 3:55
    where we'll set the counter
    to the counter plus 1,
  • 3:55 - 3:58
    effectively incrementing
    the counter every time
  • 3:58 - 3:59
    we merge on this node.
  • 3:59 - 4:01
    Let's try it out.
  • 4:01 - 4:03
    You'll see here we can
    type this MERGE statement,
  • 4:03 - 4:05
    merge, actor ID 98.
  • 4:05 - 4:11
    ON CREATE, we'll set the name to
    Mark Hamill with a counter of 0.
  • 4:11 - 4:16
    ON MATCH, we'll set the
    counter to counter plus 1
  • 4:16 - 4:18
    and we'll return the actor node.
  • 4:18 - 4:21
    Here we'll execute it and
    we should see our new node.
  • 4:21 - 4:26
    If we look at the raw results
    we'll see a counter of 0.
  • 4:26 - 4:27
    So using the browser.
  • 4:27 - 4:30
    We can click on the
    Cipher statement again.
  • 4:30 - 4:33
    Press it again, our
    counter will increment.
  • 4:33 - 4:36
    And every time we
    execute this since we're
  • 4:36 - 4:41
    matching on the ID of 98,
    our counter increments.
  • 4:41 - 4:43
    So this shows the
    two conditions we
  • 4:43 - 4:48
    have used in the MERGE statement
    ON CREATE and ON MATCH.
  • 4:48 - 4:57
Title:
Learning Neo4j Graphs and Cypher : Update/Delete | packtpub.com
Description:

more » « less
Video Language:
English
Duration:
04:57

English subtitles

Revisions