<docbook><section><title>VirtTipsAndTricksSPARQL11Insert</title><title>Virtuoso SPARQL 1.1.
 INSERT Usage Examples</title>Virtuoso SPARQL 1.1.
 INSERT Usage Examples
<bridgehead class="http://www.w3.org/1999/xhtml:h2">What?</bridgehead>
 This guide contains Virtuoso SPARQL 1.1.
 INSERT Usage examples 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>
 Using INSERTS provides flexible way of adding data to graphs.<bridgehead class="http://www.w3.org/1999/xhtml:h2">How?</bridgehead>
<para>Here are some examples showcasing Virtuoso&#39;s support for this functionality:</para>
<para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3">INSERT DATA Examples</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h4">Adding One Triple Example</bridgehead>
<para>This example request describes 1 triple to be added to the named graph identified by the IRI &lt;urn:sparql:tests:insert:data&gt;</para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>&lt;#book1&gt; &lt;#price&gt; 42 .
</programlisting></listitem>
<listitem>Load the sample data: <programlisting>INSERT DATA
  { 
    GRAPH &lt;urn:sparql:tests:insert:data&gt; 
      { 
        &lt;#book1&gt; &lt;#price&gt; 42 
      } 
  }
</programlisting></listitem>
<listitem>Query the graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:insert:data&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/WFcxEN">View the SPARQL Query Definition via SPARQL Protocol URL</ulink>; </listitem>
<listitem><ulink url="http://bit.ly/Xc45My">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h3">INSERT (Informative) Examples</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h4">Coping Triples Based on a Pattern Example</bridgehead>
<para>This example copies triples from one named graph to another named graph based on a pattern:</para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>@prefix xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt; .

&lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;Fundamentals of Compiler Design&quot; .
&lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/date&gt;  &quot;1977-01-01T00:00:00-02:00&quot;^^xsd:dateTime .

&lt;#book2&gt; &lt;#price&gt; 42 .
&lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/title&gt;   &quot;David Copperfield&quot; .
&lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/creator&gt; &quot;Edmund Wells&quot; .
&lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/date&gt;    &quot;1948-01-01T00:00:00-02:00&quot;^^xsd:dateTime .

&lt;#book3&gt; &lt;http://purl.org/dc/elements/1.1/title&gt;   &quot;SPARQL 1.1 Tutorial&quot; .
</programlisting></listitem>
<listitem>Load the sample data into graph &lt;urn:sparql:tests:insert:informative&gt;; <programlisting>PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt; 

INSERT INTO &lt;urn:sparql:tests:insert:informative&gt; 
  {
    &lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;Fundamentals of Compiler Design&quot; .
    &lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/date&gt;  &quot;1977-01-01T00:00:00-02:00&quot;^^xsd:dateTime .

    &lt;#book2&gt; &lt;#price&gt; 42 .
    &lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/title&gt;   &quot;David Copperfield&quot; .
    &lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/creator&gt; &quot;Edmund Wells&quot; .
    &lt;#book2&gt; &lt;http://purl.org/dc/elements/1.1/date&gt;    &quot;1948-01-01T00:00:00-02:00&quot;^^xsd:dateTime .
  }
</programlisting></listitem>
<listitem>Assume another Raw Data Representation in Turtle: <programlisting>&lt;#book4&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.0 Tutorial&quot; .
</programlisting></listitem>
<listitem>Load the sample data into graph &lt;urn:sparql:tests:insert:informative2&gt;; <programlisting>INSERT INTO &lt;urn:sparql:tests:insert:informative2&gt; 
  {
    &lt;#book4&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.0 Tutorial&quot; .
  }
</programlisting></listitem>
<listitem>Copy triples from the &lt;urn:sparql:tests:insert:informative2&gt; named graph to the &lt;urn:sparql:tests:insert:informative&gt; named graph based on a pattern: <programlisting>PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;

INSERT 
  { 
    GRAPH &lt;urn:sparql:tests:insert:informative2&gt; { ?book ?p ?v } 
  }
WHERE
  { 
    GRAPH  &lt;urn:sparql:tests:insert:informative&gt;
      { 
        ?book &lt;http://purl.org/dc/elements/1.1/date&gt; ?date .
        FILTER ( ?date &gt; &quot;1970-01-01T00:00:00-02:00&quot;^^xsd:dateTime )
        ?book ?p ?v
      } 
  }	
</programlisting></listitem>
<listitem>Query the &lt;urn:sparql:tests:insert:informative2&gt; graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:insert:informative2&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/VscJad">View the SPARQL Query Definition via SPARQL Protocol URL</ulink>; </listitem>
<listitem><ulink url="http://bit.ly/U7dlqI">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><bridgehead class="http://www.w3.org/1999/xhtml:h4">Coping Triples Based on a Name and Mail Example</bridgehead>
<para>This example copies triples of name and email from one named graph to another.
 Some individuals may not have an address, but the name is copied regardless:</para>
<orderedlist spacing="compact"><listitem>Assume the following Raw Data Representation in Turtle: <programlisting>_:a  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt;   &lt;http://xmlns.com/foaf/0.1/Person&gt; . 
_:a  &lt;http://xmlns.com/foaf/0.1/name&gt;                    &quot;Alice&quot; . 
_:a  &lt;http://xmlns.com/foaf/0.1/mbox&gt;                    &lt;mailto:alice@example.com&gt; . 

_:b  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt;   &lt;http://xmlns.com/foaf/0.1/Person&gt; . 
_:b  &lt;http://xmlns.com/foaf/0.1/name&gt;                    &quot;Bob&quot; . 
</programlisting></listitem>
<listitem>Load the sample data into graph &lt;urn:sparql:tests:insert:informative3&gt;; <programlisting>INSERT INTO &lt;urn:sparql:tests:insert:informative3&gt; 
  {
    _:a  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt;  &lt;http://xmlns.com/foaf/0.1/Person&gt; . 
    _:a  &lt;http://xmlns.com/foaf/0.1/name&gt;                   &quot;Alice&quot; . 
    _:a  &lt;http://xmlns.com/foaf/0.1/mbox&gt;                   &lt;mailto:alice@example.com&gt; . 

    _:b  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt;  &lt;http://xmlns.com/foaf/0.1/Person&gt; . 
    _:b  &lt;http://xmlns.com/foaf/0.1/name&gt;  &quot;Bob&quot; . 
  }
</programlisting></listitem>
<listitem>Assume in another named graph &lt;urn:sparql:tests:insert:informative4&gt; there are no triples inserted.
</listitem>
<listitem>Copy triples of name and email from &lt;urn:sparql:tests:insert:informative3&gt; to &lt;urn:sparql:tests:insert:informative4&gt; : <programlisting>INSERT 
  { 
    GRAPH &lt;urn:sparql:tests:insert:informative4&gt;
      {
        ?person  &lt;http://xmlns.com/foaf/0.1/name&gt;  ?name . 
        ?person  &lt;http://xmlns.com/foaf/0.1/mbox&gt;  ?email 
      }
  }
WHERE
  { 
    GRAPH  &lt;urn:sparql:tests:insert:informative3&gt;
      {
        ?person  &lt;http://xmlns.com/foaf/0.1/name&gt;  ?name . 
        OPTIONAL { ?person  &lt;http://xmlns.com/foaf/0.1/mbox&gt;  ?email } 
      } 
  }    
</programlisting></listitem>
<listitem>Query the &lt;urn:sparql:tests:insert:informative4&gt; graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:insert:informative4&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/WGBJ0v">View the SPARQL Query Definition via SPARQL Protocol URL</ulink>; </listitem>
<listitem><ulink url="http://bit.ly/TkyT1t">View the SPARQL Query Results via SPARQL Protocol URL</ulink></listitem>
</orderedlist><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h4">Copy Triples and Delete Physical Objects Example</bridgehead>
<para>This example request first copies triples from one named graph to another named graph based on a pattern; triples about all the copied objects that are classified as Physical Objects are then deleted.
 This demonstrates two operations in a single request, both of which share common PREFIX definitions:</para>
<para> </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 Design&quot; .
&lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/date&gt; &quot;1996-01-01T00:00:00-02:00&quot;^^xsd:dateTime .
&lt;#book1&gt; a &lt;http://purl.org/dc/dcmitype/PhysicalObject&gt; .

&lt;#book3&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.1 Tutorial&quot; .
</programlisting></listitem>
<listitem>Load the sample data into graph &lt;urn:sparql:tests:insert:informative5&gt;; <programlisting>INSERT INTO &lt;urn:sparql:tests:insert:informative5&gt; 
  {
    &lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;Fundamentals of Compiler Design&quot; .
    &lt;#book1&gt; &lt;http://purl.org/dc/elements/1.1/date&gt; &quot;1996-01-01T00:00:00-02:00&quot;^^xsd:dateTime .
    &lt;#book1&gt; a &lt;http://purl.org/dc/dcmitype/PhysicalObject&gt; .

    &lt;#book3&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.1 Tutorial&quot; .
  }
</programlisting></listitem>
<listitem>Assume another Raw Data Representation in Turtle: <programlisting>&lt;#book4&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.0 Tutorial&quot; .
</programlisting></listitem>
<listitem>Load the sample data into graph &lt;urn:sparql:tests:insert:informative6&gt;; <programlisting>INSERT INTO &lt;urn:sparql:tests:insert:informative6&gt; 
  {
    &lt;#book4&gt; &lt;http://purl.org/dc/elements/1.1/title&gt; &quot;SPARQL 1.0 Tutorial&quot; .
  }
</programlisting></listitem>
<listitem>Copy triples from &lt;urn:sparql:tests:insert:informative5&gt; to &lt;urn:sparql:tests:insert:informative6&gt; <programlisting>PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;

INSERT
  { 
    GRAPH &lt;urn:sparql:tests:insert:informative6&gt; { ?book ?p ?v } 
  }
WHERE
  { 
    GRAPH  &lt;urn:sparql:tests:insert:informative5&gt;
      { 
        ?book &lt;http://purl.org/dc/elements/1.1/date&gt; ?date . 
        FILTER ( ?date &lt; &quot;2000-01-01T00:00:00-02:00&quot;^^xsd:dateTime )
        ?book ?p ?v
      }
   } 
</programlisting></listitem>
<listitem>Delete triples about all the copied objects that are classified as Physical Objects: <programlisting>PREFIX xsd: &lt;http://www.w3.org/2001/XMLSchema#&gt;

WITH &lt;urn:sparql:tests:insert:informative5&gt;
DELETE
 { 
   ?book ?p ?v 
 }
WHERE
 { 
   ?book &lt;http://purl.org/dc/elements/1.1/date&gt; ?date ;
   a &lt;http://purl.org/dc/dcmitype/PhysicalObject&gt; .
   FILTER ( ?date &lt; &quot;2000-01-01T00:00:00-02:00&quot;^^xsd:dateTime ) 
   ?book ?p ?v .
 }   
</programlisting></listitem>
<listitem>Query the &lt;urn:sparql:tests:insert:informative5&gt; graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:insert:informative5&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/Xca6Jg">View the SPARQL Query Definition via SPARQL Protocol URL</ulink>; </listitem>
<listitem><ulink url="http://bit.ly/W6FCdX">View the SPARQL Query Results via SPARQL Protocol URL</ulink> </listitem>
<listitem>Query the &lt;urn:sparql:tests:insert:informative6&gt; graph data: <programlisting>SELECT * 
FROM &lt;urn:sparql:tests:insert:informative6&gt; 
WHERE 
  {
    ?s ?p ?o
  }
</programlisting></listitem>
<listitem><ulink url="http://bit.ly/XXIVCG">View the SPARQL Query Definition via SPARQL Protocol URL</ulink>; </listitem>
<listitem><ulink url="http://bit.ly/12eJG15">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/#insertData">SPARQL 1.1 INSERT DATA</ulink> </listitem>
<listitem><ulink url="http://www.w3.org/TR/rdf-sparql-protocol/">SPARQL Protocol (HTTP based Query Service)</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://bit.ly/Uo5hP6">Virtuoso SPARQL 1.1 INSERT Tutorial</ulink> </listitem>
<listitem><ulink url="http://docs.openlinksw.com/virtuoso/rdfsparql.html">Virtuoso Documentation</ulink></listitem>
</itemizedlist></section></docbook>