这是我创建的表格:
CREATE TABLE enrolled
(
sid integer NOT NULL,
ccode character varying(6) NOT NULL,
CONSTRAINT enrolled_pkey1 PRIMARY KEY (sid, ccode)
);
现在我想更新 ccode 列,从名为Class的表中声明为外键。我该怎么做?
答案 0 :(得分:1)
ALTER TABLE ONLY enrolled
ADD CONSTRAINT enrolled_ccode_fkey FOREIGN KEY (ccode)
REFERENCES class(ccode)
ON UPDATE CASCADE ON DELETE RESTRICT;
但是要记住这一点,请务必在class.ccode
列上添加唯一索引:
CREATE UNIQUE INDEX class_ccode_idx ON class USING btree (ccode);