Graph Management

Graph management operations allow creating, destroying, moving and copying named graphs in the Graph Store, or adding the contents of one graph to another. Operations for creation and destruction are not required to result in any actions, since Graph Stores are not required to record the existence of empty named graphs.

The default graph in a Graph Store always exists.

SPARQL 1.1 Update provides these graph management operations:

  • The CREATE operation creates a new graph in stores that support empty graphs.
  • The DROP operation removes a graph and all of its contents.
  • The COPY operation modifies a graph to contain a copy of another.
  • The MOVE operation moves all of the data from one graph into another.
  • The ADD operation reproduces all data from one graph into another.

CREATE

This operation creates a graph in the Graph Store:

**CREATE** ( **SILENT** )? **GRAPH** *[IRIref](../../../sparql11-query/grammar/#rIRIREF)*

For stores that record empty graphs, this will create a new empty graph in the store with a name specified by the IRI. If the graph already exists, then a failureSHOULD be returned, except when the SILENT keyword is used; in either case, the contents of already existing graphs remain unchanged. If the graph may not be created, then a failureMUST be returned, except when the SILENT keyword is used.

Stores that do not record empty named graphs will always return success on creation of a non-existing graph.

DROP

**DROP**  ( **SILENT** )? (**GRAPH** *[IRIref](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT** | **NAMED** | **ALL** )

The DROP operation removes the specified graph(s) from the Graph Store. The GRAPH keyword is used to remove a graph denoted by IRIref, the DEFAULT keyword is used to remove the default graph from the Graph Store, the NAMED keyword is used to remove all named graphs from the Graph Store, and the ALL keyword is used to remove all graphs from the Graph Store, i.e., resetting the store. After successful completion of this operation, the specified graphs are no longer available for further graph update operations. However, in case the DEFAULT graph of the Graph Store is dropped, implementations MUST restore it after it was removed, i.e., DROP DEFAULT is equivalent to CLEAR DEFAULT.

If the store records the existence of empty graphs, then the SPARQL 1.1 Update service, by default, SHOULD return failure if the specified named graph does not exist. If SILENT is present, the result of the operation will always be success.

Stores that do not record empty graphs will always return success.

COPY

The COPY operation is a shortcut for inserting all data from an input graph into a destination graph. Data from the input graph is not affected, but data from the destination graph, if any, is removed before insertion.

**COPY** ( **SILENT** )? ( ( **GRAPH** )? *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**) **TO** ( ( **GRAPH** )? *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT** )

is similar in operation to:

**DROP** **SILENT** (**GRAPH** *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**);
      **INSERT** { ( **GRAPH** *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } } WHERE** { ( **GRAPH** *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } }**

The difference between COPY and the DROP/INSERT combination is that if COPY is used to copy a graph onto itself then no operation will be performed and the data will be left as it was. Using DROP/INSERT in this situation would result in an empty graph.

If the destination graph does not exist, it will be created. By default, the service MAY return failure if the input graph does not exist. If SILENT is present, the result of the operation will always be success.

Example 13:

This example request copies all statements from the default graph to a named graph:

COPY DEFAULT TO <http://example.org/named>

Data before:

**# Default graph**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .
**# Graph http://example.org/named**
<http://example/fred> a foaf:Person .
<http://example/fred> foaf:givenName "Fred" .

Data after:

**# Default graph**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .
**# Graph http://example.org/named**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .

Note that the original content in http://example.org/named is lost by a COPY operation.

MOVE

The MOVE operation is a shortcut for moving all data from an input graph into a destination graph. The input graph is removed after insertion and data from the destination graph, if any, is removed before insertion.

**MOVE** (**SILENT**)? ( ( **GRAPH** )? *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**) **TO** ( ( **GRAPH** )? *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**)

is similar in operation to:

**DROP SILENT** (**GRAPH** *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**);
      **INSERT** { ( **GRAPH** *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } } WHERE** { ( **GRAPH** *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } };
DROP** ( **GRAPH** *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**)

The difference between MOVE and the DROP/INSERT/DROP combination is that if MOVE is used to move a graph onto itself then no operation will be performed and the data will be left as it was. Using DROP/INSERT/DROP in this situation would result in the graph being removed.

If the destination graph does not exist, it will be created. By default, the service MAY return failure if the input graph does not exist. If SILENT is present, the result of the operation will always be success.

Example 14:

This example request moves all statements from the default graph into a named graph:

MOVE DEFAULT TO <http://example.org/named>

Data before:

**# Default graph**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .
**# Graph http://example.org/named**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/fred> a foaf:Person .
<http://example/fred> foaf:givenName "Fred" .

Data after:

**# Default graph**
**# Graph http://example.org/named**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .

Note that the original content in http://example.org/named is lost by a MOVE operation.

ADD

The ADD operation is a shortcut for inserting all data from an input graph into a destination graph. Data from the input graph is not affected, and initial data from the destination graph, if any, is kept intact.

**ADD** ( **SILENT** )? ( ( **GRAPH** )? *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**) **TO** ( ( **GRAPH** )? *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* | **DEFAULT**)

is equivalent to:

**INSERT** { ( **GRAPH** *[IRIref_to](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } } WHERE** { ( **GRAPH** *[IRIref_from](../../../sparql11-query/grammar/#rIRIREF)* )? **{ ?s ?p ?o } }**

If the destination graph does not exist, it will be created. By default, the service MAY return failure if the input graph does not exist. If SILENT is present, the result of the operation will always be success.

Example 15:

This example request adds all statements from the default graph to a named graph:

ADD DEFAULT TO <http://example.org/named>

Data before:

**# Default graph**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .
**# Graph http://example.org/named**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/fred> a foaf:Person .

Data after:

**# Default graph**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .
**# Graph http://example.org/named**
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<http://example/fred> a foaf:Person .

<http://example/william> a foaf:Person .
<http://example/william> foaf:givenName "William" .
<http://example/william> foaf:mbox  <mailto:bill@example> .