sql 将整数列转换为自动递增

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 将整数列转换为自动递增相关的知识,希望对你有一定的参考价值。

-- See https://stackoverflow.com/a/15748896/3163905

-- Create a new sequence
CREATE sequence <column_id_seq>;
-- Alter the field you're converting to an auto-incrementing field be setting a default value based on a sequence
ALTER TABLE <table_name> alter <id_column> SET DEFAULT nextval(<'id_column_seq'>);
-- Set the current max value of the id column as the starting point for the sequence
SELECT setval(<'id_column_seq'>, <max_id_column_value> );

以上是关于sql 将整数列转换为自动递增的主要内容,如果未能解决你的问题,请参考以下文章