Skip to main content
This page provides an overview on changing and removing the objects in a database schema, with some simple examples based on Cockroach Labs’s fictional vehicle-sharing company, .

Before you begin

Before reading this page, do the following:
  • or .
  • .
  • .
  • .
  • .
  • .

Alter database schema objects

To change an existing object in a database schema, use an ALTER statement. ALTER statements generally take the following form:
For examples, see below. CockroachDB supports the following ALTER statements:

Best practices for altering objects

  • After you initialize a database schema, make any additional database schema changes in a separate set of changes (e.g., for the Cockroach SQL client, a separate .sql file; for Liquibase, a separate changeset).
  • For ALTER TABLE statements, combine multiple subcommands in a single ALTER TABLE statement, where possible.
  • Use a or the instead of a to execute .
  • Review the . CockroachDB . Cockroach Labs recommends that you perform schema changes outside explicit transactions. When a database manages transactions on your behalf, include one schema change operation per transaction.

Altering objects examples

Suppose you want to make some changes to the users table that you created in . In specific, you want to do the following:
  • Add a new username column.
  • Change the columns in the table’s primary key to username column and email.
  • Move the table to the abbey_schema user-defined schema.
  • Change the owner of the table to abbey.
The ALTER TABLE statement has subcommands for all of these changes:
  • To add a new column, use the .
  • To change the primary key columns of a table, use the .
  • To move a table to a different schema, use the subcommand.
  • To change the owner of a table, use the subcommand.
Create a new .sql file for the changes that you plan to make to the table:
Open update_users_table.sql in a text editor, and add the ALTER TABLE statement for adding the username column:
Under that first ALTER TABLE statement, add another ALTER TABLE statement for changing the primary key columns to username and email:
In order to add a column to an existing table’s primary key index, the column must have an existing . Neither the username nor the email columns have NOT NULL constraints. Add a NOT NULL constraint to the ADD COLUMN subcommand for username. In the same ALTER TABLE statement, add an to set the NOT NULL constraint on the email column:
The file should now look something like this:
The remaining changes that you want to make will require ALTER TABLE statements with the SET SCHEMA and OWNER TO subcommands. An ALTER TABLE ... SET SCHEMA statement will change the contents of two schemas, and an ALTER TABLE ... OWNER TO statement will change the privileges of two users. To follow , you should execute any statements that change databases, user-defined schemas, or user privileges as a member of the admin role (e.g., as root). Create a new .sql file for the remaining ALTER TABLE statements, to be executed by root:
Add the following statements to the file:
To execute the statements in the update_users_table.sql file as max, run the following command:
To execute the statements in the update_users_owner.sql file as root, run the following command:
The users table should now have a new column, a different primary key, a different schema, and a different owner. You can verify with some SHOW statements:

Drop database schema objects

To drop an object from a database schema, use a DROP statement. DROP statements generally take the following form:
For examples, see below. CockroachDB supports the following DROP statements:
To drop columns and column constraints from a table, use the DROP COLUMN and DROP CONSTRAINT subcommands of the statement.

Drop best practices

  • Check the contents and dependencies of the object that you want to drop before using the CASCADE option. CASCADE drops all of the contents of an object, and should be used sparingly after a schema has been initialized.

Drop example

Suppose that you want to drop an index that isn’t being used very much. In particular, you want to drop the index on first_name and last_name from the users table.
Note that users_first_name_last_name_key is a UNIQUE index, which means that it implies a dependent, UNIQUE constraint. To , you can use the CASCADE keyword. Create a new file, and add the DROP statement:
After creation, the notation for referring to indexes in CockroachDB is [table_name]@[index_name].
To drop the index, execute the file:

What’s next?

You might also be interested in the following pages: