# SQLAlchemy 2.0 Documentation

# SQLAlchemy 2.0 Documentation

# Schema Definition Language

This section references SQLAlchemy **schema metadata**, a comprehensive system of describing and inspecting
database schemas.

The core of SQLAlchemy’s query and object mapping operations are supported by
*database metadata*, which is comprised of Python objects that describe tables
and other schema-level objects. These objects are at the core of three major
types of operations - issuing CREATE and DROP statements (known as *DDL*),
constructing SQL queries, and expressing information about structures that
already exist within the database.

Database metadata can be expressed by explicitly naming the various components
and their properties, using constructs such as
[Table](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Table), [Column](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column),
[ForeignKey](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.ForeignKey) and
[Sequence](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.Sequence), all of which are imported from the
`sqlalchemy.schema` package. It can also be generated by SQLAlchemy using a
process called *reflection*, which means you start with a single object such
as [Table](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Table), assign it a name, and then instruct
SQLAlchemy to load all the additional information related to that name from a
particular engine source.

A key feature of SQLAlchemy’s database metadata constructs is that they are
designed to be used in a *declarative* style which closely resembles that of
real DDL. They are therefore most intuitive to those who have some background
in creating real schema generation scripts.

- [Describing Databases with MetaData](https://docs.sqlalchemy.org/en/20/core/metadata.html)
  - [Accessing Tables and Columns](https://docs.sqlalchemy.org/en/20/core/metadata.html#accessing-tables-and-columns)
  - [Creating and Dropping Database Tables](https://docs.sqlalchemy.org/en/20/core/metadata.html#creating-and-dropping-database-tables)
  - [Altering Database Objects through Migrations](https://docs.sqlalchemy.org/en/20/core/metadata.html#altering-database-objects-through-migrations)
  - [Specifying the Schema Name](https://docs.sqlalchemy.org/en/20/core/metadata.html#specifying-the-schema-name)
    - [Specifying a Default Schema Name with MetaData](https://docs.sqlalchemy.org/en/20/core/metadata.html#specifying-a-default-schema-name-with-metadata)
    - [Applying Dynamic Schema Naming Conventions](https://docs.sqlalchemy.org/en/20/core/metadata.html#applying-dynamic-schema-naming-conventions)
    - [Setting a Default Schema for New Connections](https://docs.sqlalchemy.org/en/20/core/metadata.html#setting-a-default-schema-for-new-connections)
    - [Schemas and Reflection](https://docs.sqlalchemy.org/en/20/core/metadata.html#schemas-and-reflection)
  - [Backend-Specific Options](https://docs.sqlalchemy.org/en/20/core/metadata.html#backend-specific-options)
  - [Column, Table, MetaData API](https://docs.sqlalchemy.org/en/20/core/metadata.html#column-table-metadata-api)
    - [Column](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column)
    - [MetaData](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.MetaData)
    - [SchemaConst](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.SchemaConst)
    - [SchemaItem](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.SchemaItem)
    - [insert_sentinel()](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.insert_sentinel)
    - [Table](https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Table)
- [Reflecting Database Objects](https://docs.sqlalchemy.org/en/20/core/reflection.html)
  - [Overriding Reflected Columns](https://docs.sqlalchemy.org/en/20/core/reflection.html#overriding-reflected-columns)
  - [Reflecting Views](https://docs.sqlalchemy.org/en/20/core/reflection.html#reflecting-views)
  - [Reflecting All Tables at Once](https://docs.sqlalchemy.org/en/20/core/reflection.html#reflecting-all-tables-at-once)
  - [Reflecting Tables from Other Schemas](https://docs.sqlalchemy.org/en/20/core/reflection.html#reflecting-tables-from-other-schemas)
    - [Interaction of Schema-qualified Reflection with the Default Schema](https://docs.sqlalchemy.org/en/20/core/reflection.html#interaction-of-schema-qualified-reflection-with-the-default-schema)
  - [Fine Grained Reflection with Inspector](https://docs.sqlalchemy.org/en/20/core/reflection.html#fine-grained-reflection-with-inspector)
    - [Inspector](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.reflection.Inspector)
    - [ReflectedColumn](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedColumn)
    - [ReflectedComputed](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedComputed)
    - [ReflectedCheckConstraint](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedCheckConstraint)
    - [ReflectedForeignKeyConstraint](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedForeignKeyConstraint)
    - [ReflectedIdentity](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedIdentity)
    - [ReflectedIndex](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedIndex)
    - [ReflectedPrimaryKeyConstraint](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedPrimaryKeyConstraint)
    - [ReflectedUniqueConstraint](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedUniqueConstraint)
    - [ReflectedTableComment](https://docs.sqlalchemy.org/en/20/core/reflection.html#sqlalchemy.engine.interfaces.ReflectedTableComment)
  - [Reflecting with Database-Agnostic Types](https://docs.sqlalchemy.org/en/20/core/reflection.html#reflecting-with-database-agnostic-types)
  - [Limitations of Reflection](https://docs.sqlalchemy.org/en/20/core/reflection.html#limitations-of-reflection)
- [Column INSERT/UPDATE Defaults](https://docs.sqlalchemy.org/en/20/core/defaults.html)
  - [Scalar Defaults](https://docs.sqlalchemy.org/en/20/core/defaults.html#scalar-defaults)
  - [Python-Executed Functions](https://docs.sqlalchemy.org/en/20/core/defaults.html#python-executed-functions)
    - [Context-Sensitive Default Functions](https://docs.sqlalchemy.org/en/20/core/defaults.html#context-sensitive-default-functions)
  - [Client-Invoked SQL Expressions](https://docs.sqlalchemy.org/en/20/core/defaults.html#client-invoked-sql-expressions)
  - [Server-invoked DDL-Explicit Default Expressions](https://docs.sqlalchemy.org/en/20/core/defaults.html#server-invoked-ddl-explicit-default-expressions)
  - [Marking Implicitly Generated Values, timestamps, and Triggered Columns](https://docs.sqlalchemy.org/en/20/core/defaults.html#marking-implicitly-generated-values-timestamps-and-triggered-columns)
  - [Defining Sequences](https://docs.sqlalchemy.org/en/20/core/defaults.html#defining-sequences)
    - [Associating a Sequence on a SERIAL column](https://docs.sqlalchemy.org/en/20/core/defaults.html#associating-a-sequence-on-a-serial-column)
    - [Executing a Sequence Standalone](https://docs.sqlalchemy.org/en/20/core/defaults.html#executing-a-sequence-standalone)
    - [Associating a Sequence with the MetaData](https://docs.sqlalchemy.org/en/20/core/defaults.html#associating-a-sequence-with-the-metadata)
    - [Associating a Sequence as the Server Side Default](https://docs.sqlalchemy.org/en/20/core/defaults.html#associating-a-sequence-as-the-server-side-default)
  - [Computed Columns (GENERATED ALWAYS AS)](https://docs.sqlalchemy.org/en/20/core/defaults.html#computed-columns-generated-always-as)
  - [Identity Columns (GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY)](https://docs.sqlalchemy.org/en/20/core/defaults.html#identity-ddl)
  - [Default Objects API](https://docs.sqlalchemy.org/en/20/core/defaults.html#default-objects-api)
    - [Computed](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.Computed)
    - [ColumnDefault](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.ColumnDefault)
    - [DefaultClause](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.DefaultClause)
    - [DefaultGenerator](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.DefaultGenerator)
    - [FetchedValue](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.FetchedValue)
    - [Sequence](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.Sequence)
    - [Identity](https://docs.sqlalchemy.org/en/20/core/defaults.html#sqlalchemy.schema.Identity)
- [Defining Constraints and Indexes](https://docs.sqlalchemy.org/en/20/core/constraints.html)
  - [Defining Foreign Keys](https://docs.sqlalchemy.org/en/20/core/constraints.html#defining-foreign-keys)
    - [Creating/Dropping Foreign Key Constraints via ALTER](https://docs.sqlalchemy.org/en/20/core/constraints.html#creating-dropping-foreign-key-constraints-via-alter)
    - [ON UPDATE and ON DELETE](https://docs.sqlalchemy.org/en/20/core/constraints.html#on-update-and-on-delete)
  - [UNIQUE Constraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#unique-constraint)
  - [CHECK Constraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#check-constraint)
  - [PRIMARY KEY Constraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#primary-key-constraint)
  - [Setting up Constraints when using the Declarative ORM Extension](https://docs.sqlalchemy.org/en/20/core/constraints.html#setting-up-constraints-when-using-the-declarative-orm-extension)
  - [Configuring Constraint Naming Conventions](https://docs.sqlalchemy.org/en/20/core/constraints.html#configuring-constraint-naming-conventions)
    - [Configuring a Naming Convention for a MetaData Collection](https://docs.sqlalchemy.org/en/20/core/constraints.html#configuring-a-naming-convention-for-a-metadata-collection)
    - [The Default Naming Convention](https://docs.sqlalchemy.org/en/20/core/constraints.html#the-default-naming-convention)
    - [Truncation of Long Names](https://docs.sqlalchemy.org/en/20/core/constraints.html#truncation-of-long-names)
    - [Creating Custom Tokens for Naming Conventions](https://docs.sqlalchemy.org/en/20/core/constraints.html#creating-custom-tokens-for-naming-conventions)
    - [Naming CHECK Constraints](https://docs.sqlalchemy.org/en/20/core/constraints.html#naming-check-constraints)
    - [Configuring Naming for Boolean, Enum, and other schema types](https://docs.sqlalchemy.org/en/20/core/constraints.html#configuring-naming-for-boolean-enum-and-other-schema-types)
    - [Using Naming Conventions with ORM Declarative Mixins](https://docs.sqlalchemy.org/en/20/core/constraints.html#using-naming-conventions-with-orm-declarative-mixins)
  - [Constraints API](https://docs.sqlalchemy.org/en/20/core/constraints.html#constraints-api)
    - [Constraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.Constraint)
    - [ColumnCollectionMixin](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.ColumnCollectionMixin)
    - [ColumnCollectionConstraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.ColumnCollectionConstraint)
    - [CheckConstraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.CheckConstraint)
    - [ForeignKey](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.ForeignKey)
    - [ForeignKeyConstraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.ForeignKeyConstraint)
    - [HasConditionalDDL](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.HasConditionalDDL)
    - [PrimaryKeyConstraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.PrimaryKeyConstraint)
    - [UniqueConstraint](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.UniqueConstraint)
    - [conv()](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.conv)
  - [Indexes](https://docs.sqlalchemy.org/en/20/core/constraints.html#indexes)
    - [Functional Indexes](https://docs.sqlalchemy.org/en/20/core/constraints.html#functional-indexes)
  - [Index API](https://docs.sqlalchemy.org/en/20/core/constraints.html#index-api)
    - [Index](https://docs.sqlalchemy.org/en/20/core/constraints.html#sqlalchemy.schema.Index)
- [Customizing DDL](https://docs.sqlalchemy.org/en/20/core/ddl.html)
  - [Custom DDL](https://docs.sqlalchemy.org/en/20/core/ddl.html#custom-ddl)
  - [Controlling DDL Sequences](https://docs.sqlalchemy.org/en/20/core/ddl.html#controlling-ddl-sequences)
  - [Using the built-in DDLElement Classes](https://docs.sqlalchemy.org/en/20/core/ddl.html#using-the-built-in-ddlelement-classes)
  - [Controlling DDL Generation of Constraints and Indexes](https://docs.sqlalchemy.org/en/20/core/ddl.html#controlling-ddl-generation-of-constraints-and-indexes)
  - [DDL Expression Constructs API](https://docs.sqlalchemy.org/en/20/core/ddl.html#ddl-expression-constructs-api)
    - [sort_tables()](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.sort_tables)
    - [sort_tables_and_constraints()](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.sort_tables_and_constraints)
    - [BaseDDLElement](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.BaseDDLElement)
    - [ExecutableDDLElement](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.ExecutableDDLElement)
    - [DDL](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DDL)
    - [_CreateDropBase](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema._CreateDropBase)
    - [CreateTable](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateTable)
    - [DropTable](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DropTable)
    - [CreateColumn](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateColumn)
    - [CreateSequence](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateSequence)
    - [DropSequence](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DropSequence)
    - [CreateIndex](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateIndex)
    - [DropIndex](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DropIndex)
    - [AddConstraint](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.AddConstraint)
    - [DropConstraint](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DropConstraint)
    - [CreateSchema](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateSchema)
    - [DropSchema](https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.DropSchema)
