ALTER VSCHEMA statements let you modify the VSchema using SQL over a regular MySQL connection to your database branch, as an alternative to editing the VSchema JSON directly in the Clusters page or with the pscale CLI.
ALTER VSCHEMA only changes Vitess routing metadata.
It never creates, alters, or drops the underlying MySQL tables — you manage those with regular DDL.Statement overview
How keyspaces are resolved
EveryALTER VSCHEMA statement operates on exactly one keyspace.
Vitess determines which one as follows:
- If the table name is qualified (
keyspace.table), the qualifier wins. - Otherwise, the keyspace you are currently connected to (or selected with
USE) is used. - If neither is available, the statement fails with an error.
Keyspace, table, and column names that contain special characters (such as hyphens) or collide with reserved words (such as
binary) must be quoted with backticks, just like in regular MySQL statements:Managing tables
Add a table
- The keyspace is sharded. In a sharded keyspace, every table needs a primary Vindex, so use
ALTER VSCHEMA ... ADD VINDEXinstead — it creates the table entry for you. - The table is already present in the VSchema.
Drop a table
ADD TABLE, this works on both sharded and unsharded keyspaces.
The underlying MySQL table is not touched.
Managing sequences
Sequence tables provide auto-increment-style ID generation for sharded tables. The sequence table itself must live in an unsharded keyspace.Add a sequence
"type": "sequence").
vitess_sequence comment, and seeded with a single row:
- The keyspace is sharded.
- A table with that name is already present in the VSchema.
Drop a sequence
- The keyspace is sharded.
- The sequence is not present in the VSchema.
Managing auto-increment columns
Add an auto-increment column
INSERT does not provide a value for it — typically the primary key of a sharded table.
The sequence table is usually in a different (unsharded) keyspace, in which case it must be qualified.
- The table is not present in the keyspace’s VSchema. Add its primary Vindex first with
ALTER VSCHEMA ... ADD VINDEX. - The table already has an auto-increment column configured. A table can only have one; drop the existing one first.
Drop an auto-increment column
- The table is not present in the keyspace’s VSchema.
- The table has no auto-increment column configured.
Managing Vindexes
A Vindex definition lives at the keyspace level and consists of a name, a type, and optional parameters. Tables then reference a Vindex by name, binding it to one or more of their columns. The first Vindex on a table is its primary Vindex — the one that decides which shard each row lives on. You will usually useALTER VSCHEMA ... ADD VINDEX, which defines the Vindex and attaches it to a table in one statement.
CREATE VINDEX exists for defining a Vindex ahead of time, without attaching it to any table.
Create a Vindex
<vindex_type> is one of the predefined Vindex types, such as xxhash, hash, unicode_loose_xxhash, binary, numeric, consistent_lookup, or consistent_lookup_unique.
Drop a Vindex
- The Vindex does not exist in the keyspace.
- Any table still uses the Vindex. Detach it from every table first with
ALTER VSCHEMA ... DROP VINDEX.
Add a Vindex to a table
USING clause is optional and controls whether the Vindex definition is created along the way:
- With
USING: if no Vindex namedvindex_nameexists in the keyspace, it is created with the given type and parameters. If it already exists, the type, owner, and parameters must match the existing definition exactly. - Without
USING: the Vindex namedvindex_namemust already exist in the keyspace (created earlier viaCREATE VINDEXor a previousADD VINDEX ... USING).
- A Vindex with the same name is already attached to the table.
USINGis given but the existing Vindex definition has a different type, owner, or parameters.USINGis omitted and no Vindex with that name exists in the keyspace.
Drop a Vindex from a table
ALTER VSCHEMA DROP VINDEX to remove it entirely.
If this was the last Vindex on the table, the table entry is removed from the VSchema as well.
- The table is not present in the keyspace’s VSchema.
- No Vindex with that name is attached to the table.
Vindex parameters
TheWITH clause passes key-value parameters to the Vindex.
Which parameters are valid depends on the Vindex type — most functional Vindexes such as xxhash take none, while lookup Vindexes require several:
Only parameter values that contain special characters need backticks — such as the dot in a qualified
table reference or the comma in a multi-column from list. Plain values like owner=corder are left unquoted:
A complete example
This example shards acustomer keyspace while keeping reference data and sequence tables in an unsharded product keyspace.
Verifying your changes
ALTER VSCHEMA changes take effect immediately.
You can inspect the resulting VSchema over the same MySQL connection:
SHOW VSCHEMA TABLES and SHOW VSCHEMA VINDEXES (without ON) report on the keyspace your connection is currently using — there is no keyspace-qualifier form. Switch keyspaces with USE <keyspace> to inspect a different one.
