Lesson 4: Writing data
|
This documentation is not current - it is for TypeDB 2.x, which is no longer supported. View the documentation for TypeDB 3.x: |
Inserting simple data
-
Insert queries are used to insert data. They comprise either an
insertclause only, or amatchclause and aninsertclause. Insert queries are run using a data session and write transaction.insert # insert clausematch # match clause insert # insert clause -
All attributes are multivalued, allowing an entity or relation to own any number of attributes of the same type, as long as they each have different values.
-
An Insert query without a
matchclause inserts data once. An Insert query with amatchclause inserts data multiple times, once for each match found.
Inserting polymorphic data
-
A polymorphic insert can be performed by using a polymorphic pattern in the
matchclause of an Insert query. -
When inserting a relation, the roles can be omitted if they can be inferred unambiguously.
Deleting data
-
Delete queries are used to delete data. They comprise a
matchclause and adeleteclause. Delete queries are run using a data session and write transaction.match # match clause delete # delete clause -
Delete queries delete data per statement.
-
A
hasstatement is used to delete an entity or relation’s attribute.$entity has $deleted-attribute; -
An
isastatement is used to delete an entity or relation.$deleted-entity isa entity-type; -
When deleting a roleplayer in a relation, the relation must also be deleted manually if required. A relation is only deleted automatically if all of its roleplayers are deleted.
Updating data
-
Update queries are used to update data. They comprise a
matchclause, adeleteclause, and aninsertclause. Update queries are run using a data session and write transaction.match # match clause delete # delete clause insert # insert clause
Data validation
-
All inserted data is validated against the schema.
-
Inserting data that does not conform to the schema will cause an exception to be thrown.
-
An exception will also be thrown if deleting or updating data leads to an invalid state.