Skip to content
Developer Edition runs on one host and does not require a product license file. Need multi-host deployment or performance testing at scale? Explore Enterprise Edition

CREATE GRAPH statement

The CREATE GRAPH statement creates a persistent graph (also known as a graph) in the schema of the current session or a specified schema.

The newly created graph is owned by the user executing this statement. The owner of the graph can rename or drop the graph after it is created. The ownership of the graph can be transferred to another user by using ALTER OWNER.

Apart from graphs, you can also create temporary graphs. For a thorough comparison between graphs and temporary graphs, see Graph vs Temporary Graph.

Note

An operation involving a non-default schema or an object in it requires access to the schema, either implicitly or through an explicit USAGE grant, as well as the privilege required for the operation. The schema access check and the privilege check for the requested operation are independent; satisfying one does not satisfy the other. Schema access control does not apply to temporary graphs or built-in procedures and functions. For more information, see Schema access control.

Prerequisites

  • To create a graph, you must be granted to perform the action CREATE GRAPH on the corresponding schema. For more information, see Roles and privileges.

Syntax

create_graph_statement
CREATE PROPERTY GRAPH IF NOT EXISTS schema_reference / graph_name :: TYPED schema_reference / graph_type_name

create_graph_statement ::=
    'CREATE' 'PROPERTY'? 'GRAPH' ('IF NOT EXISTS')? (schema_reference '/')? graph_name ('::' | 'TYPED' )? (schema_reference '/')? graph_type_name

Parameters

IF NOT EXISTS

This clause checks if a graph with the same name already exists. If it does already exist, the graph is not created and no exception is thrown.

If you do not specify this clause, an exception is thrown if a graph with the same name already exists.

schema_reference

The reference to the path of the schema.

This parameter can be used to specify the schema path of the graph to be created and the schema path of the graph type based on which the graph is created respectively. These two schema paths must be consistent. These two schema paths must be consistent.

Both absolute and relative schema paths can be used in the statement. When the schema path is provided, the graph is created in the specified schema; otherwise, it is created in the schema of the current session. The specified graph type must exist prior to creation of graph. For information about setting and viewing the schema, see SESSION SET and SHOW CURRENT_SCHEMA.

graph_name

The name of the graph to be created. graph_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.

graph_type_name

The name of the graph type based on which the graph is created. For more information, see CREATE GRAPH TYPE.

The graph elements (nodes and edges) in the new graph are created based on the graph element types (node types and edge types) defined in the referenced graph type.

Even if the graph elements in different graphs have the same identifiers, they are NOT regarded as the same entity.

Examples

Create a graph in the current schema

CREATE GRAPH IF NOT EXISTS movie TYPED movie_type

The preceding example demonstrates how the graph movie introduced in Example dataset is created based on the graph type movie_type.

Create a graph in a specified schema

'''
// Create a schema
CREATE SCHEMA IF NOT EXISTS /my_schema
'''

'''
// Set the schema for the current session
SESSION SET SCHEMA /my_schema
'''

'''
// Create a graph type in the specified schema
CREATE GRAPH TYPE IF NOT EXISTS /default_schema/my_graph_type AS { NODE Person (LABEL Person {id INT64 PRIMARY KEY, name STRING}) }
'''

'''
// Create a graph
CREATE GRAPH IF NOT EXISTS /default_schema/my_graph TYPED /default_schema/my_graph_type
'''

The preceding example demonstrates how the graph my_graph is created based on the graph type my_graph_type in the /default_schema when the schema in the current session is set to /my_schema.

More information