2011-11-13 15 views
0

我剛剛開始使用SQL,我有一個指定的操作來執行涉及SQL表的關係模式。對一個不存在的表字段的SQL外鍵

我對如何表示下面一個大疑問: 我已經表「原始」是由下面的SQL表示:

create table Original (
    idB  char(10)  not null unique, 
    tituloM varchar(255) not null, 
    primary key (idB, tituloM), 
    foreign key (idB, tituloM) references Musica on delete cascade on update cascade 
); 

我現在代表表「實時」是具有以下關係表示: Live(idB; tituloM; data; hora; tituloMO),其中idBtituloMO是「原始」的外鍵。我的疑問是,我們在「原始」表中沒有「tituloMO」。我怎麼能代表這個?目前我的「原始」表如下所示:

create table Live (
    idB  char(10) not null unique, 
    tituloM varchar(255) not null, 
    /* date goes here */ 
    /* time goes here */ 
    tituloMO varchar(255) not null, 
    primary key (idB, tituloM), 
    foreign key (idB, tituloM) references Musica on delete cascade on update cascade, 
    foreign key (idB, data, hora) references Concerto on delete cascade on update cascade, 
    foreign key (idB, tituloMO) 
); 

如何正確表示tituloMO字段?

+1

請不要發佈使用標籤代碼 - 它可以是一個真正的痛苦重新格式化。此外,簽名的「提前致謝」是沒有必要的(除了浪費屏幕房地產) - 這是選擇一個答案和upvoting是。 –

+0

你使用什麼DBMS?你的外鍵看起來像是錯過了父表的字段。這是真的允許在任何DBMS? –

+0

是的,我確實缺少父表的字段。謝謝 –

回答

0

當你犯了一個FOREIGN KEY參考,你並不需要爲相應的列具有相同的名稱,只有兼容的數據類型。

所以,你的約束條件是:

create table Original (
    idB  char(10)  not null unique, 
    tituloM varchar(255) not null, 
    primary key (idB, tituloM), 
    foreign key (idB, tituloM) 
    references Musica (idB, tituloM)   --- you need these, too 
     on delete cascade 
     on update cascade 
); 

create table Live (
    idB  char(10) not null unique, 
    tituloM varchar(255) not null, 
    /* date goes here */ 
    /* time goes here */ 
    tituloMO varchar(255) not null, 
    primary key (idB, tituloM), 
    foreign key (idB, tituloM) 
    references Musica (idB, tituloM)   --- and these 
     on delete cascade 
     on update cascade, 
    foreign key (idB, data, hora) 
    references Concerto (idB, data, hora) --- and these 
     on delete cascade 
     on update cascade, 
    foreign key (idB, tituloMO) 
    references Original (idB, tituloM)  --- this is an assumption 
     on delete cascade      --- based on the datatypes 
     on update cascade, 
); 
1

您需要知道Original表中的tituloMO字段對應的內容。它不需要是相同的名稱。

例如,你可以映射到Live.tituloMOOriginal.tituloM

我不知道是什麼DBMS你有,你的外鍵的語法看起來缺少父表的字段。

這就是你如何輸入Oracle。

foreign key (idB, tituloMO) references Original(idB, tituloM) on delete... 
+0

是的,我確實缺少父表的字段。謝謝。 –

相關問題