Knowledge Base

How do I perform the equivalent of a SQL Create Table as Select with Cypher

With a traditional SQL RDBMS one could perform a create table as select (i.e. CTAS) whereby its purpose is to create a new table and copy existing data from the original table to the new copy. If you are looking to achieve the same with Cypher this can be performed via

MATCH (n:Movie)
MERGE (m:New_Movie)
SET m=n;

The above will copy all Movie nodes and their properties to a set of new nodes with the label New_Movie. If you have a significant number of Movie nodes you may not want to copy all nodes in a single transaction, for example you may want to run utilize apoc.periodic.iterate

call apoc.periodic.iterate("MATCH (n:Movie) RETURN properties(n) as props", "CREATE (m:New_Movie) SET m = props",{});

As with a CTAS statement, this does not copy any underlying schema indexes or constraints on said label. Additionally, if the Movie nodes have additional labels they will not be copied to the New_Movie nodes.