Create, Alter, Drop a Table

For the creation, modification, or deletion of a database object, such as a table or sequence, use the execute() method.

Create

Below, you’ll find an example of how to create a table:

createTable = "CREATE TABLE person (
            id BIGINT NOT NULL,
            name VARCHAR,
            lastName VARCHAR,
            birth_date DATE,
            passport VARCHAR,
            phone VARCHAR,
            PRIMARY KEY (id)
            )"
try:
    cursor = conn.cursor()
    cursor.execute(createTable)
    conn.commit()
except Exception as e:
    print("Error:", e)
finally:
    cursor.close()

Alter

This is how you modify the table previously created:

alterTable = "ALTER TABLE person (ADD PARTITION(id) FOR VALUES(100)"
try:
    cursor = conn.cursor()
    cursor.execute(alterTable)
    conn.commit()
except Exception as e:
    print("Error:", e)
finally:
    cursor.close()

Drop

Then, here’s the process for removing the table created:

dropTable = "DROP TABLE person"
try:
    cursor = conn.cursor()
    cursor.execute(alterTable)
    conn.commit()
except Exception as e:
    print("Error:", e)
finally:
    cursor.close()