使用oid作为主键

时间:2011-09-06 11:49:32

标签: sql postgresql

是否可以将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

  

此外,你不能在OID上使用外键约束(至少这是谷歌告诉我的)

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";

1 个答案:

答案 0 :(得分:1)

只是尝试它会告诉你是否可能。答案是肯定的。 (这是不寻常的,但随便我看不出它会导致问题的原因。)