2016-01-31 42 views
0
drop table reservation; 
drop table title_copy; 
drop table title; 
drop table rental; 
drop table member; 


create table member(
    member_id number, 
    last_name varchar2(25) not null, 
    first_name varchar2(25), 
    address varchar2(100), 
    city varchar2(30), 
    phone varchar2(15), 
    join_date date DEFAULT SYSDATE not null, 
     constraint pk_member primary key(member_id)); 

commit; 

    create table title(
    Title_id number, 
    Title varchar2(60) not null, 
    Description varchar2(400) not null, 
    Rating varchar2(4), 
    Category varchar2(20), 
    Release_Date date, 
     constraint pk_title primary key(Title_id), 
     constraint ck_title check(Category = 'Drama''Comedy''Action''Child''Scifi' AND Rating = 'Available''Destroyed''Rented''Reserved')); 


commit; 

create table title_copy(
    Copy_id number, 
    Title_id number, 
    Status varchar2(15) not null, 
     constraint pk_title_copy primary key(Copy_id,Title_id)); 


commit; 

create table rental(
    Book_Date date, 
    Member_id number, 
    Copy_id number, 
    Act_Ret_Date date, 
    Exp_Ret_Date date DEFAULT sysdate+2, 
    Title_id number, 
     constraint pk_rental primary key(Book_Date,Member_id,Copy_id,Title_id)); 

commit; 


create table reservation(
    Res_Date date, 
    Member_id number, 
    Title_id number, 
     constraint pk_reservation primary key(Res_date,Member_id,Title_id)); 

commit;  






alter table title_copy 
    add constraint fk_title_copy foreign key(Title_id) 
    references title(Title_id); 


alter table rental 
    add constraint fk_rental2 foreign key(Member_id) 
    references member(Member_id); 


alter table reservation 
    add constraint fk_reservation1 foreign key(Title_id) 
    references title(Title_id); 


alter table reservation 
    add constraint fk_reservation2 foreign key(Member_id) 
    references Member(Member_id); 


alter table rental 
     add constraint fk_rental1 foreign key(Copy_id) 
     references title_copy(Copy_id); 

alter table rental 
     add constraint fk_rental foreign key(Title_id) 
     references title_copy(Title_id); 





desc member; 
desc title; 
desc title_copy; 
desc rental; 
desc reservation; 

select constraint_name, constraint_type 
from user_constraints 
where table_name in ('MEMBER','TITLE','TITLE_COPY','RENTAL','RESERVATION'); 

我不斷收到錯誤「中列列表不匹配的唯一或主鍵」,是指太.....得到錯誤「不匹配的唯一或列的列表主鍵」,在SQL

references title_copy(Copy_id); 

references title_copy(Title_id); 

從當我嘗試設置外鍵約束....

alter table rental 
     add constraint fk_rental1 foreign key(Copy_id) 
     references title_copy(Copy_id); 

alter table rental 
     add constraint fk_rental foreign key(Title_id) 
     references title_copy(Title_id); 

回答

1

你想:

alter table rental 
    add constraint fk_rental1 foreign key(Copy_id,Title_id) 
    references title_copy(Copy_id,Title_id); 

無論Copy_id也不是的title_id的title_copy一個PK。它有一個複合PK {Copy_id,Title_id}。

(如果每個這些列的是title_copy獨特,宣佈他們如此;然後你可以單獨聲明FKS每個)

+0

好的,是的,我明白了,謝謝你的幫助! – bobblehead808

1

檢查了這一點:

https://stackoverflow.com/a/10809656/4633893

如果主鍵是複合鍵(因爲它是在標題副本[「主鍵(Copy_id,title_id的)」]),那麼你不能有外鍵僅引用該複合鍵的一部分(開始編輯每個註釋),除非該複合鍵的該字段被定義爲UNIQUE。

+0

感謝,我明白了吧! – bobblehead808

+0

@phillipxy:感謝您的澄清,您是對的 - 這是唯一性,而不是外鍵的存在,這是這裏的詭計。爲此編輯。 –

+0

那組字段。 – philipxy

相關問題