Schema

Schema

Some queries are designed to retrieve information about the structure of the data within the database, for example, node labels and properties associated, as well as relationship types and properties.

Schema-related queries

To use schema-associated queries, start Memgraph with the --storage-enable-schema-metadata configuration flag set to True. The flag facilitates the utilization of a specialized cache designed to store specific metadata related to the database.

Information about node labels

The SHOW NODE_LABELS INFO; query provides a list of node labels that currently exist in the database or have existed at some point.

If the database contains the following data:

CREATE (:Dog {name: "Rex", age: 5})-[l:LOVES {how_much: "very"}]->(:Human:Owner {name: "Carl", age: 90});

The SHOW NODE_LABELS INFO; will return all the labels:

SHOW NODE_LABELS INFO;

Result:

+-------------+
| node labels |
+-------------+
| "Owner"     |
| "Human"     |
| "Dog"       |
+-------------+

Information about relationship types

The SHOW EDGE_TYPES INFO; query provides a list of relationship types that currently exist in the database or have existed at some point.

If the database contains the following data:

CREATE (:Dog {name: "Rex", age: 5})-[l:LOVES {how_much: "very"}]->(:Human:Owner {name: "Carl", age: 90});

The SHOW EDGE_TYPES INFO; will return all the types:

SHOW EDGE_TYPES INFO;

Result:

+------------+
| edge types |
+------------+
| "LOVES"    |
+------------+

Schema-related procedures

node_type_properties()

The schema.node_type_properties() procedure returns schema information about nodes and their properties in the graph.

Output:

  • nodeType: string ➡ Concatenated node labels separated by a :.
  • nodeLabels: List[string] ➡ A list of node labels.
  • mandatory: boolean ➡ Returns True if every node with a given node type (defined by nodeType or nodeLabels) possesses the listed property (propertyName), and False otherwise.
  • propertyName: string ➡ Property name.
  • propertyTypes: string ➡ Property type.

Usage:

To get the information about nodes and properties, run the following query:

CALL schema.node_type_properties()
YIELD nodeType, nodeLabels, mandatory, propertyName, propertyTypes;

rel_type_properties()

This schema.rel_type_properties() procedure returns schema information about relationships and their properties in the graph.

Output:

  • relType: string ➡ The type of the relationship.
  • mandatory: boolean ➡ Returns True if every relationship with a given relationship type (defined by relType) possesses the listed property (propertyName), and False otherwise.
  • propertyName: string ➡ Property name.
  • propertyTypes: string ➡ Property type.

Usage:

To get the information about relationships and properties, run the following query:

CALL schema.rel_type_properties()
YIELD relType, mandatory, propertyName, propertyTypes;

assert()

The schema.assert() is used to ensure indices and constraints exist in Memgraph.

Input:

  • indices_map: (mgp.Map, required): Requires a map of key-value pairs where the key is a string representing a label and the value is a list containing properties as strings. If the list of properties is empty, an index will be created only on the label. If the list is not empty, indexes on the properties belonging to that label will be created. To create indexes on both the label and properties, add an empty string to the list of properties, for example, ["", "prop1", "prop2", ...].
  • unique_constraints: (mgp.Map, required): Requires a map of key-value pairs where the key is a string representing a label and the value is a list of lists containing properties as strings. List of lists is required since unique constraints can be created on a combination of multiple properties.
  • existence_constraints: (mgp.Map, optional): Requires a map of key-value pairs where the key is a string representing a label and the value is a list containing properties as strings. For each property, one existence constraint is created.
  • drop_existing: (bool, optional, default=true): If true, all existing indices and constraints will be dropped. If the user asserts existing indexes or constraints, they won't be dropped and re-created.

Output:

  • action: string - The modification made to an index or a constraint.
  • label: string - Label used for modifying index or constraint.
  • key: string - Properties used for modifying index or constraint. When a label index is modified, an empty string is returned. When a unique constraint is modified, where key ia a list of properties a stringified list is returned.
  • keys: List[string] - List of properties used to modify indexes or constraints.
  • unique: bool - When true the record referres to unique constraint, when false it reffers to existence constraint and indices.

Usage:

Create initial indices and constraints in the database, using the following queries:

CREATE INDEX ON :Entity;
CREATE CONSTRAINT ON (p:Person) ASSERT EXISTS (p.address);
CREATE CONSTRAINT ON (p:Person) ASSERT p.id IS UNIQUE;

The following query will modify indexes and constraints by creating an index on the :Person label, and a label-property index on :Person(id) property. Also, a unique constraints will be created on (Person.name, Person.surname) and (Person.id). The index on the :Entity label will be dropped along with the existence constraint on :Person(id) property.

CALL schema.assert({Person: ["", "id"]}, {Person: [["name", "surname"], ["id"]]}, {}, true) 
YIELD action, key, keys, label, unique
RETURN action, key, keys, label, unique;

Results:

+-----------------------------------------------------------------------------+
| action     | key               | keys                | label       | unique |
+-----------------------------------------------------------------------------+
| "Created"  | ""                | []                  | "Person"    | false  |
+-----------------------------------------------------------------------------+
| "Created"  | "id"              | ["id"]              | "Person"    | false  |
+-----------------------------------------------------------------------------+
| "Dropped"  | ""                | []                  | "Entity"    | false  |
+-----------------------------------------------------------------------------+
| "Dropped"  | "address"         | ["address"]         | "Person"    | false  |
+-----------------------------------------------------------------------------+
| "Created"  | "[name, surname]" | ["name", "surname"] | "Person"    | true   |
+-----------------------------------------------------------------------------+
| "Kept"     | "id"              | ["id"]              | "Person"    | true   |
+-----------------------------------------------------------------------------+

Delete all node indexes

The assert() procedure can be used to delete all indexes and constraints, except for edge-type indices. By providing empty indices_map, unique_constraints and existence_constraints as well as drop_existing set to true, ensure that there are no indexes or constraints in the database. Here is the query for deleting all indexes and constraints:

CALL schema.assert({}, {}, {}, true) 
YIELD action, key, keys, label, unique
RETURN action, key, keys, label, unique;

Example

Create a graph using the following query:

CREATE (:Dog {name: "Rex", age: 5})-[l:LOVES {how_much: "very"}]->(:Human:Owner {name: "Carl", age: 90})
CREATE (:Dog)-[r:RUNS_AND_PLAYS_IN {speed: 100, duration: "5 hours" }]->(:Park)
CREATE (:Bird)-[f:FLIES_TO]->(:Sky);

Call the procedure to get information about the nodes:

CALL schema.node_type_properties()
YIELD nodeType, nodeLabels, mandatory, propertyName, propertyTypes;

Result:

+--------------------+--------------------+--------------------+--------------------+--------------------+
| nodeType           | nodeLabels         | mandatory          | propertyName       | propertyTypes      |
+--------------------+--------------------+--------------------+--------------------+--------------------+
| ":`Sky`"           | ["Sky"]            | false              | ""                 | ""                 |
| ":`Park`"          | ["Park"]           | false              | ""                 | ""                 |
| ":`Human`:`Owner`" | ["Human", "Owner"] | false              | "age"              | "Int"              |
| ":`Human`:`Owner`" | ["Human", "Owner"] | false              | "name"             | "String"           |
| ":`Bird`"          | ["Bird"]           | false              | ""                 | ""                 |
| ":`Dog`"           | ["Dog"]            | false              | "age"              | "Int"              |
| ":`Dog`"           | ["Dog"]            | false              | "name"             | "String"           |
+--------------------+--------------------+--------------------+--------------------+--------------------+

Call the procedure to get information about the relationships:

CALL schema.rel_type_properties()
YIELD relType, mandatory, propertyName, propertyTypes;

Results:

+------------------------+------------------------+------------------------+------------------------+
| relType                | mandatory              | propertyName           | propertyTypes          |
+------------------------+------------------------+------------------------+------------------------+
| ":`FLIES_TO`"          | false                  | ""                     | ""                     |
| ":`RUNS_AND_PLAYS_IN`" | false                  | "duration"             | "String"               |
| ":`RUNS_AND_PLAYS_IN`" | false                  | "speed"                | "Int"                  |
| ":`LOVES`"             | true                   | "how_much"             | "String"               |
+------------------------+------------------------+------------------------+------------------------+