SQL:2个外键引用一个主键

时间:2013-12-16 18:32:49

标签: sql

我有一个数据库图表,需要创建一个包含不同表格的数据库。

enter image description here

这是我的代码:

use FirmaLieferungen;

drop table liefert;
drop table rabatt;
drop table artikel;
drop table firma;

SET DATEFORMAT dmy;

create table firma (
   fnr            integer primary key,
   name           char(10),
   jahrgruendung  integer, -- Gründungsjahr
   land           char(3)
);

insert into firma     values (101,'Schwer'    ,1890,'A'  );
insert into firma     values (102,'Schmal'    ,1901,'CH' );
insert into firma     values (103,'Tief'      ,1945,'I'  );
insert into firma     values (104,'Breit'     ,1950,'A'  );
insert into firma     values (105,'Leicht'    ,1945,'F'  );
insert into firma     values (106,'Hoch'      ,1920,'CH' );
insert into firma     values (107,'Hell'      ,1900,'A'  );

create table artikel (
   fnr            integer,
   lfdnr          integer,
   bezeichnung    char(10),
   preis          decimal(6,2),
   einheit        char(3),
   land           char(3),
   primary key(fnr, lfdnr),
   foreign key(fnr) references firma
);

insert into artikel   values (101,1,'Schaufel'  ,12.30,'Stk','A'  );
insert into artikel   values (101,2,'Hacke'     ,15.20,'Stk','F'  );
insert into artikel   values (102,1,'Spaten'    ,13.00,'Stk','A'  );
insert into artikel   values (103,1,'Schere'    , 8.00,'Stk','A'  );
insert into artikel   values (103,2,'Messer'    ,10.60,'Stk','F'  );
insert into artikel   values (103,3,'Schnur'    , 1.10,'m'  ,'D'  );
insert into artikel   values (105,1,'Schnur'    , 0.40,'m'  ,'D'  );
insert into artikel   values (106,1,'Hacke'     ,20.70,'Stk','CH' );
insert into artikel   values (106,2,'Draht'     , 0.60,'m'  ,'CH' );

create table liefert (
   fnrvon         integer,
   fnran          integer,
   fnr            integer,
   lfdnr          integer,
   datum          date,
   menge          decimal(8,2)
   primary key(fnrvon, fnran, fnr, lfdnr, datum),
   foreign key(fnr, lfdnr) references artikel,
   foreign key(fnr) references firma
);

insert into liefert values (101,102,101,1,'01.02.1999',    3.00);
insert into liefert values (101,102,101,1,'02.01.2000',    2.00);
insert into liefert values (101,104,101,2,'13.02.2000',   11.00);
insert into liefert values (101,104,101,1,'24.11.1999',   19.00);
insert into liefert values (101,105,103,3,'31.03.2001', 1553.00);
insert into liefert values (102,101,102,1,'21.04.1999',   28.00);
insert into liefert values (102,101,101,1,'11.12.1999',    1.00);
insert into liefert values (102,104,101,1,'04.07.2000',   63.00);
insert into liefert values (103,101,103,3,'21.04.1999',    3.25);
insert into liefert values (103,104,101,1,'08.02.1998',   17.00);
insert into liefert values (104,102,105,1,'19.11.2001',  132.50);
insert into liefert values (104,106,101,1,'04.07.2000',   22.00);
insert into liefert values (106,102,101,1,'07.08.2002',   81.00);
insert into liefert values (106,102,106,2,'01.06.2002',   21.30);
insert into liefert values (106,104,101,1,'26.09.2001',    2.00);

create table rabatt (
   fnrvon         integer,
   fnran          integer,
   prozent        decimal (5,2),
   primary key (fnrvon, fnran),
   foreign key (fnrvon, fnran) references firma
);

insert into rabatt    values (101,102, 5.25);
insert into rabatt    values (102,101, 5.50);
insert into rabatt    values (101,103,15.75);
insert into rabatt    values (103,102, 7.50);
insert into rabatt    values (102,103,10.50);
insert into rabatt    values (105,106, 5.25);
insert into rabatt    values (104,101, 7.50);



select * from rabatt;
select * from firma;
select * from liefert;
select * from artikel;

但是'rabatt'创建中存在错误,它表示最后一个命令无效。

  

外键(fnrvon,fnran)引用firma

这有些不对,但我不知道为什么......图表错了吗?从'liefert'到'firma'还有两个键如何做到这一点?请帮我!

谢谢! (我正在使用Microsoft SQL Server 2008)

3 个答案:

答案 0 :(得分:4)

引用另一个表时,应指定要引用的列。 所以,例如:

create table liefert (
   fnrvon         integer,
   fnran          integer,
   fnr            integer,
   lfdnr          integer,
   datum          date,
   menge          decimal(8,2)
   primary key(fnrvon, fnran, fnr, lfdnr, datum),
   foreign key(fnr, lfdnr) references artikel (fnr, lfdnr),
   foreign key(fnr) references firma (fnr)
);

答案 1 :(得分:1)

Mureinik是对的,只在我面前1秒回答。 为了这个问题,我提出了一个小提琴,以便进一步提出问题

http://sqlfiddle.com/#!6/6a1b7

答案 2 :(得分:0)

您的firma表没有名为fnrvon或fnran的列,所以

foreign key (fnrvon, fnran) references firma

未能弄清楚所引用的内容。

你需要明确。此外,如果两列都引用相同的外键,则它需要是两个单独的语句

foreign key (fnran) references firma (fnr)
foreign key (fnrvon) references firma (fnr)