Alter

The alter method loads up an existing table in order to make modifications. These modifications may include adding, renaming, or dropping columns and constraints.

To begin altering an existing table, call the alter method off of the SchemaBuilder. This method takes a callback as the second parameter that is passed a Blueprint object, much like the create method.

Calling multiple methods inside a single alter callback creates multiple SQL statements to be executed. qb takes care of this execution for you by default.

The following methods off of Blueprint let you modify the table inside the callback:

addColumn

Add a new column to an existing table. Takes a Column instance as the only argument.

Any instance of Column is valid like those returned by the column methods (integer, string, etc.) as well as the column modifier methods (unsigned, nullable, etc.).

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.addColumn( table.boolean( "is_active" ) );
} );

SQL (MySQL)

ALTER TABLE `users` ADD `is_active` TINYINT(1) NOT NULL

raw

An escape hatch to directly insert any sql in to the statement.

Example:

SchemaBuilder

schema.alter( "registrars", function ( table ) {
    table.addColumn(
        table.raw( "HasDNSSecAPI bit NOT NULL CONSTRAINT DF_registrars_HasDNSSecAPI DEFAULT (0)" )
    );
} );

SQL (MySQL)

ALTER TABLE `registrars`
ADD HasDNSSecAPI bit NOT NULL
CONSTRAINT DF_registrars_HasDNSSecAPI DEFAULT (0)

dropColumn

Drop a column on an existing table.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.dropColumn( "username" );
} );

SQL (MySQL)

ALTER TABLE `users` DROP COLUMN `username`

modifyColumn

Modify an existing column on a table.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.modifyColumn( "name", table.string( "username" ) );
} );

SQL (MySQL)

ALTER TABLE `users` CHANGE `name` `username` VARCHAR(255) NOT NULL

renameColumn

Rename a column on a table. A full Column instance is required as the second argument for Grammars that need to redeclare the column definition when renaming.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.renameColumn( "name", table.string( "username" ) );
} );

SQL (MySQL)

ALTER TABLE `users` CHANGE `name` `username` VARCHAR(255) NOT NULL

addConstraint

Add an index or key to an existing table. Any TableIndex instance is valid, like those created by the index methods (unique, index, primaryKey, etc.).

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.addConstraint( table.unique( "username" ) );
} );

SQL (MySQL)

ALTER TABLE `users` ADD CONSTRAINT `unq_users_username` UNIQUE (`username`)

dropConstraint

Drop an existing table constraint.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.dropConstraint( "unq_users_full_name" );
    table.dropConstraint( table.unique( "username" ) );
} );

SQL (MySQL)

ALTER TABLE `users` DROP INDEX `unq_users_full_name`
ALTER TABLE `users` DROP INDEX `unq_users_username`

dropIndex

Drop an existing index.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.dropIndex( "idx_username" );
    table.dropIndex( table.index( "username" ) );
} );

SQL (MySQL)

ALTER TABLE `users` DROP INDEX `idx_username`
ALTER TABLE `users` DROP INDEX `idx_users_username`

SQL (SQL Server)

DROP INDEX [users].[idx_username]
DROP INDEX [users].[idx_users_username]

renameConstraint

Rename an existing table constraint.

Example:

SchemaBuilder

schema.alter( "users", function( table ) {
    table.renameConstraint( "unq_users_first_name_last_name", "unq_users_full_name" );
} );

SQL (MySQL)

ALTER TABLE `users` RENAME INDEX `unq_users_first_name_last_name` TO `unq_users_full_name`

renameTable

Rename an existing table.

Example:

SchemaBuilder

schema.renameTable( "workers", "employees" );

SQL (MySQL)

RENAME TABLE `workers` TO `employees`

rename

An alias for renameTable.

Example:

SchemaBuilder

schema.rename( "workers", "employees" );

SQL (MySQL)

RENAME TABLE `workers` TO `employees`