Introduction To Databases in Python: Creating Databases and Tables
Introduction To Databases in Python: Creating Databases and Tables
Creating
Databases
and
Tables
Introduction to Databases in Python
Creating Databases
● Varies by the database type
● Databases like PostgreSQL and MySQL have
command line tools to initialize the database
● With SQLite, the create_engine() statement will
create the database and file is they do not already exist
Introduction to Databases in Python
Building a Table
In [1]: from sqlalchemy import (Table, Column, String,
...: Integer, Decimal, Boolean)
In [3]: metadata.create_all(engine)
In [4]: engine.table_names()
Out[4]: [u'employees']
Introduction to Databases in Python
Creating Tables
● Still uses the Table object like we did for reflection
● Replaces the autoload keyword arguments with
Column objects
● Creates the tables in the actual database by using
the create_all() method on the MetaData
instance
● You need to use other tools to handle database
table updates, such as Alembic or raw SQL
Introduction to Databases in Python
In [2]: employees.constraints
Out[2]: {CheckConstraint(...
Column('name', String(length=255), table=<employees>,
nullable=False),
Column('salary', Float(), table=<employees>,
default=ColumnDefault(100.0)),
Column('active', Boolean(), table=<employees>,
default=ColumnDefault(True)) ...
UniqueConstraint(Column('name', String(length=255),
table=<employees>, nullable=False))}
INTRODUCTION TO DATABASES IN PYTHON
Let’s practice!
INTRODUCTION TO DATABASES IN PYTHON
Inserting Data
into a
Table
Introduction to Databases in Python
In [4]: print(result_proxy.rowcount)
Out[4]: 1
Introduction to Databases in Python
In [4]: print(result_proxy.rowcount)
Out[4]: 2
INTRODUCTION TO DATABASES IN PYTHON
Let’s practice!
INTRODUCTION TO DATABASES IN PYTHON
Updating
Data
in a
Table
Introduction to Databases in Python
In [6]: print(result_proxy.rowcount)
Out[6]: 1
Introduction to Databases in Python
In [5]: print(result_proxy.rowcount)
Out[5]: 3
Introduction to Databases in Python
Correlated Updates
In [1]: new_salary = select([employees.columns.salary])
In [7]: print(result_proxy.rowcount)
Out[7]: 3
Introduction to Databases in Python
Correlated Updates
● Uses a select() statement to find the value for the
column we are updating
● Commonly used to update records to a maximum value or
change a string to match an abbreviation from another table
INTRODUCTION TO DATABASES IN PYTHON
Let’s practice!
Introduction to Databases in Python
Deleting
Data
from a
Database
Introduction to Databases in Python
In [3]: connection.execute(stmt).scalar()
Out[3]: 3
In [6]: result_proxy.rowcount
Out[6]: 3
Introduction to Databases in Python
In [3]: result_proxy.rowcount
Out[3]: 1
Introduction to Databases in Python
Dropping a table
In [1]: extra_employees.drop(engine)
In [2]: print(extra_employees.exists(engine))
Out[2]: False
Introduction to Databases in Python
In [2]: engine.table_names()
Out[2]: []
INTRODUCTION TO DATABASES IN PYTHON
Let’s practice!