我有id和name的表,其中id是主键,bigint作为数据类型。现在我想改变列,其中id为具有自动递增值的主键。
我的表:
CREATE TABLE test(
testid bigint NOT NULL,
testname character varying(255),
CONSTRAINT test_pkey PRIMARY KEY (testid)
);
更改查询:
ALTER TABLE test ALTER COLUMN testid BIGSERIAL PRIMARY KEY;
执行查询后,我收到以下错误,
ERROR: syntax error at or near "BIGSERIAL"
LINE 1: ALTER TABLE test ALTER COLUMN testid BIGSERIAL P...
^
********** Error **********
ERROR: syntax error at or near "BIGSERIAL"
SQL state: 42601
Character: 50
答案 0 :(得分:0)
你可以尝试这个命令
ALTER TABLE your_table ALTER COLUMN key_column TYPE BIGSERIAL PRIMARY KEY;
我希望它对你有所帮助
答案 1 :(得分:0)
--This will drop the primary key temporarily
ALTER TABLE test
drop CONSTRAINT test_pkey
--change data type
ALTER TABLE test
ALTER COLUMN testid BigInt
--add primary key
ALTER TABLE test
ADD CONSTRAINT test_pkey PRIMARY KEY (testid )