Adding an `IDENTITY` column to an existing delta table (Databricks)

Viewed 373

How to add an identity column to an existing delta table.

ALTER TABLE tname ADD COLUMN id BIGINT GENERATED BY DEFAULT AS IDENTITY;

doesn't seem to be supported.

1 Answers

Yes, apparently you can't add a generated column. ALTER TABLE syntax doesn't seem to allow that.

As a workaround create a table from scratch and copy data:

CREATE TABLE tname_ (
    <tname columns>,
    id BIGINT GENERATED BY DEFAULT AS IDENTITY
);
INSERT INTO tname_ (<tname columns>) SELECT * FROM tname;
DROP TABLE tname;
ALTER TABLE tname_ RENAME TO tname;
Related