0

我有三个表:A、B 和 C。我想创建表 A,它应该有 3 列,第一个应该是PRIMARY KEY.

1)如何创建第二列,其中是对表 B 的引用,

2) 第三个 with 是对表 CAid = C.id 中的“C.id”行的引用

CREATE TABLE A
(
    id SERIAL PRIMARY KEY,
    // ? - reference to table B
    // ? - reference to C.id row. A.id = C.id
)

数据库:postgresql

4

1 回答 1

1

我假设你使用 mysql 数据库。

CREATE TABLE A
(
    id INT NOT NULL PRIMARY KEY,
    b_id INT NOT NULL,
    c_id INT NOT NULL,
    FOREIGN KEY (b_id) REFERENCES B (id),
    FOREIGN KEY (c_id) REFERENCES C (id)
) TYPE = INNODB;

使用 postgresql 的更新:

CREATE TABLE "A"
(
   id integer NOT NULL, 
   b_id integer NOT NULL, 
   c_id integer NOT NULL, 
   CONSTRAINT id PRIMARY KEY (id), 
   CONSTRAINT b_id FOREIGN KEY (b_id) REFERENCES "B" (id) 
      ON UPDATE NO ACTION ON DELETE NO ACTION, --with no action restriction
   CONSTRAINT c_id FOREIGN KEY (c_id) REFERENCES "C" (id) 
      ON UPDATE CASCADE ON DELETE CASCADE  --with cascade restriction
) 
WITH (
  OIDS = FALSE
)
;
ALTER TABLE "C" OWNER TO postgres;

于 2012-11-24T21:13:19.993 回答