I have a table like this,
table name = staff
staff-id(pk), last-name, first-name, job-title, reports-to(fk)
Reports to is a self referencing key.
I already have data to insert in to this table. In my data all the staff members have reports-to person but MD does not have a reports-to person as MD is the highest position. So as it references to no key how can I create table as a self referencing foreign key because insertion will probably cause a problem.
Data to MD column is
staff-id last-name first-name job-title reports-to
200 Vellum Harry MD 0
CREATE TABLE staff
( staff-id number not null,
last-name varchar2(20) not null,
first-name varchar2(20),
job-title varchar2(20) not null,
reports-to number not null,
CONSTRAINT staffid_pk PRIMARY KEY (staff-id)
CONSTRAINT reportsto_fk FOREIGN KEY (staff-id) REFERENCES staff(staff-id)
);
Can I have MD's reports-to key as his primary key, if so again adding will cause a problem. How to solve this problem? I'm using oracle 11g
要么将MD报告设置为self(即,将Reports-To
列设置为staff-id
列值),要么将Reports-To
列设置为可空(外键列可以等于NULL
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.