Schema 定义语言¶
Schema Definition Language
本节引用了SQLAlchemy 模式元数据,这是一个描述和检查数据库模式的综合系统。
SQLAlchemy的查询和对象映射操作的核心由 数据库元数据 支持,数据库元数据由描述表和其他模式级对象的Python对象组成。这些对象是三类主要操作的核心 - 发出CREATE和DROP语句(称为 DDL ),构造SQL查询,以及表达数据库中已经存在的结构的信息。
数据库元数据可以通过显式命名各种组件及其属性来表达,使用的构造如:class:~sqlalchemy.schema.Table, Column
, ForeignKey
和 Sequence
,所有这些都从 sqlalchemy.schema
包中导入。它也可以通过SQLAlchemy使用称为 反射 的过程生成,这意味着你从一个对象,如 Table
开始,给它分配一个名称,然后指示SQLAlchemy从特定的引擎源加载与该名称相关的所有其他信息。
SQLAlchemy数据库元数据构造的一个关键特性是它们被设计为以 声明式 风格使用,这种风格与实际的DDL非常相似。因此,对于那些在创建真实模式生成脚本方面有一定背景的人来说,它们是最直观的。
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
, Column
,
ForeignKey
and
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
, 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.
- 使用元数据描述数据库
- 反射数据库对象
- 列 INSERT/UPDATE 默认值
- 定义约束和索引
- 自定义 DDL