Officially out now: The TypeDB 3.0 Roadmap >>

Lesson 5: Defining schemas

In this lesson, we’ll learn how to define entity, relation, and attribute types using Define queries. We’ll start by defining individual types and their interfaces, namely ownership interfaces for attributes and role interfaces for relations. We’ll then see how we can define type hierarchies for entities, relations, and attributes. We’ll also see how we can apply different kinds of value constraints to attributes and define rules to infer new data. Finally, we’ll see how TypeDB validates changes to the schema to ensure it stays in a consistent state.

  • You should complete Lesson 4 before starting this lesson.

Lesson contents

Learn how to define new entity, relation, and attribute types.

Learn how to define type hierarchies and abstract types.

Learn how to constrain attribute values and ownerships.

Learn how to define rules to generate new data by rule inference.

Learn about how types and rules are validated against the schema.

Running examples

The sample database already has the complete bookstore schema defined and data inserted. If you would like to try running the Define queries featured in the examples and exercises in this section, then you should do so using another database. To create a new database, use the database manager studio dbs. After creation, make sure to switch over to it using the database selector. For more information on how to manage databases using TypeDB Studio, refer back to Lesson 2.2

To run a Define query, make sure to use a schema session and write transaction. After running a Define query, you should see any committed changes to the schema reflected in the type browser. The examples and exercises featured in this lesson are annotated with one of the following instructions to indicate how they should be run.

studio run Run

Run this query without committing the transaction afterward. Write transactions will remain open for further queries, and changes will not be persisted until a commit.

studio runstudio check Run and commit

Run this query and then commit the transaction, persisting the changes to the database.

studio fail Do not run

Do not run this query, which is shown for information only. To rollback an open transaction, close it without committing.

studio run Try running

Run this query to see the error generated.

For information on how to control sessions, transactions, and queries using TypeDB Studio, refer back to Lesson 2.2.

TypeQL keywords introduced

  • define

  • value

  • abstract

  • as

  • @key

  • @unique

  • regex

  • rule

  • when

  • then