# Lesson 5: Defining schemas

## [](#_defining_individual_types)[Defining individual types](../5.1-defining-individual-types/index.md)

*   **Define queries** are used to define new types, interfaces, and functions. They comprise a `define` clause only. Define queries are run **schema transaction**.
    
    ```typeql
    define
    # define clause
    ```
    
*   A `entity`, `relation`, or `attribute` kind statement is used to define a new type. You can use `sub` to attach new types to other types as a subtype.
    
    ```typeql
    entity-type sub entity;
    ```
    
*   Define queries cannot contain variables, only keywords and type labels.
    
*   A `relates` statement is used to define a new role for a relation type. All relation types must have at least one role defined.
    
    ```typeql
    relation-type relates role;
    ```
    
*   A `plays` statement is used to define a new role player for a role.
    
    ```typeql
    entity-type plays role;
    ```
    
*   A `value` statement is used to define the value type of an attribute type. All attribute types must have a value type defined.
    
    ```typeql
    attribute-type value boolean;
    ```
    
*   An `owns` statment is used to define a new owner of an attribute type.
    
    ```typeql
    entity-type owns attribute-type;
    ```
    

## [](#_defining_type_hierarchies)[Defining type hierarchies](../5.2-defining-type-hierarchies/index.md)

*   A **type hierarchy** can be defined by using an existing type using a `sub` statement.
    
    ```typeql
    entity-type sub supertype;
    ```
    
*   A subtype of an entity or relation type will **inherit** all of its owned attributes and played roles.
    
*   An `@abstract` annotation is used to define a type as **abstract**.
    
    ```typeql
    entity-type @abstract;
    ```
    
*   By default, a subtype of a relation type will inherit all of its roles. Inherited roles can be overridden using the `as` keyword. An overriding role is the subtype of the role it overrides.
    
    ```typeql
    relation-type relates role as overridden-role;
    ```
    

## [](#_defining_data_constraints)[Defining data constraints](../5.3-defining-constraints/index.md)

*   A `@card` annotation controls the connectivity of your dataset
    
*   A `@regex` annotation can be used to constrain string attribute type by regex
    
*   A `@values` annotation can be used to constrain any value type to set a possible values
    
*   A `@range` annotation can be used to constraint any numerical value type
    
    ```typeql
    attribute attribute-type @regex("^regex pattern$");
    ```
    
*   A `@key` annotation is used to define a **key constraint** on an attribute ownership, requiring that instances of the owner type own exactly one instance of the attribute type, and that it must be uniquely owned by the owner type.
    
    ```typeql
    entity-type owns attribute-type @key;
    ```
    
*   A `@unique` annotation is used to define a **unique constraint** on an attribute ownership, requiring that instances of the attribute type are uniquely owned by the owner type.
    
    ```typeql
    entity-type owns attribute-type @unique;
    ```
    

## [](#_defining_functions)[Defining functions](../5.4-defining-functions/index.md)

*   A function definition has three components: a **signature**, a **body**, and a **return**.
    
    ```typeql
    fun function-name($arg: arg-type) -> { return-type }:
      <body>
      return <return operation>;
    ```
    
*   The body can be any read-only query pipeline
    
*   The return operation must match the declared signature
    

## [](#_schema_validation)[Schema validation](../5.5-schema-validation/index.md)

*   Checks must be passed whenever changes to the schema are made to ensure internal consistency.
    
*   Making changes that fail a validation check will cause an exception to be thrown.
    

## [](#_further_learning)Further learning

[Lesson 6: Building applications](../../6-building-applications/index.md)

Learn how to build applications on TypeDB, covering database management, transaction control, and result stream processing.

[Lesson 7: Understanding query patterns](../../7-understanding-query-patterns/index.md)

Learn how to build query patterns utilising advanced elements of TypeQL syntax, and how queries are resolved by TypeDB.

[Reference: Defining schemas](../../../typeql-reference/schema/index.md)

Learn more about defining schemas for TypeDB, covering Define queries, Undefine queries, and schema editing.

[Schema validation](../5.5-schema-validation/index.md) [6\. Building applications](../../6-building-applications/index.md)

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