Lesson 3: Reading data
|
This documentation is not current - it is for TypeDB 2.x, which is no longer supported. View the documentation for TypeDB 3.x: |
Fetching simple data
-
Fetch queries are used to retrieve data in JSON format. They comprise a
matchclause andfetchclause. Fetch queries are run using a data session and read transaction.match # match clause fetch # fetch clause -
Variables are declared with a
$prefix. -
An
isastatement is used to specify the type of a variable.$entity isa entity-type; -
Data objects can be either entities or relations.
-
Relations have roles. An entity that plays a role is a roleplayer. All relations must have at least one roleplayer.
($role-1: $a, $role-2: $b) isa relation-type; -
A
hasstatement is used to specify the value of an entity or relation’s attribute.$entity has attribute-type "attribute value";
Fetching polymorphic data
-
Type inference allows the database to infer the possible types of a variable without having to explicitly specify them all.
-
Inheritance polymorphism allows us to query data of multiple types through a common supertype.
-
Interface polymorphism allows us to query data of multiple types through a common implemented interface: either a role in a relation or ownership of an attribute.
-
Parametric polymorphism allows us to query data of multiple types through the structure of the data alone. Purely parametric queries can be run on any schema.
-
Different types of polymorphism can be combined to produce complex queries.
Fetching inferred data
-
Rule inference allows the database to infer new data based on existing data and rules defined as part of the schema.
-
Rule inference must be enabled for inferred data to be returned.
Fetching schema types
-
Types can be variablized in the same way as data instances, by using a variable in place of a type label in statements.
$entity isa $entity-type; -
An
isa!statement is used to specify the exact type of a data variable.$entity isa! exact-type; -
A
substatement is used to specify a supertype of a type.$type sub supertype; -
A
sub!statement is used to specify the direct supertype of a type.$type sub! direct-supertype; -
An
ownsstatement is used to specify an owner of an attribute type.$entity-type owns attribute-type; -
A
relatesstatement is used to specify a role of a relation type.relation-type relates $role; -
A
playsstatement is used to specify a roleplayer of a role.$entity-type plays $role;
Query validation
-
All queries are validated against the schema.
-
Running a query that does not conform to the schema will cause an exception to be thrown.