gpt4 book ai didi

sql - 使用 oid 作为主键

转载 作者:行者123 更新时间:2023-11-29 13:37:08 25 4
gpt4 key购买 nike

是否可以使用 oid 数据类型作为主键?

CREATE TABLE "Test"
(
id oid NOT NULL DEFAULT nextval('"Test_id_seq"'::regclass),
"some" text,
CONSTRAINT "Test_pkey" PRIMARY KEY (id)
)

我想使用 unsigned int 数据类型。

DrColossos

Furthermore, you can't use foreign key constraints on OIDs (at least this is what google tells me)

CREATE TABLE "Test"
(
id oid NOT NULL,
val text NOT NULL,
CONSTRAINT "Test_pkey" PRIMARY KEY (id)
);

CREATE TABLE "Test2"
(
id integer NOT NULL DEFAULT nextval('"Test2_Id_seq"'::regclass),
"TestId" oid NOT NULL,
CONSTRAINT "Test2_pkey" PRIMARY KEY (id),
CONSTRAINT "Test2_TestId_fkey" FOREIGN KEY ("TestId")
REFERENCES "Test" (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT
);

INSERT INTO "Test"(id, val) VALUES (1, 'one');
INSERT INTO "Test"(id, val) VALUES (2, 'two');

select * from "Test";

insert into "Test2" ("TestId") values (1);
insert into "Test2" ("TestId") values (2);
insert into "Test2" ("TestId") values (4);

ERROR: insert or update on table "Test2" violates foreign key constraint "Test2_TestId_fkey"
DETAIL: Key (TestId)=(4) is not present in table "Test".

select * from "Test2";

最佳答案

只要尝试一下,您就会知道这是否可行。答案是肯定的。 (这很不寻常,但我暂时看不出它会导致问题的原因。)

关于sql - 使用 oid 作为主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com