N: Creating Nodes and Relationships
Creating nodes and relationships is the first step in building a graph database. Nodes represent entities in the graph, such as students, rooms, or activities. Relationships connect nodes and represent the connections between them.
Creating Nodes
Nodes are created using the CREATE clause in Cypher. The general syntax for creating a node is:
CREATE (n:NodeLabel {propertyName: propertyValue, ...})
Where:
nis the node variableNodeLabelis the label assigned to the nodepropertyNameis the property namepropertyValueis the value assigned to the property...represents additional properties
Example: Creating a Student Node
CREATE (s:Student {studentID: '123456', studentName: 'Alice'...})
Creating Relationships
Relationships are created using the CREATE clause in Cypher. The general syntax for creating a relationship is:
MATCH (n1:NodeLabel1), (n2:NodeLabel2)
WHERE n1.propertyName = propertyValue1 AND n2.propertyName = propertyValue2
CREATE (n1)-[r:RELATIONSHIP_TYPE]->(n2)
Where:
n1andn2are the node variablesNodeLabel1andNodeLabel2are the labels assigned to the nodespropertyNameis the property namepropertyValueis the value assigned to the propertyris the relationship variableRELATIONSHIP_TYPEis the type of relationship->represents the direction of the relationshipMATCHis used to find the nodes to connect, optionalWHEREis used to filter the nodes, optionalCREATEis used to create the relationship...represents additional properties
Example: Creating a Relationship Between a Student and an Activity
MATCH (s:Student {studentID: '123456'}), (a:Activity {activityID: '789'})
CREATE (s)-[r:ATTENDS]->(a)
Relationships created after ETL
The following relationships were applied to my proof-of-concept graph after loading data using the ETL, using node properties. Delete code also supplied below.
Caution
Use code with caution, especially DELETE code. Run pattern matching and investigate results before committing to delete.
(student)-[REGISTERED_ON]->(programme)
// Create REGISTERED_ON relationship between student and programme nodes
// Match student and programme nodes based on matching properties
MATCH (s:student), (p:programme)
WHERE s.stuProgSplusID = p.posSplusID
// Create REGISTERED_ON relationship
MERGE (s)-[:REGISTERED_ON]->(p)
// Delete REGISTERED_ON relationships
MATCH (:student)-[r:REGISTERED_ON]->(:programme)
DELETE r
(student)-[ENROLLED_ON]->(module)
// Create ENROLLED_ON relationship between students and modules
// Match student, activity, and module nodes based on ATTEND and BELONGS_TO relationships
MATCH (s:student)-[:ATTENDS]->(a:activity)-[:BELONGS_TO]->(m:module)
// Create ENROLLED_ON relationship
MERGE (s)-[:ENROLLED_ON]->(m)
// Delete ENROLLED_ON relationships
MATCH (:student)-[r:ENROLLED_ON]->(:module)
DELETE r
(activity)-[HAS_TYPE]->(activity_type)
// Create HAS_TYPE relationship between activity and activityType nodes
// Match activity and activityType nodes based on matching properties
MATCH (a:activity), (at:activityType)
WHERE a.actTypeName = at.actTypeDescription
// Create HAS_TYPE relationship
MERGE (a)-[:HAS_TYPE]->(at)
// Find activities without an activityType
MATCH (a:activity)
WHERE NOT EXISTS ((a)-[:HAS_TYPE]->(:activityType))
RETURN a
// Delete HAS_TYPE relationships between activity and activityType nodes
MATCH (a:activity)-[r:HAS_TYPE]->(at:activityType)
DELETE r
(module)-[HAS_OWNING_DEPT]->(department)
// Create HAS_OWNING_DEPT relationship between module and department nodes
// Match module and department nodes based on matching properties
MATCH (m:module), (d:department)
WHERE m.modDepartment = d.deptName
// Create HAS_OWNING_DEPT relationship
MERGE (m)-[:HAS_OWNING_DEPT]->(d)
// Delete HAS_OWNING_DEPT relationships between module and department nodes
MATCH (m:module)-[r:HAS_OWNING_DEPT]->(d:department)
DELETE r
(programme)-[HAS_OWNING_DEPT]->(department)
// Create HAS_OWNING_DEPT relationship between programme and department nodes
// Match programme and department nodes based on matching properties
MATCH (p:programme), (d:department)
WHERE p.posDepartment = d.deptName
// Create HAS_OWNING_DEPT relationship
MERGE (p)-[:HAS_OWNING_DEPT]->(d)
// Delete HAS_OWNING_DEPT relationships between programme and department nodes
MATCH (p:programme)-[r:HAS_OWNING_DEPT]->(d:department)
DELETE r