### Exposed 0.56.0 Help

# Table types

In Exposed, the `Table` class is the core abstraction for defining database tables. This class provides methods to define various column types, constraints, and other table-specific properties.

## Auto-incrementing `id` column tables

Apart from the core `Table` class, Exposed provides the base `IdTable` class and its subclasses through the DAO API.

The `IdTable` class extends `Table` and is designed to simplify the definition of tables that use a standard `id` column as the primary key. These tables can be declared without explicitly including the `id` column, as IDs of the appropriate type are automatically generated when creating new table rows.

`IdTable` and its subclasses are located in the org.jetbrains.exposed.dao.id package of the exposed-core module.

`IntIdTable`

  `Int` id column

`LongIdTable`

  `Long` id column

`UIntIdTable`

  `UInt` id column

`ULongIdTable`

  `ULong` id column

`UUIDTable`

  `UUID` id column

The following example represents a table with custom columns `sequel_id`, `name`, and `director`:

import org.jetbrains.exposed.dao.id.IntIdTable const val MAX_VARCHAR_LENGTH = 50 object StarWarsFilmsTable : IntIdTable() { val sequelId = integer("sequel_id").uniqueIndex() val name = varchar("name", MAX_VARCHAR_LENGTH) val director = varchar("director", MAX_VARCHAR_LENGTH) }

The `IntIdTable` class automatically generates an auto-incrementing integer `id` column, which serves as the primary key for the table. When the table is created, it corresponds to the following SQL query:

CREATE TABLE IF NOT EXISTS STARWARSFILMS (ID INT AUTO_INCREMENT PRIMARY KEY, SEQUEL_ID INT NOT NULL, "name" VARCHAR(50) NOT NULL, DIRECTOR VARCHAR(50) NOT NULL);

For more information on defining and configuring tables in Exposed, see Working with tables.

## Composite id table

To define multiple columns as part of the primary key and id, use `CompositeIdTable` and mark each composite column using `entityId()`. Each component column will be available for CRUD operations either individually (as for any standard column) or all together as part of the `id` column:

package org.example.tables import org.jetbrains.exposed.dao.id.CompositeIdTable enum class Genre { HORROR, DRAMA, THRILLER, SCI_FI } const val NAME_LENGTH = 50 object DirectorsTable : CompositeIdTable("directors") { val name = varchar("name", NAME_LENGTH).entityId() val guildId = uuid("guild_id").autoGenerate().entityId() val genre = enumeration<Genre>("genre") override val primaryKey = PrimaryKey(name, guildId) }
package org.example.entities import org.example.tables.DirectorsTable import org.jetbrains.exposed.dao.CompositeEntity import org.jetbrains.exposed.dao.CompositeEntityClass import org.jetbrains.exposed.dao.id.CompositeID import org.jetbrains.exposed.dao.id.EntityID class DirectorEntity(id: EntityID<CompositeID>) : CompositeEntity(id) { companion object : CompositeEntityClass<DirectorEntity>(DirectorsTable) var genre by DirectorsTable.genre }

If any of the key component columns have already been marked by `entityId()` in another table, they can still be identified using `addIdColumn()`. This might be useful for key columns that reference another `IdTable`:

package org.example.tables import org.jetbrains.exposed.dao.id.CompositeIdTable const val DIRECTOR_NAME_LENGTH = 50 object DirectorsWithGuildRefTable : CompositeIdTable() { val name = varchar("name", DIRECTOR_NAME_LENGTH).entityId() val guildId = reference("guild_id", GuildsTable) val genre = enumeration<Genre>("genre") init { addIdColumn(guildId) } override val primaryKey = PrimaryKey(name, guildId) }
package org.example.tables import org.jetbrains.exposed.dao.id.UUIDTable object GuildsTable : UUIDTable("guilds")

## Custom column type table

To define a custom column type as the primary key and id, use a typed `IdTable` directly and override the `id` column, as shown in the following example:

package org.example.tables import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.dao.id.IdTable import org.jetbrains.exposed.sql.Column const val MAX_ID_LENGTH = 32 const val MAX_DIRECTOR_NAME_LENGTH = 32 object DirectorsCustomTable : IdTable<String>() { override val id: Column<EntityID<String>> = varchar("id", MAX_ID_LENGTH).entityId() val name = varchar("name", MAX_DIRECTOR_NAME_LENGTH) override val primaryKey = PrimaryKey(id) }
package org.example.entities import org.example.tables.DirectorsCustomTable import org.jetbrains.exposed.dao.Entity import org.jetbrains.exposed.dao.EntityClass import org.jetbrains.exposed.dao.id.EntityID class DirectorCustomEntity(id: EntityID<String>) : Entity<String>(id) { companion object : EntityClass<String, DirectorCustomEntity>(DirectorsCustomTable) var name by DirectorsCustomTable.name }

In the definition of `DirectorsCustomTable`, the `id` field is of type `Column<EntityID<String>>`, which will hold `String` values with a length of up to 32 characters. Using the `override` keyword indicates that this `id` column is overriding the default `id` column provided by `IdTable`.

Once all columns are defined, the `id` column is explicitly set as the primary key for the table, using the `override` keyword once again.

Last modified: 30 October 2024