Virtuoso SPARQL 1.1. INSERT Usage Examples

What?

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.

Why?

Using INSERTS provides flexible way of adding data to graphs.

How?

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

INSERT DATA Examples

Adding One Triple Example

This example request describes 1 triple to be added to the named graph identified by the IRI <urn:sparql:tests:insert:data>

  1. Assume the following Raw Data Representation in Turtle:

    <#book1> <#price> 42 .

  2. Load the sample data:

    INSERT DATA { GRAPH <urn:sparql:tests:insert:data> { <#book1> <#price> 42 } }

  3. Query the graph data:

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

  4. View the SPARQL Query Definition via SPARQL Protocol URL;
  5. View the SPARQL Query Results via SPARQL Protocol URL

INSERT (Informative) Examples

Coping Triples Based on a Pattern Example

This example copies triples from one named graph to another named graph based on a pattern:

  1. Assume the following Raw Data Representation in Turtle:

    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" . <#book1> <http://purl.org/dc/elements/1.1/date> "1977-01-01T00:00:00-02:00"^^xsd:dateTime . <#book2> <#price> 42 . <#book2> <http://purl.org/dc/elements/1.1/title> "David Copperfield" . <#book2> <http://purl.org/dc/elements/1.1/creator> "Edmund Wells" . <#book2> <http://purl.org/dc/elements/1.1/date> "1948-01-01T00:00:00-02:00"^^xsd:dateTime . <#book3> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.1 Tutorial" .

  2. Load the sample data into graph <urn:sparql:tests:insert:informative>;

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> INSERT INTO <urn:sparql:tests:insert:informative> { <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" . <#book1> <http://purl.org/dc/elements/1.1/date> "1977-01-01T00:00:00-02:00"^^xsd:dateTime . <#book2> <#price> 42 . <#book2> <http://purl.org/dc/elements/1.1/title> "David Copperfield" . <#book2> <http://purl.org/dc/elements/1.1/creator> "Edmund Wells" . <#book2> <http://purl.org/dc/elements/1.1/date> "1948-01-01T00:00:00-02:00"^^xsd:dateTime . }

  3. Assume another Raw Data Representation in Turtle:

    <#book4> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.0 Tutorial" .

  4. Load the sample data into graph <urn:sparql:tests:insert:informative2>;

    INSERT INTO <urn:sparql:tests:insert:informative2> { <#book4> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.0 Tutorial" . }

  5. Copy triples from the <urn:sparql:tests:insert:informative2> named graph to the <urn:sparql:tests:insert:informative> named graph based on a pattern:

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> INSERT { GRAPH <urn:sparql:tests:insert:informative2> { ?book ?p ?v } } WHERE { GRAPH <urn:sparql:tests:insert:informative> { ?book <http://purl.org/dc/elements/1.1/date> ?date . FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime ) ?book ?p ?v } }

  6. Query the <urn:sparql:tests:insert:informative2> graph data:

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

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

Coping Triples Based on a Name and Mail Example

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:

  1. Assume the following Raw Data Representation in Turtle:

    _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . _:a <http://xmlns.com/foaf/0.1/name> "Alice" . _:a <http://xmlns.com/foaf/0.1/mbox> <mailto:alice@example.com> . _:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . _:b <http://xmlns.com/foaf/0.1/name> "Bob" .

  2. Load the sample data into graph <urn:sparql:tests:insert:informative3>;

    INSERT INTO <urn:sparql:tests:insert:informative3> { _:a <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . _:a <http://xmlns.com/foaf/0.1/name> "Alice" . _:a <http://xmlns.com/foaf/0.1/mbox> <mailto:alice@example.com> . _:b <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> . _:b <http://xmlns.com/foaf/0.1/name> "Bob" . }

  3. Assume in another named graph <urn:sparql:tests:insert:informative4> there are no triples inserted.
  4. Copy triples of name and email from <urn:sparql:tests:insert:informative3> to <urn:sparql:tests:insert:informative4> :

    INSERT { GRAPH <urn:sparql:tests:insert:informative4> { ?person <http://xmlns.com/foaf/0.1/name> ?name . ?person <http://xmlns.com/foaf/0.1/mbox> ?email } } WHERE { GRAPH <urn:sparql:tests:insert:informative3> { ?person <http://xmlns.com/foaf/0.1/name> ?name . OPTIONAL { ?person <http://xmlns.com/foaf/0.1/mbox> ?email } } }

  5. Query the <urn:sparql:tests:insert:informative4> graph data:

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

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

Copy Triples and Delete Physical Objects Example

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:

  1. Assume the following Raw Data Representation in Turtle:

    <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" . <#book1> <http://purl.org/dc/elements/1.1/date> "1996-01-01T00:00:00-02:00"^^xsd:dateTime . <#book1> a <http://purl.org/dc/dcmitype/PhysicalObject> . <#book3> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.1 Tutorial" .

  2. Load the sample data into graph <urn:sparql:tests:insert:informative5>;

    INSERT INTO <urn:sparql:tests:insert:informative5> { <#book1> <http://purl.org/dc/elements/1.1/title> "Fundamentals of Compiler Design" . <#book1> <http://purl.org/dc/elements/1.1/date> "1996-01-01T00:00:00-02:00"^^xsd:dateTime . <#book1> a <http://purl.org/dc/dcmitype/PhysicalObject> . <#book3> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.1 Tutorial" . }

  3. Assume another Raw Data Representation in Turtle:

    <#book4> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.0 Tutorial" .

  4. Load the sample data into graph <urn:sparql:tests:insert:informative6>;

    INSERT INTO <urn:sparql:tests:insert:informative6> { <#book4> <http://purl.org/dc/elements/1.1/title> "SPARQL 1.0 Tutorial" . }

  5. Copy triples from <urn:sparql:tests:insert:informative5> to <urn:sparql:tests:insert:informative6>

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> INSERT { GRAPH <urn:sparql:tests:insert:informative6> { ?book ?p ?v } } WHERE { GRAPH <urn:sparql:tests:insert:informative5> { ?book <http://purl.org/dc/elements/1.1/date> ?date . FILTER ( ?date < "2000-01-01T00:00:00-02:00"^^xsd:dateTime ) ?book ?p ?v } }

  6. Delete triples about all the copied objects that are classified as Physical Objects:

    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> WITH <urn:sparql:tests:insert:informative5> DELETE { ?book ?p ?v } WHERE { ?book <http://purl.org/dc/elements/1.1/date> ?date ; a <http://purl.org/dc/dcmitype/PhysicalObject> . FILTER ( ?date < "2000-01-01T00:00:00-02:00"^^xsd:dateTime ) ?book ?p ?v . }

  7. Query the <urn:sparql:tests:insert:informative5> graph data:

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

  8. View the SPARQL Query Definition via SPARQL Protocol URL;
  9. View the SPARQL Query Results via SPARQL Protocol URL
  10. Query the <urn:sparql:tests:insert:informative6> graph data:

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

  11. View the SPARQL Query Definition via SPARQL Protocol URL;
  12. View the SPARQL Query Results via SPARQL Protocol URL

Related