> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to rename a column that is part of the sorting key or primary key

> A column used in ORDER BY, PRIMARY KEY or PARTITION BY cannot be renamed. Add an ALIAS column to query it under a new name without rewriting data, and keep using the primary index.

A column that is used in the `ORDER BY`, `PRIMARY KEY` or `PARTITION BY` expression of a `MergeTree` table cannot be renamed. If you only need to query it under a different name, add an [`ALIAS`](https://clickhouse.com/docs/reference/statements/create/table#alias) column that refers to it. This is a metadata change: no data is rewritten, the table is not recreated, and filters on the alias use the primary index in the same way as filters on the original column.

## The restriction

```sql theme={null}
CREATE TABLE app_logs
(
    svc String,
    ts DateTime('UTC'),
    message String
)
ENGINE = MergeTree
ORDER BY (svc, ts);

INSERT INTO app_logs
SELECT
    ['auth', 'billing', 'checkout', 'search'][number % 4 + 1],
    toDateTime('2026-01-01 00:00:00', 'UTC') + number,
    concat('message ', toString(number))
FROM numbers(100000);

ALTER TABLE app_logs RENAME COLUMN svc TO service_name;
```

```text theme={null}
Code: 524. DB::Exception: Trying to ALTER RENAME key svc column which is a part of key expression. (ALTER_OF_COLUMN_IS_FORBIDDEN)
```

The [`RENAME COLUMN`](https://clickhouse.com/docs/reference/statements/alter/column#rename-column) reference notes this limitation.

## Add an ALIAS column

```sql theme={null}
ALTER TABLE app_logs ADD COLUMN service_name String ALIAS svc;
```

The new name can be used anywhere in a query:

```sql theme={null}
SELECT service_name, count() AS rows, max(ts) AS last_ts
FROM app_logs
WHERE service_name = 'search'
GROUP BY service_name;
```

```text theme={null}
   ┌─service_name─┬──rows─┬─────────────last_ts─┐
1. │ search       │ 25000 │ 2026-01-02 03:46:39 │
   └──────────────┴───────┴─────────────────────┘
```

## Filters on the alias use the primary index

ClickHouse replaces an alias column with its expression before it plans the query. `EXPLAIN indexes = 1` shows that the condition on `service_name` is applied to the key column `svc`:

```sql theme={null}
EXPLAIN indexes = 1
SELECT ts, message
FROM app_logs
WHERE service_name = 'search';
```

```text theme={null}
Output: ts, message

ReadFromMergeTree (default.app_logs)
   Read type: Default
   Parts: 1 | Granules: 3
   Output: ts, message
   Prewhere filter
   Prewhere filter column:  svc = 'search'
   Indexes:
     PrimaryKey
       Keys:
         svc
       Condition: (svc in ['search', 'search'])
       Parts: 1/1
       Granules: 3/12
       Search Algorithm: binary search
     Ranges: 1
```

The plan is identical to the plan of the same query with `WHERE svc = 'search'`: 3 of 12 granules are read. The same holds for partition pruning when the alias refers to a partition key column, and for `ORDER BY service_name, ts`, which reads the data in the order of the sorting key.

## What the alias does not change

The stored column keeps its name. `svc` is still the key column in the table definition, and the new name is an expression over it:

```sql theme={null}
SELECT name, type, default_kind, default_expression, is_in_sorting_key
FROM system.columns
WHERE database = currentDatabase() AND table = 'app_logs';
```

```text theme={null}
   ┌─name─────────┬─type────────────┬─default_kind─┬─default_expression─┬─is_in_sorting_key─┐
1. │ svc          │ String          │              │                    │                 1 │
2. │ ts           │ DateTime('UTC') │              │                    │                 1 │
3. │ message      │ String          │              │                    │                 0 │
4. │ service_name │ String          │ ALIAS        │ svc                │                 0 │
   └──────────────┴─────────────────┴──────────────┴────────────────────┴───────────────────┘
```

This has a few consequences:

* Both names work in queries, so existing queries that use `svc` are not affected.
* `SELECT *` does not return alias columns. Name the column in the query, or enable the setting [`asterisk_include_alias_columns`](https://clickhouse.com/docs/reference/settings/session-settings/asterisk-include#asterisk_include_alias_columns), which makes `SELECT *` return both `svc` and `service_name`.
* You cannot insert into an alias column. `INSERT INTO app_logs (service_name, ts, message) ...` fails with `No such column service_name in table default.app_logs` (`NO_SUCH_COLUMN_IN_TABLE`). Writers keep using `svc`.
* The alias can be removed at any time with `ALTER TABLE app_logs DROP COLUMN service_name`, which does not touch the data either.

## If the stored column must have the new name

Renaming the stored key column requires a new table. Create it with the column names you want, copy the data with `INSERT INTO ... SELECT`, and swap the two tables with [`EXCHANGE TABLES`](https://clickhouse.com/docs/reference/statements/exchange). Stop writes to the table before the copy starts and resume them only after the exchange: rows inserted into the old table in between are not in the new one.
