<docbook><section><title>VirtTipsAndTricksSPARQL11Update</title><title>Virtuoso SPARQL 1.1.
 Update Examples</title>Virtuoso SPARQL 1.1.
 Update Examples
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2">What?</bridgehead>
 This guide contains Virtuoso SPARQL 1.1.
 Update Usage example queries which you can run against any SPARQL endpoint that supports SPARQL 1.1 and the ability to allow a verified user perform INSERT operations.<bridgehead class="http://www.w3.org/1999/xhtml:h2">Why?</bridgehead>
 Change existing graphs in the Quad Store.<bridgehead class="http://www.w3.org/1999/xhtml:h2">How?</bridgehead>
<para>SPARQL 1.1 Update provides these graph update operations ( <ulink url="http://www.w3.org/TR/2012/PR-sparql11-update-20121108/#graphUpdate">See more from the specification</ulink> ):</para>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="VirtTipsAndTricksSPARQL11Insert">INSERT DATA</ulink> -- adds some triples, given inline in the request, into a graph.
 This SHOULD create the destination graph if it does not exist.
 If the graph does not exist and it can not be created for any reason, then a failure MUST be returned.
</listitem>
<listitem><ulink url="VirtTipsAndTricksSPARQL11Delete">DELETE DATA</ulink> -- removes some triples, given inline in the request, if the respective graph contains those.
</listitem>
<listitem>The fundamental pattern-based actions for graph updates are INSERT and DELETE.
 These actions consist of groups of triples to be deleted and groups of triples to be added based on query patterns.
 The difference between INSERT / DELETE and INSERT DATA / DELETE DATA is that INSERT DATA and DELETE DATA do not substitute bindings into a template from a pattern.
</listitem>
<listitem><ulink url="VirtTipsAndTricksSPARQL11Load">LOAD</ulink> -- reads the contents of a document representing a graph into a graph in the Graph Store.
</listitem>
<listitem><ulink url="VirtTipsAndTricksSPARQL11Clear">CLEAR</ulink> -- removes all the triples in (one or more) graphs.</listitem>
</itemizedlist><para>Here are some examples showcasing Virtuoso&#39;s support for this functionality:</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">Delete and Add a Triple to a Graph Example</bridgehead>
<para> This example demonstrates two operations -- delete a triple and add a triple in the named graph identified by the IRI &lt;urn:sparql:tests:update&gt; :</para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>&lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;Fundamentals of Compiler Desing&quot; .
</programlisting></listitem>
<listitem>Load the sample data: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:update&gt;
      { 
        &lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;Fundamentals of Compiler Design&quot; .
      } 
  }
</programlisting></listitem>
<listitem>Delete the triple from the graph: <programlisting>DELETE DATA
  { 
    GRAPH &lt;urn:sparql:tests:update&gt; 
      { 
        &lt;#book1&gt;  &lt;http://purl.org/dc/elements/1.1/title&gt;  &quot;Fundamentals of Compiler Design&quot; 
      } 
  }  
</programlisting></listitem>
<listitem>Insert a new triple to the graph: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:update&gt;
      { 
        &lt;#book2&gt;  &lt;http://purl.org/dc/elements/1.1/title&gt;  &quot;Fundamentals of Compiler Design&quot; 
      } 
  }
</programlisting></listitem>
<listitem>Query the graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:update&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/Ua3MHI">View the SPARQL Query Definition via SPARQL Protocol URL</ulink> </listitem>
<listitem><ulink url="http://bit.ly/UKCANo">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">DELETE/Insert WITH Clause Usage Example</bridgehead>
<para>This example demonstrates how to rename all people with the given name &quot;Bill&quot; to &quot;William&quot;:</para>
<para> </para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>&lt;#president25&gt; &lt;http://xmlns.com/foaf/0.1/givenName&gt;       &quot;Bill&quot; . 
&lt;#president25&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;McKinley&quot; . 
&lt;#president27&gt; &lt;http://xmlns.com/foaf/0.1/foaf:givenName&gt;  &quot;Bill&quot; . 
&lt;#president27&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;Taft&quot; . 
&lt;#president42&gt; &lt;http://xmlns.com/foaf/0.1/foaf:givenName&gt;  &quot;Bill&quot; . 
&lt;#president42&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;Clinton&quot; .  
</programlisting></listitem>
<listitem>Load the sample data: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:update:insert:delete:with&gt; 
      { 
        &lt;#president25&gt; &lt;http://xmlns.com/foaf/0.1/givenName&gt;       &quot;Bill&quot; . 
        &lt;#president25&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;McKinley&quot; . 
        &lt;#president27&gt; &lt;http://xmlns.com/foaf/0.1/foaf:givenName&gt;  &quot;Bill&quot; . 
        &lt;#president27&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;Taft&quot; . 
        &lt;#president42&gt; &lt;http://xmlns.com/foaf/0.1/foaf:givenName&gt;  &quot;Bill&quot; . 
        &lt;#president42&gt; &lt;http://xmlns.com/foaf/0.1/foaf:familyName&gt; &quot;Clinton&quot; .    
      } 
  }
</programlisting></listitem>
<listitem>Rename all people with the given name &quot;Bill&quot; to &quot;William&quot;: <programlisting>WITH &lt;urn:sparql:tests:update:insert:delete:with&gt;
DELETE { ?person &lt;http://xmlns.com/foaf/0.1/givenName&gt; &#39;Bill&#39; }
INSERT { ?person &lt;http://xmlns.com/foaf/0.1/givenName&gt; &#39;William&#39; }
WHERE
  { 
    ?person &lt;http://xmlns.com/foaf/0.1/givenName&gt; &#39;Bill&#39;
  } 
</programlisting></listitem>
<listitem>Query the graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:update:insert:delete:with&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/WZVZbV">View the SPARQL Query Definition via SPARQL Protocol URL</ulink> </listitem>
<listitem><ulink url="http://bit.ly/11uJAgW">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h3">DELETE/Insert WITH MODIFY Usage Example</bridgehead>
<para>This example demonstrates use of MODIFY to alter the objects of a schema:<ulink url="WebPage">WebPage</ulink> relation using objects of a foaf:homePage relation.
 In this example, we have a constant relation subject identified the HTTP URI &lt;<ulink url="http://example.com/product/Sample1#this&gt;">http://example.com/product/Sample1#this&gt;</ulink> .</para>
<para> </para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>&lt;http://example.com/product/Sample1#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample1.com/index1&gt; . 
&lt;http://example.com/product/Sample2#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample2.com/index2&gt; . 
&lt;http://example.com/product/Sample3#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample3.com/index3&gt; . 
&lt;http://example.com/product/Sample4#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample4.com/index4&gt; . 
&lt;http://example.com/product/Sample5#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample5.com/index5&gt; .  
</programlisting></listitem>
<listitem>Load the sample data: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:update:insert:delete:modify:graph1&gt; 
      { 
        &lt;http://example.com/product/Sample1#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample1.com/index1&gt; . 
        &lt;http://example.com/product/Sample2#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample2.com/index2&gt; . 
        &lt;http://example.com/product/Sample3#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample3.com/index3&gt; . 
        &lt;http://example.com/product/Sample4#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample4.com/index4&gt; . 
        &lt;http://example.com/product/Sample5#this&gt; &lt;http://schema.org/WebPage&gt; &lt;http://sample5.com/index5&gt; . 
      } 
  }
</programlisting></listitem>
<listitem>Assume the following Raw Data Representation in Turtle: <programlisting>&lt;http://sample1.com/index1&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample1.com/&gt; . 
&lt;http://sample2.com/index2&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample2.com/&gt; .
&lt;http://sample3.com/index3&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample3.com/&gt; . 
&lt;http://sample4.com/index4&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample4.com/&gt; . 
&lt;http://sample5.com/index5&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample5.com/&gt; . 
</programlisting></listitem>
<listitem>Load the sample data: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:update:insert:delete:modify:graph2&gt; 
      { 
        &lt;http://sample1.com/index1&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample1.com/&gt; . 
        &lt;http://sample2.com/index2&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample2.com/&gt; .
        &lt;http://sample3.com/index3&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample3.com/&gt; . 
        &lt;http://sample4.com/index4&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample4.com/&gt; . 
        &lt;http://sample5.com/index5&gt; &lt;http://xmlns.com/foaf/0.1/homepage&gt; &lt;http://sample5.com/&gt; . 
      } 
  }
</programlisting></listitem>
<listitem>Replace schema:<ulink url="WebPage">WebPage</ulink> objects with foaf:home objects values: <programlisting>PREFIX schema: &lt;http://schema.org/&gt;
PREFIX foaf:   &lt;http://xmlns.com/foaf/0.1/&gt; 
MODIFY &lt;urn:sparql:tests:update:insert:delete:modify:graph1&gt;
DELETE 
  {
    &lt;http://example.com/product/Sample1#this&gt; schema:WebPage ?s .
  }
INSERT 
  {
    &lt;http://example.com/product/Sample1#this&gt; schema:WebPage ?ns .
  }
WHERE 
  {
    GRAPH &lt;urn:sparql:tests:update:insert:delete:modify:graph1&gt; 
      {
      &lt;http://example.com/product/Sample1#this&gt; schema:WebPage ?s .
      }
    GRAPH &lt;urn:sparql:tests:update:insert:delete:modify:graph2&gt; 
      {
      ?s foaf:homepage ?ns .
      }   
  }    
</programlisting></listitem>
<listitem>Query the graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:update:insert:delete:modify:graph1&gt; 
WHERE 
  {
    ?s ?p ?o .
    FILTER (?s = &lt;http://example.com/product/Sample1#this&gt;) .
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/1EuDfaP">View the SPARQL Query Definition via SPARQL Protocol URL</ulink> </listitem>
<listitem><ulink url="http://bit.ly/1srjQyX">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2">Related</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="http://www.w3.org/TR/2012/PR-sparql11-update-20121108/#graphUpdate">SPARQL 1.1 Update</ulink> </listitem>
<listitem><ulink url="http://www.w3.org/TR/2012/PR-sparql11-update-20121108/#deleteInsert">SPARQL 1.1. DELETE/INSERT + WITH clause</ulink> </listitem>
<listitem><ulink url="VirtTipsAndTricksGuide">Virtuoso Tips and Tricks Collection</ulink> </listitem>
<listitem><ulink url="VirtTipsAndTricksSPARQL11FeaturesExamplesCollection">Virtuoso SPARQL 1.1 Usage Examples Collection</ulink> </listitem>
<listitem><ulink url="http://virtuoso.openlinksw.com/tutorials/sparql/SPARQL_Tutorials_Part_9/SPARQL_Tutorials_Part_9.html">Virtuoso SPARQL 1.1 Syntax Tutorial</ulink> </listitem>
<listitem><ulink url="http://docs.openlinksw.com/virtuoso/rdfsparql.html">Virtuoso Documentation</ulink> </listitem>
</itemizedlist></section></docbook>