Creating a Property Graph Using PGQL

CREATE PROPERTY GRAPH is a PGQL DDL statement to create a PGQL property graph from the database tables.

The CREATE PROPERTY GRAPH statement starts with the name you give the graph, followed by a set of vertex tables and edge tables. The graph can have no vertex tables or edge tables (an empty graph), or vertex tables and no edge tables (a graph with only vertices and no edges), or both vertex tables and edge tables (a graph with vertices and edges). However, a graph cannot be specified with only edge tables and no vertex tables.

Optionally, you can also create a PGQL property graph from existing graphs. See Creating a PGQL Property Graph with the BASE_GRAPHS Clause for more information.

Note:

The following best practices are recommended when creating a PGQL property graph:

pgqlStmt.execute("CALL pg.validate('<graph_name>')")

For example, consider the bank_accounts and bank_transfers database tables created using the sample graph data in opt/oracle/graph/data directory. See Using Sample Graph Data for more information.

You can create a PGQL property graph using the database tables as shown:

CREATE PROPERTY GRAPH bank_graph
     VERTEX TABLES(
       bank_accounts AS accounts
         KEY(id)
         LABEL accounts
         PROPERTIES (id, name)
     )
     EDGE TABLES(
       bank_transfers AS transfers
         KEY (txn_id)
         SOURCE KEY (src_acct_id) REFERENCES accounts (id)
         DESTINATION KEY (dst_acct_id) REFERENCES accounts (id)
         PROPERTIES (description, amount)
     ) OPTIONS (PG_PGQL)

The following graph concepts are explained by mapping the database tables to the graph and using the preceding PGQL DDL statement:

For more details on the CREATE PROPERTY GRAPH statement, see the PGQL Specification.

Refer to the following table for creating a property graph:

Table: CREATE PROPERTY GRAPH Statement Support

Method More Information
Create a property graph in the graph server (PGX) using the oracle.pgx.api Java package. Java APIs for Executing CREATE PROPERTY GRAPH Statements
Create a property graph in the graph server (PGX) using the pypgx.api Python package. Python APIs for Executing CREATE PROPERTY GRAPH Statements
Create a PGQL property graph in your database. Creating a PGQL Property Graph

Creating a PGQL Property Graph with the BASE_GRAPHS Clause

You can create a PGQL property graph by providing a list of existing PGQL property graphs.

You can specify the BASE GRAPHS clause in the CREATE PROPERTY GRAPH DDL statement for specifying one or more existing PGQL property graphs from which you wish to create the new PGQL property graph. It is allowed to specify the BASE GRAPHS clause without specifying the VERTEX TABLES and EDGE TABLES clauses.

The syntax of the BASE GRAPHS clause in the CREATE PROPERTY GRAPH statement is as shown:

CreatePropertyGraph   ::= 'CREATE' 'PROPERTY' 'GRAPH' GraphName
                           BaseGraphs?
                           VertexTables?
                           EdgeTables?

BaseGraphs            ::= 'BASE' 'GRAPHS' '(' BaseGraph ( ',' BaseGraph )* ')'

BaseGraph             ::= SchemaQualifiedName

ElementTablesClause   ::=   AllElementTables
                          | ElementTablesList

AllElementTables      ::= 'ALL' 'ELEMENT' 'TABLES' ExceptElementTables?

ExceptElementTables   ::= 'EXCEPT' '(' ElementTableReference ( ',' ElementTableReference )* ')'

ElementTablesList     ::= '(' ElementTable ( ',' ElementTable )* ')'

ElementTable          ::= ElementTableReference TableAlias?

ElementTableReference ::= Identifier

The BASE GRAPHS clause option allows you to duplicate a graph using a different name.

CREATE PROPERTY GRAPH <new_graph>
  BASE GRAPHS (<old_graph>)
  OPTIONS ( PG_PGQL )

Also, note that once the new_graph is created, it does not have any dependency on old_graph. This implies that updating or deleting the old_graph has no impact on the new_graph.

Consider the following example schema:

Figure: Example Schema

Description of image follows

Description of the illustration base_graphs_example_schema.png

Assume that the following two graphs, social_network and bank_transactions, are created from the preceding schema:

Figure: Graphs Created from the Example Schema

Description of image follows

Description of the illustration bank_txn_and_social_nw_graph.png

Using the BASE GRAPHS clause, you can then create a new PGQL property graph by establishing a relationship between both the preceding graphs as shown:

Figure: Financial_Transactions Graph

Description of image follows

Description of the illustration base_graph_diagram.png

To obtain this new graph based on the social_network and bank_transactions graphs:

  1. Specify the names of the two graphs, social_network and bank_transactions, in the BASE GRAPHS clause. If a base graph does not exist in the current schema, then the user must specify the schema name.

  2. Eliminate the Knows edge in the social_network graph. This can be achieved by using the ALL ELEMENT TABLES EXCEPT clause and specifying the table_name of that edge. Alternatively, you can use the ELEMENT TABLES clause and specify only the two tables, Persons and Companies.

  3. Create a new edge between the Accounts vertex in the bank_transactions graph and the Persons vertex in the social_network graph.

  4. Create a new edge between the Accounts vertex in the bank_transactions graph and the Companies vertex in the social_network graph.

The optimized CREATE PROPERTY GRAPH statement with the BASE GRAPHS clause to create the new PGQL property graph is as shown:

CREATE PROPERTY GRAPH financial_transactions
  BASE GRAPHS(
    bank_transactions,
    social_network ALL ELEMENT TABLES EXCEPT ( knows )
  )
  EDGE TABLES(
    Accounts AS PersonOwner
      SOURCE KEY ( "number" ) REFERENCES Accounts ( "number" )
      DESTINATION Persons
      LABEL owner NO PROPERTIES,
    Accounts AS CompanyOwner
      SOURCE KEY ( "number" ) REFERENCES Accounts ( "number" )
      DESTINATION Companies
      LABEL owner NO PROPERTIES
  ) OPTIONS ( PG_PGQL )

Creating a PGQL Property Graph with Arbitrary Property Expressions

You can create a PGQL property graph with vertex and edge properties mapped to arbitrary property expressions.

For instance, consider the following example data. The table contains emp_dtls as a JSON column.

CREATE TABLE emp_data (
  emp_id NUMBER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  emp_dtls JSON,
  CONSTRAINT emp_pk PRIMARY KEY (emp_id));

INSERT INTO emp_data (emp_dtls)
       VALUES ('{"name":"John","department":"IT","role":"Software Developer"}');

INSERT INTO emp_data (emp_dtls)
       VALUES ('{"name":"Mary","department":"HR","role":"HR Manager"}');

INSERT INTO emp_data (emp_dtls)
       VALUES ('{"name":"Bob","department":"IT","role":"Technical Consultant"}');

INSERT INTO emp_data (emp_dtls)
       VALUES ('{"name":"Alice","department":"HR","role":"HR Assistant"}');

You can then create a PGQL property graph with vertex and edge properties mapped to JSON data using the JSON_VALUE function.

CREATE PROPERTY GRAPH g
VERTEX TABLES (
  emp_data PROPERTIES (
           JSON_VALUE(emp_dtls, '$.name') AS name,
           JSON_VALUE(emp_dtls, '$.department') AS department,
           JSON_VALUE(emp_dtls, '$.role') AS role)
) OPTIONS(PG_PGQL)

Finally, you can query the vertex and edge properties of the graph as shown:

SELECT *
FROM GRAPH_TABLE ( g
  MATCH (n IS emp_data)
  COLUMNS (n.name, n.department, n.role) )

The query produces the following output:

+-------------------------------------------+
| NAME  | DEPARTMENT | ROLE                 |
+-------------------------------------------+
| John  | IT         | Software Developer   |
| Mary  | HR         | HR Manager           |
| Bob   | IT         | Technical Consultant |
| Alice | HR         | HR Assistant         |
+-------------------------------------------+