Skip to content
Home » Cloudflare D1 SQL Docs » Alter Table

Alter Table

The ALTER TABLE data definition statement in Cloudflare D1 SQL is used to modify an existing database table.

Using the ALTER TABLE statement you can:

  1. Rename a table
  2. Rename a column
  3. Add new column
  4. Remove existing column

Renaming a Table

The basic structure of the ALTER TABLE command for renaming a D1 database table is:

ALTER TABLE <table name> RENAME TO <new table name>

In the example below we will rename and existing table ‘customer‘ to a new table name ‘customers

-- Renaming the 'customer' table to 'customers'
ALTER TABLE customer RENAME TO customers;

Renaming a column

Below is how does the rename column syntax of ALTER TABLE look like:

ALTER TABLE <table name> RENAME COLUMN <column-name> TO <new column-name>

For example to rename column ‘id‘ to ‘customer_id‘, we can execute:

ALTER TABLE customer RENAME COLUMN id TO customer_id;

Adding new column

To add a new column to an existing D1 table execute:

ALTER TABLE <table name> ADD COLUMN <column definition>

e.g. to add a column called ‘email‘ to the customers table, we can execute:

ALTER TABLE customers ADD COLUMN email TEXT;

Removing existing column

The ALTER TABLE syntax to remove column from Cloudflare D1 table is:

ALTER TABLE <table name> DROP COLUMN <column name>

So, lets drop the previously added ‘email‘ column

ALTER TABLE customers DROP COLUMN email;

Summary

This is a short article that illustrates how to perform the ALTER TABLE SQL command in the D1 relational database of Cloudflare. Using this command we can rename an existing table, rename existing column, drop existing column and add brand new column to a table.