Graph Type Schema Enforcement
Define and enforce a Neo4j graph schema with graph types: typed nodes and relationships, KEY and UNIQUE constraints, and incremental evolution.
In this 40-minute lab, you will learn
This lab teaches you to define and enforce a database-level schema in Neo4j using graph types, a Full definition for Cypher (opens in a new tab)Neo4j's implementation of GQL, the ISO standard query language for graph databases. It is declarative: you describe the pattern to find, and the database decides how to find it. 25 feature that validates your data at write time. In around 40 minutes you will build a complete, enforceable schema for a movie recommendations Full definition for data model (opens in a new tab)The labels, relationship types and properties chosen to represent a domain., from your first Full definition for node (opens in a new tab)A vertex in a graph. In a property graph it can carry labels and properties. element type to a full graph type covering nodes, Full definition for relationship (opens in a new tab)A named, directed connection between two nodes. Every relationship has a type, a start node and an end node., and Full definition for constraint (opens in a new tab)A rule the database enforces on every node or relationship with a given label or type, rejecting any write that breaks it..
Graphs are schema-optional by default: any node can carry any Full definition for label (opens in a new tab)A tag on a node that groups it with other nodes of the same kind. A node can carry more than one., any Full definition for property (opens in a new tab)A named value stored on a node or a relationship., and any type. That flexibility is a strength while you explore, but in production it lets problems creep in silently. A Movie without a title breaks your front end. A release year stored as a string breaks your date filters. Graph types let you declare what your data should look like, and Neo4j rejects any write that breaks the rules, so you catch bad data at the moment it is created rather than after the damage is done.
Node and relationship types
Define node element types and relationship element types with typed properties to enforce your data model.
Constraints and validation
Add `KEY` and `UNIQUE` constraints and typed, mandatory properties within graph types for data quality enforcement.
Managing graph types
Extend, alter, inspect, and drop graph type elements to evolve your schema over time.
Who this course is for
This lab is for developers, data engineers, and database administrators who run Neo4j in production, or are preparing to. If you have ever traced a bug back to a missing Full definition for property (opens in a new tab)A named value stored on a node or a relationship., a mistyped value, or a Full definition for relationship (opens in a new tab)A named, directed connection between two nodes. Every relationship has a type, a start node and an end node. connecting the wrong Full definition for node (opens in a new tab)A vertex in a graph. In a property graph it can carry labels and properties., this lab shows you how to make the database itself prevent that entire class of error. Graph types use an open model — they constrain only the elements you define and leave everything else flexible — so schema enforcement is practical to adopt incrementally. You should be comfortable writing Full definition for Cypher (opens in a new tab)Neo4j's implementation of GQL, the ISO standard query language for graph databases. It is declarative: you describe the pattern to find, and the database decides how to find it. from Cypher Fundamentals and understand how Full definition for label (opens in a new tab)A tag on a node that groups it with other nodes of the same kind. A node can carry more than one., relationship types, and properties are chosen, as taught in Graph Data Modeling Fundamentals.
What you'll do
This is a hands-on lab, not a reading exercise. Graph types were introduced in Full definition for Cypher (opens in a new tab)Neo4j's implementation of GQL, the ISO standard query language for graph databases. It is declarative: you describe the pattern to find, and the database decides how to find it. 25 and are available in Neo4j Enterprise Edition; the lab provides an embedded sandbox already configured to use Cypher 25 as its default language, so you can focus on the schema rather than the setup.
You run every command yourself as you build up a schema for a recommendations Full definition for data model (opens in a new tab)The labels, relationship types and properties chosen to represent a domain. of movies, people, users, and genres. You declare Full definition for node (opens in a new tab)A vertex in a graph. In a property graph it can carry labels and properties. element types with
ALTER CURRENT GRAPH TYPE SET, giving Full definition for label (opens in a new tab)A tag on a node that groups it with other nodes of the same kind. A node can carry more than one. typed Full definition for property (opens in a new tab)A named value stored on a node or a relationship. such astitle :: STRING NOT NULL, then layer onIS KEYandIS UNIQUEFull definition for constraint (opens in a new tab)A rule the database enforces on every node or relationship with a given label or type, rejecting any write that breaks it. and namedCREATE CONSTRAINTstatements. You extend the schema with Full definition for relationship (opens in a new tab)A named, directed connection between two nodes. Every relationship has a type, a start node and an end node. element types usingADD, inspect the result withSHOW CURRENT GRAPH TYPE, and modify it withALTERandDROPas the model evolves. The closing challenge asks you to write a complete schema from scratch, verified against the database.Where to go next
A schema is most valuable at the point where data enters the graph. Importing CSV data into Neo4j teaches you to load data with
LOAD CSV, casting values to the correct types as they arrive — exactly the discipline your graph type now enforces. Schema design and Full definition for constraint (opens in a new tab)A rule the database enforces on every node or relationship with a given label or type, rejecting any write that breaks it. also feature in the Neo4j Certified Professional exam, so the skills from this lab count toward a recognised credential.
Posts from the GraphAcademy blog.
My five favorite cards in Aura Dashboards
When Dashboards first became available in Aura, I started to move my reporting away from custom tools. Here are the five cards I use the most, and a Cypher statement behind them.
SQL to Cypher - 10 Queries You Already Know
You already know how to write these queries. This article shows you what they look like when the joins go away.
Writing Cypher Queries That Don't Lose Your Data
The query trap that hides your newest data, and the query shape that brings it back.