Use oid as primary key
Is it possible to use oid data type as primary key?
CREATE TABLE "Test"
(
id oid NOT NULL DEFAULT nextval('"Test_id_seq"'::regclass),
"some" text,
CONSTRAINT "Test_pkey" PRIMARY KEY (id)
)
I want to use unsigned int data type.
to DrColossos
Furthermore, you can't use foreign key constraints on OIDs (at least this is what google tells me)
CREATE开发者_C百科 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";
Just trying it would have told you whether it's possible. The answer is yes. (It's unusual, but offhand I can't see a reason why it would cause problems.)
精彩评论