简体   繁体   中英

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM