在Neo4J中创建关系

时间:2017-03-12 11:06:33

标签: neo4j cypher

我是Neo4J的新手(以及一般的数据库)。我读过“Learning Neo4J”一书,创建节点没什么问题。我在创建关系方面遇到了很大问节点代表国家(仅两个),也代表城市(八个)。

以下是我用于创建国家/地区节点的CREATE语句:

CREATE(SG: Country {Name:'Singapore', FullName:'Republic of
   Singapore'})
CREATE(MY: Country {Name:'Malaysia',
   FullName:'Malaysia'})

以下是我创建城市的CREATE语句:

CREATE(City1: City {Name:'Singapore'})
CREATE(City2: City {Name:'Kuala Lumpur'})
CREATE(City3: City {Name:'Johor Bahru'})
CREATE(City4: City {Name:'Malacca'})
CREATE(City5: City {Name:'Georgetown'})
CREATE(City6: City {Name:'Alor Star'})
CREATE(City7: City {Name:'Klang'})
CREATE(City8: City {Name:'Shah Alam'})

到目前为止一切顺利。但是,当我发出这些命令来创建关系时,它们不起作用:

CREATE(City1)-[:CITY_IN]->(SG)
CREATE(City2)-[:CITY_IN]->(MY)
CREATE(City3)-[:CITY_IN]->(MY)
CREATE(City4)-[:CITY_IN]->(MY)
CREATE(City5)-[:CITY_IN]->(MY)
CREATE(City6)-[:CITY_IN]->(MY)
CREATE(City7)-[:CITY_IN]->(MY)
CREATE(City8)-[:CITY_IN]->(MY)

我做错了什么?谢谢。

诚恳, CKLee

1 个答案:

答案 0 :(得分:0)

我猜你在浏览器/ shell中分别运行了这三个cypher块?

如果是这样,您就丢失了标识符所代表的内容。如果您将其作为一个事务运行,这将有效 -

CREATE(SG: Country {Name:'Singapore', FullName:'Republic of
   Singapore'})
CREATE(MY: Country {Name:'Malaysia',
   FullName:'Malaysia'})
CREATE(City1: City {Name:'Singapore'})
CREATE(City2: City {Name:'Kuala Lumpur'})
CREATE(City3: City {Name:'Johor Bahru'})
CREATE(City4: City {Name:'Malacca'})
CREATE(City5: City {Name:'Georgetown'})
CREATE(City6: City {Name:'Alor Star'})
CREATE(City7: City {Name:'Klang'})
CREATE(City8: City {Name:'Shah Alam'})
CREATE(City1)-[:CITY_IN]->(SG)
CREATE(City2)-[:CITY_IN]->(MY)
CREATE(City3)-[:CITY_IN]->(MY)
CREATE(City4)-[:CITY_IN]->(MY)
CREATE(City5)-[:CITY_IN]->(MY)
CREATE(City6)-[:CITY_IN]->(MY)
CREATE(City7)-[:CITY_IN]->(MY)
CREATE(City8)-[:CITY_IN]->(MY)