<jupyter_start><jupyter_text>Neo4j command note **Outline*** [Introduction](intro)* [Cypher Note](cypher)* [Reference](refer) ---  Introduction A graph database is an online database management system with Create, Read, Update and Delete (CRUD) operations working on a graph data model. **Why do we need Graph database**:The relationships between data points matter more than the individual points themselves. Generate deep insights over the data.**Graph Databases are composed of two elements: *** Nodes* Relationships (a.k.a. links/edges)**Note*** Both nodes and links can have attributes.  Cypher In all the following example, any thing start with $ is a variable, which I can change it into something I want.Here is a screenshot after running the following creating related commands  > **show all nodes**<jupyter_code>MATCH (n) RETURN (n)<jupyter_output><empty_output><jupyter_text>> **create node**<jupyter_code># format
create ($variable: $NodeName{name: $value} )
# example
create (masud:User{name:"masud"}) 
create (goce:User{name:"goce"}) 
create (diego:User{name:"diego"}) 
create (sarah:User{name:"sarah"})
create (gamestream:Channel{name:"Masud’s online gaming"}) 
create (lab430:Channel{name:"Masud’s 430 lab"})
create (bigdataclass:Channel{name:"Diego’s big data class"}) 
create (sarahCalendar:Channel{name:"Sarah’s calendar"}) 
create (diegoCalendar:Channel{name:"Diego’s calendar"})<jupyter_output><empty_output><jupyter_text>> **delete node**<jupyter_code>### format
MATCH ($variable: User { name: 'johnny' })
DELETE $variable
### example
# create the node to be deleted
create (jchiu:User{name:"johnny"})

# delete a specific node using match
MATCH (n:User { name: 'johnny' })
DELETE n
# delete everything
MATCH (n) DETACH DELETE n<jupyter_output><empty_output><jupyter_text>> **Create links/relationships**<jupyter_code>### format version1, Preferable
match ($variable1: $node_name1 {name:"$target_name1"}), ($variable2: $node_name2 {name:"$target_name2"})
CREATE ($variable1)-[:$link_name]->($variable2)
### example create in separate command
MATCH (u:User {name:"masud"}), (c:Channel {name:"Masud’s online gaming"}) 
CREATE (u)-[:OWNS]->(c)

MATCH (u:User {name:"masud"}), (c:Channel {name:"Masud’s 430 lab"})
CREATE (u)-[:OWNS]->(c)

MATCH (u:User {name:"diego"}), (c:Channel {name:"Diego’s big data class"}) 
CREATE (u)-[:OWNS]->(c)

MATCH (u:User {name:"sarah"}), (c:Channel {name:"Sarah’s calendar"}) 
CREATE (u)-[:OWNS]->(c)

MATCH (u:User {name:"diego"}), (c:Channel {name:"Diego’s calendar"}) 
CREATE (u)-[:OWNS]->(c)
### format version2
# noted that both $variable1 and $variable2 in this case are defined previously
# this only works creating for altogheter, i.e., creating node and link in a single query
create ($variable1)-[:$link_name]->($variable2)
### example
(masud)-[:OWNS]->(gamestream), (masud)-[:OWNS]->(lab430), (diego)-[:OWNS]->(bigdataclass), (sarah)-[:OWNS]->(sarahCalendar), (diego)-[:OWNS]->(diegoCalendar)

# or 
(masud)-[:OWNS]->(gamestream)
(masud)-[:OWNS]->(lab430)
(diego)-[:OWNS]->(bigdataclass)
(sarah)-[:OWNS]->(sarahCalendar)
(diego)-[:OWNS]->(diegoCalendar)<jupyter_output><empty_output><jupyter_text>> **match**<jupyter_code># Find anything named “goce”
match (n {name:"goce"}) return n

# Find users named “goce”
match (n:User {name:"goce"}) return n

# Find all users
match (user:User) return user limit 3

# Find users, return the names of them limiting output to be 3
match (user:User) return user.name limit 3

# List all class owned by masud
MATCH (masud:User {name:"masud"})-[:OWNS]->(masudChannels) 
return masud,masudChannels

# Who owned “Diego’s bigdataclass”?
# * the output is a table
MATCH (c:Channel {name: "Diego’s big data class"})<-[:OWNS]-(owner)
return owner.name

# Masud’sco-subscribers
# * the output is a table
match (masud:User {name:"masud"})-[:SUB_TO]->(c)<-[:SUB_TO]-(coSubs)
return coSubs.name
# Lets recommend new channels for masud to sub_to.
# Extend masud’s co-subs, to find co-sub-channels that masud has not sub_to.

# m collect all the channels that masud has subscribe to
# coSubs collect all the Users that has subsrcibed to the channels that subcribed by masud
# m2 records all the channels that these coSubs has subscribed to
# RETURN the name of the courses that all the coSubs has subscribed to
# WHERE filter out the channels that masud has not yet subscribed to
# * the output is a table

match (masud:User {name:"masud"})-[:SUB_TO]->(m)<-[:SUB_TO]- (coSubs)-[:SUB_TO]->(m2) 
WHERE NOT (masud)-[:SUB_TO]->(m2) 
RETURN m2.name AS Recommended, count(*) AS Strength ORDER BY Strength DESC<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15937635339974016, 'fraction_numerical': 0.00801212646167172, 'mean_word_length': 6.414125200642054, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '21951', 'license': 'MIT', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1539, 'n_words': 478, 'path': 'others/neo4j_command_note.ipynb', 'repo_name': 'johnnychiuchiu/Machine-Learning', 'size': '4618'}