Source: https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph-projection/create/ Documentation version: v5.3.2 # CREATE TEMPORARY GRAPH The `CREATE TEMPORARY GRAPH` statement creates a temporary graph. Temporary graphs reside in the memory of the Graph service that creates them. They are lost when that service exits and can be accessed only through sessions connected to that service. After a temporary graph is created, if the graph type or graph on which it is based is altered or dropped, the temporary graph remains unaffected. ## Scope of use - After a temporary graph is created, you can perform `INSERT`, `SET`, and `DELETE` operations on it in subsequent queries. - You can create a temporary graph in the following ways: - Based on an existing graph - Based on a subgraph - As an empty graph based on an existing named graph type - As an empty graph based on an anonymous graph type defined in the statement Note To check or compact the memory usage of temporary graphs in NebulaGraph Database Developer Edition, you can [call](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/dql/call/call-procedure/) the built-in procedure `temporary_graph_compact`. For more information, run `SHOW PROCEDURE temporary_graph_compact` to view the procedure details. ## Prerequisites - When creating a temporary graph based on an existing graph, a subgraph, or a named graph type, first create the referenced graph or graph type. For more information, see [CREATE GRAPH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph/create/) and [CREATE GRAPH TYPE](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph-type/create/). When using an anonymous graph type, you do not need to create a graph or graph type beforehand. - To create a temporary graph based on an existing graph or a subgraph, you must be granted to perform the action `MATCH ANY GRAPH` on the corresponding schema or the action `MATCH` on the graph being referenced. For more information, see [Roles and privileges](https://dev-docs.nebula-graph.io/v5.3.2/server-admin/authorization/). - To create a temporary graph based on an anonymous graph type, you must be granted to perform the action `CREATE GRAPH TYPE` on the default schema (`/default_schema`). ## Permission rules - The newly created temporary graph is owned by the user executing this statement. The owner of the temporary graph cannot be altered. - Only the user with the role `ADMIN` and the owner can perform DQL and DML operations on the temporary graph. ## Syntax diagramsource create_temporary_graph_statement ```` create_temporary_graph_statement ::= 'CREATE' ('TEMPORARY'|'TEMP') 'GRAPH' ('IF NOT EXISTS')? temp_graph_name ( ('AS COPY OF' ( (schema_reference '/')? graph_name | 'GRAPH' '{' statement_block '}' ) ) | ('TYPED'? (schema_reference '/')? graph_type_name ) | ('TYPED'? '{' graph_type_specification '}') ) ```` ## Parameters ### `IF NOT EXISTS` This clause checks if a temporary graph with the same name already exists. If it does already exist, the temporary graph is not created and no exception is thrown. If you do not specify this clause, an exception is thrown if a temporary graph with the same name already exists. ### `temp_graph_name` The name of the temporary graph to be created. `temp_graph_name` must be a regular identifier that starts with a number sign (`#`). For more information about identifiers, see [Rules for naming identifiers](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/keywords-identifiers/#rules_for_naming_identifiers). ### `schema_reference` The path of the referenced schema. In this statement, the two occurrences of `schema_reference` specify the schema path of the referenced graph and the schema path of the referenced graph type, respectively. Both absolute and relative schema paths can be used in the statement. When the schema path is provided, the temporary graph is created based on the referenced graph or the referenced graph type. For information about setting and viewing the schema, see [SESSION SET](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/sessions/set/) and [SHOW CURRENT_SCHEMA](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/data-admin/show/show-current-schema/). ### `graph_name` The name of the source graph on which the temporary graph is based. This graph must already exist. When creating a temporary graph based on a graph, the graph type of the graph must contain at least one node type. When a temporary graph is created from an existing graph, the source graph data is copied into the temporary graph. Later changes to the temporary graph do not affect the source graph. ### `statement_block` A block of one or more statements used to retrieve the subgraph on which the temporary graph is based. The subgraph may be empty, meaning it contains no graph elements, but it must not contain dangling edges. For more information, see [Statement block](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/statement-block/). ### `graph_type_name` The name of the graph type on which the temporary graph is based. The graph type must already exist in the catalog. Temporary graphs created from graph types are empty graphs. You can populate them later by using `INSERT`, `IMPORT`, or similar statements. ### `graph_type_specification` Define node types, edge types, and properties directly inside `{...}` instead of referencing an existing named graph type. This definition is called an anonymous graph type. You do not need to run `CREATE GRAPH TYPE` first, and the `TYPED` keyword is optional. The rules for defining node types, edge types, and properties are the same as those in [CREATE GRAPH TYPE](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph-type/create/). The temporary graph is initially empty. You can populate it later with `INSERT` or `IMPORT`. ## Usage notes - After a temporary graph is created, you can declare it as the working graph with the `USE` clause and then run queries or DML operations on it. Caution Temporary graphs have additional restrictions on property types. If the referenced graph or graph type contains any of the following property types, creating the temporary graph fails: - `SET` - `MAP` - `GEOGRAPHY` - `LIST` ## Examples The examples based on existing graphs or named graph types use the [example dataset](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/example-dataset/). The anonymous graph type example does not require this dataset. ### Create temporary graphs based on existing graphs #### Create a temporary graph based on a graph in the current schema ```` CREATE TEMPORARY GRAPH IF NOT EXISTS #movie_temporary_graph AS COPY OF movie ```` The preceding example creates a temporary graph named `#movie_temporary_graph` based on the graph `movie`. Based on this temporary graph, you can perform the following operations: - Declare the temporary graph as the working graph and query the number of edges in the graph. ```` ''' USE #movie_temporary_graph MATCH ()-[]->() RETURN count(*) AS num GROUP BY () ''' ```` Return: ```` +-------+ | num | +-------+ | 10175 | +-------+ ```` - Insert a node into the temporary graph `#movie_temporary_graph`. ```` ''' USE #movie_temporary_graph INSERT (v@Director{id: 11, name: "Alice"}) ''' ```` - Update a node in the temporary graph `#movie_temporary_graph`. ```` ''' USE #movie_temporary_graph MATCH (v@Director{id: 11}) SET v.name = "Bob" ''' ```` - Delete a node from the temporary graph `#movie_temporary_graph`. ```` ''' USE #movie_temporary_graph MATCH (v@Director{id: 11}) DELETE v ''' ```` #### Create a temporary graph in standalone mode based on a graph in the specified schema ```` CREATE TEMPORARY GRAPH IF NOT EXISTS #my_temporary_graph AS COPY OF /default_schema/my_graph ```` The preceding example creates a temporary graph named `#my_temporary_graph` based on the graph `my_graph` created in the Examples section of [CREATE GRAPH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph/create/#create_a_graph_in_a_specified_schema). ### Create a temporary graph based on a subgraph ```` CREATE TEMPORARY GRAPH IF NOT EXISTS #sub_movie_temp AS COPY OF GRAPH { USE movie MATCH (v) RETURN v } ```` The preceding example creates a temporary graph based on the subgraph returned by the `MATCH` statement. ### Create an empty temporary graph based on a graph type ```` ''' CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie TYPED movie_type ''' ```` ### Create an empty temporary graph based on an anonymous graph type Run the following statement in the Graph service. You do not need to create a graph type beforehand: ```` ''' CREATE TEMPORARY GRAPH #person_temp_graph { NODE person (:Person {id INT64 PRIMARY KEY, name STRING}) } ''' ```` The example creates an empty temporary graph named `#person_temp_graph` with a node type named `person`, the label `Person`, the primary key property `id`, and the string property `name`. You can also add the `TYPED` keyword before `{` with the same result. After creation, run `SHOW CREATE GRAPH #person_temp_graph` to view the graph structure, or declare the working graph with `USE #person_temp_graph` and insert data. []() ### Create temporary graphs based on a graph or graph type in the specified schema The following example assumes that `/my_schema` already exists and that the [example dataset](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/example-dataset/) has been imported into it. ```` ''' // Based on a graph in the specified schema CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie AS COPY OF /my_schema/movie ''' ''' // Based on a graph type in the specified schema CREATE TEMPORARY GRAPH IF NOT EXISTS #temp_movie TYPED /my_schema/movie_type ''' ```` ## More information - [SHOW GRAPHS](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/data-admin/show/show-graphs/) - [SHOW GRAPHS](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/data-admin/show/show-graphs/) - [SHOW CREATE GRAPH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/data-admin/show/show-create-graph/) - [CREATE SCHEMA](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/schema/create/) - [IMPORT](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/in-out/import/)