Before you begin
Before reading this page, do the following:- Create a CockroachDB Standard cluster or start a local cluster.
- Review the database schema objects.
- Create a database.
- Create a user-defined schema.
- Create a table.
- Add secondary indexes.
Alter database schema objects
To change an existing object in a database schema, use anALTER statement.
ALTER statements generally take the following form:
For examples, see below.
CockroachDB supports the following
ALTER statements:
ALTER DATABASEALTER SCHEMAALTER TABLEALTER INDEXALTER VIEWALTER SEQUENCEALTER TYPEALTER USER/ROLE
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
.sqlfile; for Liquibase, a separate changeset). -
For
ALTER TABLEstatements, combine multiple subcommands in a singleALTER TABLEstatement, where possible. - Use a database schema migration tool or the CockroachDB SQL client instead of a client library to execute database schema changes.
- Review the limitations of online schema changes. CockroachDB doesn’t guarantee the atomicity of schema changes within transactions with multiple statements. Cockroach Labs recommends that you perform schema changes outside explicit transactions. When a database schema management tool manages transactions on your behalf, include one schema change operation per transaction.
Altering objects examples
Suppose you want to make some changes to theusers table that you created in Create a Table. In specific, you want to do the following:
- Add a new
usernamecolumn. - Change the columns in the table’s primary key to
usernamecolumn andemail. - Move the table to the
abbey_schemauser-defined schema. - Change the owner of the table to
abbey.
ALTER TABLE statement has subcommands for all of these changes:
- To add a new column, use the
ADD COLUMNsubcommand. - To change the primary key columns of a table, use the
ALTER PRIMARY KEYsubcommand. - To move a table to a different schema, use the
SET SCHEMAsubcommand. - To change the owner of a table, use the
OWNER TOsubcommand.
.sql file for the changes that you plan to make to the table:
update_users_table.sql in a text editor, and add the ALTER TABLE statement for adding the username column:
ALTER TABLE statement, add another ALTER TABLE statement for changing the primary key columns to username and email:
NOT NULL constraint. 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 ALTER COLUMN subcommand to set the NOT NULL constraint on the email column:
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 authorization best practices, 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:
update_users_table.sql file as max, run the following command:
update_users_owner.sql file as root, run the following command:
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 aDROP 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 ALTER TABLE statement.Drop best practices
- Check the contents and dependencies of the object that you want to drop before using the
CASCADEoption.CASCADEdrops 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 onfirst_name and last_name from the users table.
users_first_name_last_name_key is a UNIQUE index, which means that it implies a dependent, UNIQUE constraint. To drop indexes with dependencies, 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].
