3 Introduction to SQL Property Graphs

You can work with SQL property graphs in any SQL based interface (such as SQL Developer, SQLPLUS, or SQLcl) or from a Java program using JDBC.

Using SQL statements, you can perform the following:

  • Create a SQL property graph from existing database objects in your schema, such as:
  • Create a synonym for a SQL property graph.
  • Revalidate a SQL property graph.
  • Run graph pattern matching queries on a SQL property graph.
  • Drop a SQL property graph.

For example, the following figure shows the creation of a SQL property graph using the SQL Developer tool.

Figure 3-1 Using SQL Developer to Create a SQL Property Graph

Description of Figure 3-1 follows
Description of "Figure 3-1 Using SQL Developer to Create a SQL Property Graph"

3.1 Quick Start for Working with SQL Property Graphs

This tutorial helps you get started on creating and querying a SQL property graph.

In order to try this tutorial, ensure that you meet the following requirements:

In the following tutorial, the examples in Step 1, Step 2, and Step 4 are performed using the SQLcl tool. However, you can run these examples using any SQL based interface.
  1. Create a SQL property graph using the CREATE PROPERTY GRAPH DDL statement.
    SQL> CREATE PROPERTY GRAPH bank_sql_pg
      2    VERTEX TABLES (
      3      bank_accounts
      4      KEY (id)
      5      LABEL account
      6      PROPERTIES ALL COLUMNS
      7    )
      8    EDGE TABLES (
      9      bank_txns
     10        KEY (txn_id)
     11        SOURCE KEY (from_acct_id) REFERENCES bank_accounts (id)
     12        DESTINATION KEY (to_acct_id) REFERENCES bank_accounts (id)
     13        LABEL transfer
     14        PROPERTIES ALL COLUMNS
     15*   );
    
    Property created.

    On execution, the bank_sql_pg graph schema object is created in the database. The graph is made up of one vertex graph element table (bank_accounts) and one edge graph element table (bank_txns).

    See Creating a SQL Property Graph to learn the concepts of graph element tables, keys, labels and properties.

  2. Run a GRAPH_TABLE query, on the newly created graph, to list all the transactions from the account with id value 816.
    SQL> SELECT * FROM GRAPH_TABLE (bank_sql_pg
      2    MATCH
      3    (a IS account WHERE a.id = 816) -[e IS transfer]-> (b IS account)
      4    COLUMNS (a.id AS acc_a, e.amount AS amount, b.id AS acc_b)
      5* );
    
       ACC_A    AMOUNT    ACC_B
    ________ _________ ________
         816      4713      287
         816      8001      590
         816      4186      934
         816      3718      289
         816      4039      812

    See SQL GRAPH_TABLE Queries to understand more about GRAPH_TABLE queries..

  3. Optionally, if you have installed the graph server (PGX), then you can also visualize the preceding GRAPH_TABLE query, using the graph visualization tool.

    The only difference is that you must return the vertex and edge IDs in order to visualize the vertices and edges of the GRAPH_TABLE query together with their IDs and all their labels and properties. Note that the COLUMNS clause in the following example uses the VERTEX_ID and EDGE_ID operators:

    Figure 3-2 Visualizing GRAPH_TABLE Query

    Description of Figure 3-2 follows
    Description of "Figure 3-2 Visualizing GRAPH_TABLE Query"
  4. Drop the SQL property graph after running the graph queries.
    SQL> drop property graph bank_sql_pg;
    
    Property dropped.