Virtuoso SPARQL 1.1. Update Examples

What?

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.

Why?

Change existing graphs in the Quad Store.

How?

SPARQL 1.1 Update provides these graph update operations ( See more from the specification ):

Here are some examples showcasing Virtuoso's support for this functionality:

Delete and Add a Triple to a Graph Example

This example demonstrates two operations -- delete a triple and add a triple in the named graph identified by the IRI <urn:sparql:tests:update> :

  1. Assume the following Raw Data Representation in Turtle:

    <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Desing" .

  2. Load the sample data:

    INSERT DATA { GRAPH <urn:sparql:tests:update> { <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" . } }

  3. Delete the triple from the graph:

    DELETE DATA { GRAPH <urn:sparql:tests:update> { <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" } }

  4. Insert a new triple to the graph:

    INSERT DATA { GRAPH <urn:sparql:tests:update> { <#book2> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" } }

  5. Query the graph data:

    SELECT * FROM <urn:sparql:tests:update> WHERE { ?s ?p ?o }

  6. View the SPARQL Query Definition via SPARQL Protocol URL
  7. View the SPARQL Query Results via SPARQL Protocol URL

DELETE/Insert WITH Clause Usage Example

This example demonstrates how to rename all people with the given name "Bill" to "William":

  1. Assume the following Raw Data Representation in Turtle:

    <#president25> <http://xmlns.com/foaf/0.1/givenName> "Bill" . <#president25> <http://xmlns.com/foaf/0.1/foaf:familyName> "McKinley" . <#president27> <http://xmlns.com/foaf/0.1/foaf:givenName> "Bill" . <#president27> <http://xmlns.com/foaf/0.1/foaf:familyName> "Taft" . <#president42> <http://xmlns.com/foaf/0.1/foaf:givenName> "Bill" . <#president42> <http://xmlns.com/foaf/0.1/foaf:familyName> "Clinton" .

  2. Load the sample data:

    INSERT DATA { GRAPH <urn:sparql:tests:update:insert:delete:with> { <#president25> <http://xmlns.com/foaf/0.1/givenName> "Bill" . <#president25> <http://xmlns.com/foaf/0.1/foaf:familyName> "McKinley" . <#president27> <http://xmlns.com/foaf/0.1/foaf:givenName> "Bill" . <#president27> <http://xmlns.com/foaf/0.1/foaf:familyName> "Taft" . <#president42> <http://xmlns.com/foaf/0.1/foaf:givenName> "Bill" . <#president42> <http://xmlns.com/foaf/0.1/foaf:familyName> "Clinton" . } }

  3. Rename all people with the given name "Bill" to "William":

    WITH <urn:sparql:tests:update:insert:delete:with> DELETE { ?person <http://xmlns.com/foaf/0.1/givenName> 'Bill' } INSERT { ?person <http://xmlns.com/foaf/0.1/givenName> 'William' } WHERE { ?person <http://xmlns.com/foaf/0.1/givenName> 'Bill' }

  4. Query the graph data:

    SELECT * FROM <urn:sparql:tests:update:insert:delete:with> WHERE { ?s ?p ?o }

  5. View the SPARQL Query Definition via SPARQL Protocol URL
  6. View the SPARQL Query Results via SPARQL Protocol URL

DELETE/Insert WITH MODIFY Usage Example

This example demonstrates use of MODIFY to alter the objects of a schema:WebPage? relation using objects of a foaf:homePage relation. In this example, we have a constant relation subject identified the HTTP URI <http://example.com/product/Sample1#this> .

  1. Assume the following Raw Data Representation in Turtle:

    <http://example.com/product/Sample1#this> <http://schema.org/WebPage> <http://sample1.com/index1> . <http://example.com/product/Sample2#this> <http://schema.org/WebPage> <http://sample2.com/index2> . <http://example.com/product/Sample3#this> <http://schema.org/WebPage> <http://sample3.com/index3> . <http://example.com/product/Sample4#this> <http://schema.org/WebPage> <http://sample4.com/index4> . <http://example.com/product/Sample5#this> <http://schema.org/WebPage> <http://sample5.com/index5> .

  2. Load the sample data:

    INSERT DATA { GRAPH <urn:sparql:tests:update:insert:delete:modify:graph1> { <http://example.com/product/Sample1#this> <http://schema.org/WebPage> <http://sample1.com/index1> . <http://example.com/product/Sample2#this> <http://schema.org/WebPage> <http://sample2.com/index2> . <http://example.com/product/Sample3#this> <http://schema.org/WebPage> <http://sample3.com/index3> . <http://example.com/product/Sample4#this> <http://schema.org/WebPage> <http://sample4.com/index4> . <http://example.com/product/Sample5#this> <http://schema.org/WebPage> <http://sample5.com/index5> . } }

  3. Assume the following Raw Data Representation in Turtle:

    <http://sample1.com/index1> <http://xmlns.com/foaf/0.1/homepage> <http://sample1.com/> . <http://sample2.com/index2> <http://xmlns.com/foaf/0.1/homepage> <http://sample2.com/> . <http://sample3.com/index3> <http://xmlns.com/foaf/0.1/homepage> <http://sample3.com/> . <http://sample4.com/index4> <http://xmlns.com/foaf/0.1/homepage> <http://sample4.com/> . <http://sample5.com/index5> <http://xmlns.com/foaf/0.1/homepage> <http://sample5.com/> .

  4. Load the sample data:

    INSERT DATA { GRAPH <urn:sparql:tests:update:insert:delete:modify:graph2> { <http://sample1.com/index1> <http://xmlns.com/foaf/0.1/homepage> <http://sample1.com/> . <http://sample2.com/index2> <http://xmlns.com/foaf/0.1/homepage> <http://sample2.com/> . <http://sample3.com/index3> <http://xmlns.com/foaf/0.1/homepage> <http://sample3.com/> . <http://sample4.com/index4> <http://xmlns.com/foaf/0.1/homepage> <http://sample4.com/> . <http://sample5.com/index5> <http://xmlns.com/foaf/0.1/homepage> <http://sample5.com/> . } }

  5. Replace schema:WebPage? objects with foaf:home objects values:

    PREFIX schema: <http://schema.org/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> MODIFY <urn:sparql:tests:update:insert:delete:modify:graph1> DELETE { <http://example.com/product/Sample1#this> schema:WebPage ?s . } INSERT { <http://example.com/product/Sample1#this> schema:WebPage ?ns . } WHERE { GRAPH <urn:sparql:tests:update:insert:delete:modify:graph1> { <http://example.com/product/Sample1#this> schema:WebPage ?s . } GRAPH <urn:sparql:tests:update:insert:delete:modify:graph2> { ?s foaf:homepage ?ns . } }

  6. Query the graph data:

    SELECT * FROM <urn:sparql:tests:update:insert:delete:modify:graph1> WHERE { ?s ?p ?o . FILTER (?s = <http://example.com/product/Sample1#this>) . }

  7. View the SPARQL Query Definition via SPARQL Protocol URL
  8. View the SPARQL Query Results via SPARQL Protocol URL

Related