1 00:00:00,000 --> 00:00:07,945 2 00:00:07,945 --> 00:00:09,320 PROFESSOR: In this section, we're 3 00:00:09,320 --> 00:00:13,050 going to be talking about updating data, deleting data, 4 00:00:13,050 --> 00:00:14,960 and we're going to learn a few new commands 5 00:00:14,960 --> 00:00:16,935 and functions within Neo4j. 6 00:00:16,935 --> 00:00:18,560 In this first section, we'll be talking 7 00:00:18,560 --> 00:00:22,580 about updating existing nodes and relationships, 8 00:00:22,580 --> 00:00:26,900 using new keywords to find data, deleting nodes, detaching 9 00:00:26,900 --> 00:00:30,950 relationships, and detaching and deleting nodes. 10 00:00:30,950 --> 00:00:33,050 In this first video, we'll go over 11 00:00:33,050 --> 00:00:36,590 some of the capabilities of the MERGE statement in preparation 12 00:00:36,590 --> 00:00:38,090 of the rest of the video. 13 00:00:38,090 --> 00:00:40,880 And then we'll look at the commands of MERGE, 14 00:00:40,880 --> 00:00:44,490 the conditions that happen when you create a new node, 15 00:00:44,490 --> 00:00:46,730 as well as the conditions that happen when 16 00:00:46,730 --> 00:00:48,800 you match an existing node. 17 00:00:48,800 --> 00:00:52,610 We'll also use MERGE where we could use MATCH 18 00:00:52,610 --> 00:00:56,060 and SET to change attributes. 19 00:00:56,060 --> 00:00:58,700 To review, we have a basic data model 20 00:00:58,700 --> 00:01:01,230 set up with actors and movies. 21 00:01:01,230 --> 00:01:05,610 Our goal was to solve the six degrees of Kevin Bacon problem. 22 00:01:05,610 --> 00:01:10,380 This first MERGE statement will find an actor with the ID1 23 00:01:10,380 --> 00:01:11,940 and will show its information. 24 00:01:11,940 --> 00:01:15,300 In the Neo4j web browser, we can type 25 00:01:15,300 --> 00:01:23,520 merge parentheses lowercase A as in Alias, actor as the label. 26 00:01:23,520 --> 00:01:30,510 We can specify the ID of 1 to filter, and we can return a. 27 00:01:30,510 --> 00:01:32,550 When we execute this statement, we 28 00:01:32,550 --> 00:01:38,010 should receive one node representing Kevin Bacon. 29 00:01:38,010 --> 00:01:42,070 We can look in the rows, the texts to show more raw results. 30 00:01:42,070 --> 00:01:46,230 We can show all the attributes for this Kevin Bacon actor. 31 00:01:46,230 --> 00:01:50,520 We can also use MERGE to create new nodes. 32 00:01:50,520 --> 00:01:54,000 Here we'll create a new actor node with the ID of 99 33 00:01:54,000 --> 00:01:54,960 and we'll return it. 34 00:01:54,960 --> 00:02:01,730 Again, we'll type merge, a, an Alias actor, ID of 99, 35 00:02:01,730 --> 00:02:03,470 and will return a. 36 00:02:03,470 --> 00:02:07,560 We know this data doesn't exist since we only set up-- 37 00:02:07,560 --> 00:02:11,000 we know this data doesn't exist because we only set up actors 1 38 00:02:11,000 --> 00:02:12,410 through 7 or so. 39 00:02:12,410 --> 00:02:15,980 So when we execute this, it will create a new node 40 00:02:15,980 --> 00:02:19,040 with no attributes, oops, because we only 41 00:02:19,040 --> 00:02:23,600 supplied the ID parameter or the ID attribute 42 00:02:23,600 --> 00:02:25,110 in the MERGE statement. 43 00:02:25,110 --> 00:02:28,850 What we should have done to create a new actor 44 00:02:28,850 --> 00:02:32,030 node with more attributes is specify those attributes 45 00:02:32,030 --> 00:02:33,500 within the MERGE statement. 46 00:02:33,500 --> 00:02:36,800 Here we're going to make an actor with the ID of 99 47 00:02:36,800 --> 00:02:40,280 and the name of George George. 48 00:02:40,280 --> 00:02:42,810 When we execute that Cipher statement, 49 00:02:42,810 --> 00:02:48,410 we'll see the new actor created with the ID of 99. 50 00:02:48,410 --> 00:02:53,570 Now the problem is we ran the merge twice with an ID of 99. 51 00:02:53,570 --> 00:02:56,310 So if we run it again without the name, 52 00:02:56,310 --> 00:03:00,390 this should match on all nodes with the ID of 99, 53 00:03:00,390 --> 00:03:06,330 and in this case, we will get our two actor nodes. 54 00:03:06,330 --> 00:03:10,170 The MERGE statement can also act like an insert or update 55 00:03:10,170 --> 00:03:13,740 statement or construct within traditional database systems. 56 00:03:13,740 --> 00:03:16,200 Using the MERGE statement, you can 57 00:03:16,200 --> 00:03:18,540 create a condition called ON CREATE 58 00:03:18,540 --> 00:03:22,230 and you can execute or update specific values. 59 00:03:22,230 --> 00:03:25,140 You can also set the condition for ON MATCH where 60 00:03:25,140 --> 00:03:26,590 you can do something else. 61 00:03:26,590 --> 00:03:31,350 In this case, we'll create a new actor with the ID of 98. 62 00:03:31,350 --> 00:03:34,740 When it's created, we'll set the name to Mark Hamill 63 00:03:34,740 --> 00:03:36,550 and we'll add a counter field. 64 00:03:36,550 --> 00:03:40,320 We haven't done this yet, but we'll add a counter field of 0. 65 00:03:40,320 --> 00:03:46,530 If we rerun this MERGE statement and the condition of ID 99 66 00:03:46,530 --> 00:03:51,240 is already met, we'll execute the statements ON MATCH section 67 00:03:51,240 --> 00:03:54,820 where we'll set the counter to the counter plus 1, 68 00:03:54,820 --> 00:03:57,710 effectively incrementing the counter every time 69 00:03:57,710 --> 00:03:59,330 we merge on this node. 70 00:03:59,330 --> 00:04:00,620 Let's try it out. 71 00:04:00,620 --> 00:04:03,380 You'll see here we can type this MERGE statement, 72 00:04:03,380 --> 00:04:05,300 merge, actor ID 98. 73 00:04:05,300 --> 00:04:11,270 ON CREATE, we'll set the name to Mark Hamill with a counter of 0. 74 00:04:11,270 --> 00:04:16,279 ON MATCH, we'll set the counter to counter plus 1 75 00:04:16,279 --> 00:04:18,089 and we'll return the actor node. 76 00:04:18,089 --> 00:04:21,380 Here we'll execute it and we should see our new node. 77 00:04:21,380 --> 00:04:26,330 If we look at the raw results we'll see a counter of 0. 78 00:04:26,330 --> 00:04:27,300 So using the browser. 79 00:04:27,300 --> 00:04:30,110 We can click on the Cipher statement again. 80 00:04:30,110 --> 00:04:32,960 Press it again, our counter will increment. 81 00:04:32,960 --> 00:04:36,230 And every time we execute this since we're 82 00:04:36,230 --> 00:04:40,610 matching on the ID of 98, our counter increments. 83 00:04:40,610 --> 00:04:43,370 So this shows the two conditions we 84 00:04:43,370 --> 00:04:48,310 have used in the MERGE statement ON CREATE and ON MATCH. 85 00:04:48,310 --> 00:04:57,000