---
# System prepended metadata

title: Neo4j語法

---

# Neo4j語法
## Match
* 取得全部
```
MATCH (n) RETURN n
```
## datebase
* 新增 database
```
create database [name]
```
* 切換 database
```
:use [database name]
```
## Create
* 新增使用者
```
CREATE user YOUR_USERNAME SET password 'YOUR_PASSWORD' CHANGE NOT REQUIRED
```
* 新增節點
```
CREATE (a:Person {name: 'Alice'})
```
* 兩個名字的節點中加關係
```
MATCH (a:Structure {name: 'Speaker'}), (b:Function {name: 'Speaker'})
create (a)-[:has_feature]->(b)
```
* 兩個id的節點加關係
```
MATCH (a) 
WHERE id(a) = 123 
MATCH (b) 
WHERE id(b) = 456 
CREATE (a)-[:has_feature]->(b)
```
## Delete
* 刪除特定label的節點
```
MATCH (n:YourLabel)
DETACH DELETE n;
```
* 刪除特定label的關係
```
MATCH ()-[r:YOUR_RELATIONSHIP_TYPE]->()
DELETE r;
```
* 刪除特定id的節點
```
MATCH (n)
WHERE id(n) = 123
DETACH DELETE n;
```
* 刪除特定id的關係
```
MATCH ()-[r]->()
WHERE id(r) = 456
DELETE r;
```
* 刪除特定ids的關係
```
MATCH ()-[r]->()
WHERE id(r) = 1 OR id(r) = 2 OR id(r) = 3
DELETE r;
```
或是
```
MATCH ()-[r]->()
WHERE id(r) IN [1, 2, 3]
DELETE r;
```

## 更新
* 更新attr by id
```
MATCH (n) 
WHERE id(n) = 123 
SET n.name = "New Name"
RETURN n
```