Managing Your Data
This topic provides information about common questions to ask when managing your data.
- How do I list all of the graphs in the database?
- How do I find all of the triples that reference a resource?
- How do I perform a cascaded delete to remove all triples associated with a resource?
- How do I delete a predicate and all of its values?
How do I list all of the graphs in the database?
The following query returns a list of all of the named graphs in AnzoGraph:
SELECT DISTINCT ?graph WHERE { GRAPH ?graph { ?s ?p ?o . } }
graph ------- tickit 1 rows
How do I find all of the triples that reference a resource?
A common task is to find all of the triples in a graph that refer to a particular resource. That resource might be a subject in one triple and an object in another. For example, the person2 resource in the sample tickit graph is referenced as the subject in some triples and the object in other triples. For example:
<person2> <friend> <person12595>
<person12595> <friend> <person2>
To find all of the triples that reference person2, the following example query returns all of the triples where person2 is the subject or object:
SELECT ?s ?p ?o FROM <tickit> WHERE { { BIND (<person2> AS ?s) ?s ?p ?o . } UNION { BIND (<person2> AS ?o) ?s ?p ?o . } }
s | p | o --------------+-----------+--------------- person29074 | friend | person2 person21364 | friend | person2 person23672 | friend | person2 person48422 | friend | person2 person16636 | friend | person2 person22468 | friend | person2 person48892 | friend | person2 person24553 | friend | person2 ... listing52091 | sellerid | person2 listing54017 | sellerid | person2 listing86046 | sellerid | person2 listing160217 | sellerid | person2 sales28392 | sellerid | person2 sales75347 | sellerid | person2 sales75348 | sellerid | person2 sales138679 | sellerid | person2 sales41367 | buyerid | person2 sales42360 | buyerid | person2 ... 98 rows
How do I perform a cascaded delete to remove all triples associated with a resource?
The example above demonstrates how to find all of the triples that reference a resource. This example shows how to delete the resource and all of the triples that refer to it. The query below deletes person2 and all associated triples from the tickit graph:
DELETE { GRAPH <tickit> { ?s ?p ?o . } } WHERE { GRAPH <tickit> { ?s ?p ?o . filter(?s=<person2> || ?o=<person2>) } }
To confirm that person2 and the related triples no longer exist, you can run the query from the first example:
SELECT ?s ?p ?o FROM <tickit> WHERE { { BIND (<person2> AS ?s) ?s ?p ?o . } UNION { BIND (<person2> AS ?o) ?s ?p ?o . } }
s | p | o --+---+--- 0 rows
How do I delete a predicate and all of its values?
You might need to remove a predicate and all of the associated objects from a graph. For example, the sample Tickit dataset includes person subjects with an ssn predicate whose objects are social security numbers for each person. The following query deletes the ssn predicate and the social security numbers from the tickit graph:
DELETE { GRAPH <tickit> { ?person <ssn> ?ssn . } } WHERE { GRAPH <tickit> { ?person <ssn> ?ssn . } }