不存在的表字段的SQL外键
我只是从SQL开始,我有一个任务要做,涉及到SQL表的关系模式 我非常怀疑如何代表以下内容: 我有一个表“Original”,它由以下SQL表示:不存在的表字段的SQL外键,sql,Sql,我只是从SQL开始,我有一个任务要做,涉及到SQL表的关系模式 我非常怀疑如何代表以下内容: 我有一个表“Original”,它由以下SQL表示: create table Original ( idB char(10) not null unique, tituloM varchar(255) not null, primary key (idB, tituloM), foreign key (idB, tituloM) references Mu
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”:
Live(idB;tituloM;data;hora;tituloMO),其中idB
和tituloMO
是“Original”的外键。我的疑问是,我们在“原始”表格中没有“提图洛莫”。我怎样才能代表这一点?目前,我的“原始”表如下所示:
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
字段?您需要知道tituloMO
字段在原始表中对应的内容。它不需要是相同的名称
例如,您可以将Live.tituloMO
映射到Original.tituloM
我不知道您有什么DBMS,您的外键语法似乎缺少父表字段
这是您输入Oracle的方式
foreign key (idB, tituloMO) references Original(idB, tituloM) on delete...
当您进行外键
引用时,不需要相应的列具有相同的名称,只需要具有兼容的数据类型
因此,您的约束条件是:
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,
);
请不要发布使用制表符的代码-重新格式化可能会非常痛苦。此外,不需要像“提前感谢”这样的签准(除了浪费屏幕空间之外)-这就是选择答案和向上投票的目的。您使用什么DBMS?您的外键
似乎缺少父表字段。这在任何DBMS中都是允许的吗?是的,我确实缺少父表字段。谢谢,我确实缺少父表字段。谢谢