CREATE GRAPH TYPE statement¶
The CREATE GRAPH TYPE statement creates a graph type in the schema of the current session or a specified schema.
The newly created graph type is owned by the user executing this statement. The owner of the graph type can alter, rename, or drop the graph type after it is created. The ownership of the graph type can be transferred to another user by using ALTER OWNER.
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 type, you must be granted to perform the action CREATE GRAPH TYPE on the corresponding schema. For more information, see Roles and privileges.
Limitations¶
- For each node type, you must declare one and only one primary key, which may consist of one or more properties.
- For each edge type, you may declare one multi-edge key, which may consist of zero or more properties.
Syntax¶
- create_graph_type_statement
- node_type_filler
- primary_key_list
- edge_type_filler
- multiedge_key_list
create_graph_type_statement ::=
'CREATE' 'PROPERTY'? 'GRAPH TYPE' ('IF NOT EXISTS')? (schema_reference '/')? graph_type_name 'AS'? '{'
(
(
(('NODE' | 'VERTEX') 'TYPE'? node_type_name '(' node_type_filler ')')
|
(('EDGE' | 'RELATIONSHIP') 'TYPE'? edge_type_name
(
'(' source_node_type_name ')' '-[' edge_type_filler ']->' '(' destination_node_type_name ')'
|
'(' destination_node_type_name ')' '<-[' edge_type_filler ']-' '(' source_node_type_name ')'
|
'(' source_node_type_name ')' '~[' edge_type_filler ']~' '(' destination_node_type_name ')'
)
)
)
(
','
(
(('NODE' | 'VERTEX') 'TYPE'? node_type_name '(' node_type_filler ')')
|
(('EDGE' | 'RELATIONSHIP') 'TYPE'? edge_type_name
(
'(' source_node_type_name ')' '-[' edge_type_filler ']->' '(' destination_node_type_name ')'
|
'(' destination_node_type_name ')' '<-[' edge_type_filler ']-' '(' source_node_type_name ')'
|
'(' source_node_type_name ')' '~[' edge_type_filler ']~' '(' destination_node_type_name ')'
)
)
)
)*
)?
'}'
node_type_filler ::=
('LABEL' label_name | ( 'LABELS' | ( 'IS' | ':' ) ) label_name ('&' label_name)*)?
( ('{'
(property_name ('::' | 'TYPED')? property_value_type ('NULL' | 'NOT NULL' | 'DEFAULT' value_expression | 'PRIMARY KEY' )*)
(',' (property_name ('::' | 'TYPED')? property_value_type ('NULL' | 'NOT NULL' | 'DEFAULT' value_expression| 'PRIMARY KEY' )*))*
(',' primary_key_list)?
'}')
)
primary_key_list ::=
'PRIMARY KEY' '(' property_name (',' property_name)* ')'
edge_type_filler ::=
( 'LABEL' label_name | ( 'LABELS' | ( 'IS' | ':' ) ) label_name ('&' label_name)*)?
('{'
(property_name ('::' | 'TYPED')? property_value_type ('NULL' | 'NOT NULL' | 'DEFAULT' value_expression | 'MULTIEDGE KEY' )*)
(',' (property_name ('::' | 'TYPED')? property_value_type ('NULL' | 'NOT NULL' | 'DEFAULT' value_expression | 'MULTIEDGE KEY')*))*
(',' multiedge_key_list)?
'}'
| '{' ('MULTIEDGE KEY ( )')? '}')
multiedge_key_list ::=
'MULTIEDGE KEY' '(' (property_name ( ',' property_name)* )? ')'
Parameters¶
IF NOT EXISTS¶
This clause checks if a graph type with the same name already exists. If it does already exist, the graph type is not created and no exception is thrown.
If you do not specify this clause, an exception is thrown if a graph type with the same name already exists.
schema_reference¶
The reference to the path of the schema. It specifies the schema path of the graph type to be created here.
Both absolute and relative schema paths can be used in the statement. When the schema reference is provided, the graph type is created in the specified schema; otherwise, it is created in the schema of the current session. The specified schema must exist prior to creation of graph type. For information about setting and viewing the schema, see SESSION SET and SHOW CURRENT_SCHEMA.
graph_type_name¶
The name of the graph type to be created. graph_type_name must be an identifier and cannot start with a number sign (#). For more information about identifiers, see Rules for naming identifiers.
node_type_name¶
The name of a node type in the graph type to be created. node_type_name must be unique among all node types and edge types of the graph type.
node_type_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.
label_name¶
The name of a label of the node type or edge type in the graph type.
The name of a label must be unique within a node type or edge type if multiple labels are used.
label_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.
property_name¶
The name of a property of a node type or edge type in the graph type. Each node type must have at least one property. Each edge type can have zero or more properties.
The name of a property must be unique within a node type or edge type. However, within a graph type, if multiple properties share a name, these properties must be of the same value type.
property_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.
property_value_type¶
The value type of a property of a node type or edge type in the graph type. For more information about value types, see Data types.
NULL | NOT NULL | DEFAULT value_expression | PRIMARY KEY | MULTIEDGE KEY¶
The constraint of a property of a node type or edge type in the graph type. The details are as follows:
NULL: Default option, which specifies that the value of the property can beNULL. If no value is specified for the property when inserting nodes or edges that have this property, the value of the property isNULL.
NOT NULL: Specifies that the value of the property CANNOT beNULL. IfNOT NULLis specified, you must specify a non-null value for the property when inserting nodes or edges that have this property, or an exception is thrown.
DEFAULT value_expression: Specifies a default value for the property. If no value is specified for the property when inserting nodes or edges that have this property, the default value is used. This constraint can be used in combination withNULLorNOT NULL.value_expressionmust be an expression that does not depend on any variables.
-
PRIMARY KEY: Specifies that the property alone serves as the primary key for the node type.- The value of the property CANNOT be
NULL. - This constraint cannot be used in combination with
NULLorDEFAULT value_expression. - Each node type must have exactly one primary key; if the primary key consists of multiple properties, you must use the form
primary_key_listinstead to specify these properties. -
The value type of the property CANNOT be
LIST,SET,MAP,VECTOR, orGEOGRAPHY.Caution
SETandMAPcannot currently be used in a primary key. A graph type definition may not reject them, but writing nodes with such a primary key is not supported.
The ways to specify the primary key are as follows:
- If the primary key consists of only one property, use either of the following ways:
- By using the form
primary_key_list - By appending the constraint
PRIMARY KEYto its definition
- By using the form
- If the primary key consists of multiple properties, you must use the form
primary_key_listto specify these properties.
Note
The primary key cannot be modified after the node type is created. When adding nodes of this node type, make sure the combination of the values of these properties is unique.
For each node type, a primary key index is automatically created on the properties that form the primary key. To create custom indexes, see Indexes overview.
- The value of the property CANNOT be
-
MULTIEDGE KEY: Specifies that the property alone serves as the multi-edge key for the edge type.- The value of the property CANNOT be
NULL. - This constraint cannot be used in combination with
NULLorDEFAULT value_expression. - Each edge type can have zero or one multi-edge key; if the multi-edge key consists of multiple properties, you must use the form
multiedge_key_listinstead to specify these properties. - The value type of the property CANNOT be
VECTORorGEOGRAPHY.
The ways to specify the multi-edge key are as follows:
- If the multi-edge key consists of only one property, use either of the following ways:
- By using the form
multiedge_key_list - By appending the constraint
MULTIEDGE KEYto its definition
- By using the form
- If the multi-edge key consists of multiple properties, you must use the form
multiedge_key_listto specify these properties. - If the multi-edge key consists of no properties, you must set
multiedge_key_listasMULTIEDGE KEY().
Note
The multi-edge key cannot be modified after the edge type is created. When adding parallel edges of this edge type, make sure the combination of the values of these properties is unique.
- The value of the property CANNOT be
Combinations of constraints:
NULLandDEFAULT value_expression: The property has a default value and can beNULL. If no value is specified for the property when inserting nodes or edges that have this property, the default value is used; if specified asNULL, the property value isNULL.NOT NULLandDEFAULT value_expression: The property has a default value and cannot beNULL. If no value is specified for the property when inserting nodes or edges that have this property, the default value is used.NOT NULLandPRIMARY KEY: The property alone serves as the primary key for the node type, and the value of the property cannot beNULL. Note that ifPRIMARY KEYis specified,NOT NULLcan be omitted.NOT NULLandMULTIEDGE KEY: The property alone serves as the multi-edge key for the edge type, and the value of the property cannot beNULL. Note that ifMULTIEDGE KEYis specified,NOT NULLcan be omitted.
primary_key_list¶
The list of properties of a node type that form the primary key. These properties must meet the following conditions:
- The combination of their values must be unique.
- They cannot have the
DEFAULT value_expressionorNULLconstraint.
multiedge_key_list¶
The list of properties of an edge type that form the multi-edge key, which is a prerequisite for creating parallel edges of this edge type between two nodes. These properties must meet the following conditions:
- The combination of their values must be unique.
- They cannot have the
DEFAULT value_expressionorNULLconstraint.
edge_type_name¶
The name of an edge type in the graph type to be created. edge_type_name must be unique among all node types and edge types of the graph type.
edge_type_name is an identifier. For more information about identifiers, see Rules for naming identifiers.
source_node_type_name¶
The name of the source node type of the edge type. The source node type must be one of the node types defined in the graph type.
source_node_type_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.
destination_node_type_name¶
The name of the destination node type of the edge type. The destination node type must be one of the node types defined in the graph type.
destination_node_type_name must be an identifier. For more information about identifiers, see Rules for naming identifiers.
Examples¶
Create an empty graph type in the current schema¶
CREATE GRAPH TYPE IF NOT EXISTS empty_graph_type AS {}
The preceding example creates an empty graph type empty_graph_type that contains no node types or edge types.
Create a graph type in the 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}) }
'''
The preceding examples create a graph type my_graph_type that contains one node type in the specified schema /default_schema when the schema for the current session is set to /my_schema.
Create a graph type where each node type uses id as the primary key¶
'''
CREATE GRAPH TYPE IF NOT EXISTS movie_type AS {
NODE Actor (LABEL Person {id INT64, name STRING, birthDate DATE, PRIMARY KEY (id)}) ,
NODE Director (LABEL Person {id INT64, name STRING, birthDate DATE, PRIMARY KEY (id)}),
NODE `User` (LABEL Person {id INT64 PRIMARY KEY}),
NODE Movie (LABEL Movie {id INT64 PRIMARY KEY, name STRING}),
NODE Genre (LABEL Genre {id INT64 PRIMARY KEY, name STRING}),
EDGE Act (Actor)-[:Act]->(Movie),
EDGE Direct (Director)-[:Direct]->(Movie),
EDGE Watch (`User`)-[:Watch {rate FLOAT}]->(Movie),
EDGE WithGenre (Movie)-[:WithGenre]->(Genre)
}
'''
The preceding example demonstrates how the graph type movie_type introduced in Example dataset is defined.
Create a graph type where each edge type has a multi-edge key¶
'''
CREATE GRAPH TYPE IF NOT EXISTS social_network AS {
NODE Person (LABEL Person {id INT64 PRIMARY KEY, name STRING}),
NODE Company (LABEL Company {id INT64 PRIMARY KEY, name STRING}),
EDGE WorkAt (Person)-[:WorkAt {`from` DATE, to DATE, MULTIEDGE KEY ()}]->(Company),
EDGE Friend (Person)-[:Friend {since DATE MULTIEDGE KEY }]->(Person)
}
'''
The preceding example creates a graph type social_network consisting of two node types and two edge types. The edge type WorkAt has a multi-edge key that consists of no properties, and the edge type Friend has a multi-edge key that consists of one property since.
Create a graph type where the node type has properties with default values¶
'''
CREATE GRAPH TYPE IF NOT EXISTS social_network_2 AS {
NODE Person (LABEL Person {id INT64 PRIMARY KEY, name STRING DEFAULT 'unknown', age INT32 DEFAULT 0}),
NODE Company (LABEL Company {id INT64 PRIMARY KEY, name STRING})
}
'''
The preceding example creates a graph type social_network_2 consisting of two node types. The node type Person has two properties name and age with default values unknown and 0, respectively.
Create a graph type with a composite primary key and a composite multi-edge key¶
'''
CREATE GRAPH TYPE IF NOT EXISTS primary_key_multi_properties AS {
NODE Person (LABEL Person {id INT64, name STRING, PRIMARY KEY (id, name)}),
EDGE Know (Person)-[:`Like` {`from` DATE, to DATE, MULTIEDGE KEY (`from`,to)}]->(Person)
}
'''
The preceding example creates a graph type primary_key_multi_properties, which includes a node type Person with a composite primary key consisting of two properties id and name, and an edge type Know with a multi-edge key consisting of two properties from and to.
Create a graph type where properties include VECTOR types¶
'''
CREATE GRAPH TYPE IF NOT EXISTS ann_test AS {
NODE N1 (LABEL N1 {id INT64 PRIMARY KEY, vec1 VECTOR<3, FLOAT>, vec2 VECTOR<7, FLOAT>, vec3 VECTOR<3, FLOAT>}),
NODE N2 (LABEL N2 {id INT64 PRIMARY KEY, vec1 VECTOR<3, FLOAT>, vec2 VECTOR<7, FLOAT>, vec5 VECTOR<5, FLOAT>}),
EDGE E1 (N1)-[LABEL E1 {id INT64, vec1 VECTOR<3, FLOAT>, vec2 VECTOR<7, FLOAT>, vec3 VECTOR<3, FLOAT> }]->(N2)
}
'''
The preceding example creates a graph type ann_test consisting of two node types N1 and N2 and an edge type E1. The properties of the node types and edge type all include VECTOR types.