当我尝试在PostgreSQL中创建桥表时获取错误

时间:2014-10-05 03:09:26

标签: postgresql

我正在尝试使用PostgreSQL创建博物馆数据库

我做了所有这些工作

CREATE DOMAIN lCap as int not null check ( VALUE >= 0 );
CREATE TYPE Cap as (min lCap, max lCap);
CREATE DOMAIN lDim as float8 not null check ( VALUE > 0 );
CREATE TYPE lArea as (length lDim, width lDim);
CREATE DOMAIN lNam as varchar(50) not null;
CREATE DOMAIN lDoor as smallint not null;

create table Locations
(
    locSerial       serial not null primary key,
    locName         lNam,
    locDimensions       lArea,
    locCapacity     Cap,
    locDoors        lDoor
);

CREATE DOMAIN eTime as timestamp not null;
CREATE DOMAIN eNam as varchar(25) not null;
CREATE DOMAIN eDescr as varchar(512) not null;

create table Exhibitions
(
   exhSerial            serial not null  primary key,
   exhName          eNam,
   exhDescription       eDescr,
   exhStrtDate          eTime,
   exhEndDate       eTime

);


CREATE DOMAIN tyID AS smallint not null check (VALUE > 100 );
CREATE DOMAIN tyNam AS varchar(20) not null;

CREATE TABLE Types(
    typID       tyID primary key,
    typName     tyNam

);


CREATE DOMAIN sID AS smallint not null check (VALUE > 100 );
CREATE DOMAIN sNam AS varchar(30) not null;

CREATE TABLE Subtypes(
    sbtID       sID PRIMARY KEY,
    sbtName     sNam


);


CREATE DOMAIN mID AS smallint not null check (VALUE > 100 );
CREATE DOMAIN mNam AS varchar(50) not null;

CREATE TABLE Mediums(
    medID       mID primary key,
    medName     mNam

);


CREATE DOMAIN crID AS smallint not null check (VALUE > 100 );
CREATE DOMAIN crNam AS varchar(35);

CREATE TABLE Creators(
    creID       crID primary key,
    creName     crNam

);


CREATE DOMAIN wAlphID AS varchar(4) not null;
CREATE DOMAIN wNumID AS smallint not null check (VALUE > 1000);
CREATE DOMAIN wObjNam AS varchar(120) not null;
CREATE DOMAIN wDateMad AS varchar(15) not null;
CREATE DOMAIN wDateAcqu AS date not null;
CREATE DOMAIN wInsurValue AS smallint not null;
CREATE DOMAIN wObjDiscip AS varchar(1000) not null;

CREATE TABLE Works(
   workAlphID           wAlphID,
   workNumberID     wNumID,
  workObjectName        wObjNam,
  workCreatorNameID     crID,
  workTypeID            tyID,
  workSubtypeID     sID,
  workDateMade      wDateMad,
  workDateOfAcquisition wDateAcqu,
   workInsuranceValue       wInsurValue,
   workObjectDiscription    wObjDiscip,
  primary key (workAlphID,workNumberID),
  foreign key (workCreatorNameID) references Creators(creID),
  foreign key (workTypeID) references Types(typID),
  foreign key (workSubtypeID) references Subtypes(sbtID)
);

这一切都没关系

但是当我试图创建这个表Works_Mediums作为Works表和媒体表之间的桥梁

我一直收到这个错误 错误:对于引用表“works”

的给定键,没有唯一约束匹配
CREATE TABLE Works_Mediums(         
   wmedAlphID      wAlphID, 
   wmedNumberID    wNumID,
   wmedID          mID,
   primary key (wmedAlphID, wmedNumberID, wmedID),
    foreign key (wmedAlphID) references Works(workAlphID),
    foreign key (wmedNumberID) references Works(workNumberID),
    foreign key (wmedID) references Mediums(medID)  
);

任何想法如何解决此错误?

方面

1 个答案:

答案 0 :(得分:2)

这部分

foreign key (wmedAlphID) references Works(workAlphID),
foreign key (wmedNumberID) references Works(workNumberID),

应该是

foreign key (wmedAlphID, wmedNumberID) references Works (workAlphID, workNumberID),

PostgreSQL的错误信息非常好。在“Works”表中,唯一约束(在这种情况下是主键约束)位于“workAlphID”和“workNumberID”列对上。您的外键约束需要引用该对

唯一约束不一定必须是主键约束。声明为unique的任何列都可以是外键约束的目标。