Skip to content

Day 35: Altering a table

Modifying SQL Tables: Adding a New Column

There might be instances where you've created a table, but later decide to modify it by adding a new column. This can be accomplished using the ALTER keyword in SQL.

For instance, let's say we want to add a new column named 'publisher' to our 'books' table. We can do this using the SQLite3 library in Python. We first connect to our bookstore database and get a cursor. Then, we execute the ALTER TABLE command, specifying the name of the table we want to alter. We use the ADD COLUMN keywords to add a new column, name the column 'publisher', and define the type of information it will hold, which in this case is text. As always, we commit the changes and close the connection to save our data.

Adding a New Column to a Table with SQL

Here's how you can add a new column to the 'books' table using SQL and Python:

import sqlite3

# Connect to the bookstore.db database
conn = sqlite3.connect("bookstore.db")

# Create a cursor to execute SQL commands
cursor = conn.cursor()

# Add a new column 'publisher' to the 'books' table
cursor.execute("""
ALTER TABLE books
ADD COLUMN publisher text
""")

# Commit the changes to the database
conn.commit()

# Close the connection to the database
conn.close()

In this Python script, we connect to the bookstore.db database and create a cursor to execute SQL commands. We then use the ALTER TABLE statement to add a new column 'publisher' to the 'books' table. Finally, we commit the changes and close the database connection.

By using the ALTER TABLE command in SQL, we can easily modify our tables to better suit our data needs.

Learn More

Want to learn more about SQL for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.