Creating and Dropping Tables

Creating Table:

So far we have created two Tables with a set of Columns and constraints. The next thing is we have to emit DDL to the SQLite database (in this case) so that we can query with the tables. This can be done as shown below:

Python3




from sqlalchemy import create_engine
 
# creating an engine object
engine = create_engine("sqlite+pysqlite:///:memory:",
                       echo=True, future=True)
# emitting DDL
metadata_object.create_all(engine)


Output:

Dropping Table

The drop_all() method is used to drop all the tables in the metadata object.

Python




from sqlalchemy import create_engine
 
# creating an engine object
engine = create_engine("sqlite+pysqlite:///:memory:",
                       echo=True, future=True)
 
# emitting DDL
metadata_object.drop_all(engine)


Output:

Describing Databases with MetaData – SQLAlchemy

In this article, we are going to see how to describe Databases with MetaData using SQLAlchemy in Python.

Database Metadata describes the structure of the database in terms of Python data structures. The database usually consists of Tables and Columns. The Database Metadata serves us in generating SQL queries and Object Relational Mapping. It helps us in generating a Schema. The most fundamental objects of Database MetaData are MetaData, Table, and Column.

Similar Reads

Describing Databases with MetaData: SQLAlchemy Core

Setting up MetaData with Table objects:...

Accessing Tables and Columns

...

Accessing tables and keys using MetaData object

...

Declaring Constraints

The columns of a Table are usually stored in an associative array i.e., Table.c, and can be accessed using “c” as shown in the following examples....

Creating and Dropping Tables

...

Describing Databases with MetaData: SQLAlchemy ORM

...

Table Reflection

...