6.外鍵

A foreign key is a column or group of columns in one table that contains values that match the primary key values in the same or another table.

語法:

[CONSTRAINT c_name]

[[FOREIGN KEY] ({col_name1} ,...)]

REFERENCES table_name ({col_name2},...)

[ON DELETE {NO ACTION| CASCADE | SET NULL | SET DEFAULT}]

[ON UPDATE {NO ACTION | CASCADE | SET NULL | SET DEFAULT}]

 

CREATE TABLE works_on1

(emp_no INTEGER NOT NULL,

project_no CHAR(4) NOT NULL,

job CHAR (15) NULL,

enter_date DATE NULL,

CONSTRAINT prim_works1 PRIMARY KEY(emp_no, project_no),

CONSTRAINT foreign1_works1 FOREIGN KEY(emp_no)

REFERENCES employee(emp_no) ON DELETE CASCADE,

CONSTRAINT foreign2_works1 FOREIGN KEY(project_no)

REFERENCES project(project_no) ON UPDATE CASCADE);

 

works_on1 table that uses the ON DELETE CASCADE and ON UPDATE CASCADE options.Each deletion of a row in the employee table will causethe additional deletion of all rows in the  works_on1 table that have the corresponding value in the emp_no column. Similarly, each update of a value in the project_no column of the project table will cause the same modification on all corresponding values in the project_no column of the works_on1 table.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章