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 anALTER 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
.sqlfile; for Liquibase, a separate changeset). -
For
ALTER TABLEstatements, combine multiple subcommands in a singleALTER TABLEstatement, 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 theusers table that you created in . 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 .
- 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.
.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:
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:
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:
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 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 , 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].
