# 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 **functions** to compute results. Finally, we’ll see how TypeDB validates changes to the schema to ensure it stays in a consistent state.

## [](#_recommended_prerequisites)Recommended prerequisites

*   You should complete [Lesson 4](../4-writing-data/index.md) before starting this lesson.
    

## [](#_lesson_contents)Lesson contents

[Lesson 5.1: Defining individual types](5.1-defining-individual-types/index.md)

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

[Lesson 5.2: Defining type hierarchies](5.2-defining-type-hierarchies/index.md)

Learn how to define type hierarchies and abstract types.

[Lesson 5.3: Defining data constraints](5.3-defining-constraints/index.md)

Learn how to constrain attribute values and ownerships.

[Lesson 5.4: Defining functions](5.4-defining-functions/index.md)

Learn how to define functions to capture modular logic and abstract away complex operations.

[Lesson 5.5: Schema validation](5.5-schema-validation/index.md)

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

## [](#_running_examples)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**. 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 the [TypeDB studio reference](../../tools/studio/index.md).

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.

:: **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. _→ Commit_:: **Run** this query and then **commit** the transaction, persisting the changes to the database. **_Do not run_**:: Do not run this query, which is shown for information only. To rollback an open transaction, **close** it without committing. **_Try running_**:: **Run** this query to see the error generated.

## [](#_typeql_keywords_introduced)TypeQL keywords introduced

*   `define`
    
*   `value`
    
*   `abstract`
    
*   `as`
    
*   `@key`
    
*   `@unique`
    
*   `regex`
    
*   `fun`
    
*   `when`
    
*   `then`
    

[Summary](../4-writing-data/summary/index.md) [Defining individual types](5.1-defining-individual-types/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/academy/modules/ROOT/pages/5-defining-schemas/index.adoc) Edit this page on GitHub.