Click to copy
Tables • Reviewed for ksqlDB 0.29
How to Add a Column to a Table in ksqlDB
To add a column to a table, simply rerun the CREATE
statement by adding the OR REPLACE
modifier alongside the new column at the end:
CREATE TABLE OR REPLACE product (
id STRING PRIMARY KEY,
name STRING,
color STRING,
new_category STRING
) WITH (...);
Remember how to get the statement used to create a table.
If your table was created from a query, rerun the same CREATE TABLE AS SELECT
statement with the new column.
CREATE TABLE OR REPLACE blue_product
AS SELECT id, name, color, new_category
FROM product_source
WHERE color = 'blue';
Note that since ksqlDB reuses the same offsets for the new query, the new column will only be parsed and populated for newly produced records after the moment you ran the statement.
This only applies to pull queries on materialized tables since push queries on non-materialized tables will do a full scan on the source anyways.
Was this article helpful?