UPDATE
Modifying the graph with INSERT, DELETE, and conditional updates.
SPARQL UPDATE modifies the graph by inserting and deleting triples. In Poliglot, UPDATE is primarily used in reconciliation scripts that retract stale statements when new data arrives.
The examples on this page assume the following prefixes:
PREFIX tasks: <https://example.org/spec/tasks#>INSERT DATA
Add triples directly:
PREFIX tasks: <https://example.org/spec/tasks#>
INSERT DATA {
tasks:123 tasks:title "Review Q3" .
tasks:123 tasks:status tasks:Open .
}DELETE DATA
Remove specific triples:
PREFIX tasks: <https://example.org/spec/tasks#>
DELETE DATA {
tasks:123 tasks:status tasks:Open .
}The triples must match exactly. If they don't exist, nothing happens.
DELETE/INSERT WHERE
Conditionally delete and insert in one operation:
PREFIX tasks: <https://example.org/spec/tasks#>
DELETE {
?task tasks:status ?oldStatus .
}
INSERT {
?task tasks:status tasks:InProgress .
}
WHERE {
?task a tasks:Task ;
tasks:title "Review Q3" ;
tasks:status ?oldStatus .
}The WHERE clause finds matching data. DELETE removes the matched triples. INSERT adds new triples. Both use variables bound by the WHERE clause.
DELETE WHERE (Shorthand)
When the DELETE pattern matches the WHERE pattern exactly:
PREFIX tasks: <https://example.org/spec/tasks#>
DELETE WHERE {
?task a tasks:Task ;
tasks:status tasks:Completed .
}Removes all triples matching the pattern.
Graph-Scoped Updates
Target a specific named graph:
PREFIX tasks: <https://example.org/spec/tasks#>
INSERT {
GRAPH ?g {
?summary tasks:totalOpen ?count .
}
}
WHERE {
SELECT (COUNT(?task) AS ?count) WHERE {
?task a tasks:Task ;
tasks:status tasks:Open .
}
}Reconciliation Pattern
The most common UPDATE pattern in Poliglot is the reconciliation script. It retracts statements from the default graph that aren't present in a new dataset (?changeset):
PREFIX tasks: <https://example.org/spec/tasks#>
DELETE { ?s ?p ?o }
WHERE {
?s a tasks:Task ;
?p ?o .
FILTER(?p IN (tasks:title, tasks:status, tasks:assignee))
FILTER NOT EXISTS {
GRAPH ?changeset { ?s ?p ?o }
}
}See Reconciliation for the full mechanics.
See Also
- Basics: reading data with SELECT
- CONSTRUCT and DESCRIBE: building subgraphs
- Reconciliation: how UPDATE is used to retract stale observations