2013-12-08 91 views
2

這裏是我創建的表:如何將列更新爲外鍵?

CREATE TABLE enrolled 
(
    sid integer NOT NULL, 
    ccode character varying(6) NOT NULL, 
    CONSTRAINT enrolled_pkey1 PRIMARY KEY (sid, ccode) 
); 

現在我想更新聲明爲從一個叫做類表的外鍵的CCODE列。我怎麼做?

回答

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); 
+0

解決的問題。謝謝。 – user3079679