0Pricing
Kotlin Academy · Lesson

Defining Tables with the Table DSL

Define typed table schemas with columns, primary keys, and foreign keys.

Exposed Table Objects

In Exposed, database tables are represented as Kotlin singleton objects that extend Table (Query DSL) or IntIdTable/LongIdTable/UUIDTable (DAO layer). Column definitions are Kotlin properties.

A Basic Table Definition

Extend Table and define columns using the built-in column factory functions:

import org.jetbrains.exposed.sql.Table

object Users : Table("users") {
    val id    = long("id").autoIncrement()
    val name  = varchar("name", 100)
    val email = varchar("email", 255).uniqueIndex()
    val age   = integer("age").nullable()

    override val primaryKey = PrimaryKey(id)
}

All lessons in this course

  1. Exposed Setup: Database Connection and Transaction DSL
  2. Defining Tables with the Table DSL
  3. CRUD with the Query DSL: insert, select, update, delete
  4. Exposed DAO: Entity Classes and Relationships
← Back to Kotlin Academy