简体   繁体   English

SQL 查询 - 引用表中没有主键或候选 > 键

[英]SQL queries - There are no primary or candidate > keys in the referenced table

I am trying to create some tables in SQL but when I am running these queries, I am getting the following exception:我正在尝试在 SQL 中创建一些表,但是当我运行这些查询时,出现以下异常:

Msg 1776, Level 16, State 0, Line 1消息 1776,第 16 级,State 0,第 1 行
There are no primary or candidate keys in the referenced table 'Couple' that match the referencing column list in the foreign key 'FK__wedding__CoupleN__2739D489'.引用表“Couple”中没有与外键“FK__wedding__CoupleN__2739D489”中的引用列列表匹配的主键或候选键。

Msg 1750, Level 16, State 0, Line 1消息 1750,第 16 级,State 0,第 1 行
Could not create constraint.无法创建约束。 See previous errors.请参阅以前的错误。

CREATE TABLE Person
(
    FirstName nvarchar (25) NOT NULL,
    SurName nvarchar (25) NOT NULL,
    id char(9) PRIMARY KEY
        CHECK (id LIKE REPLICATE('[0-9]',9)),
    PhoneNumber char(10)  
        CHECK (PhoneNumber LIKE REPLICATE('[0-9]',10)),
    Birthday date 
        CHECK ((GETDATE() - YEAR(Birthday)) > 10)
);

CREATE TABLE Couple
(
    Number int identity(1000,1),
    Partner1 char(9) 
        REFERENCES Person(Id) ON DELETE NO ACTION,
    Partner2 char(9) 
        REFERENCES Person(Id) ON DELETE NO ACTION,
    CoupleName varchar(25) NOT NULL,
    CONSTRAINT PK_Couple1 CHECK (Partner1 <> Partner2),
    CONSTRAINT PK_Couple PRIMARY KEY (Partner1, Partner2, Number)
);

CREATE TABLE weddinghall
(
    Name varchar (25) PRIMARY KEY,
    SeveralSeats int 
         CHECK (SeveralSeats >= 50 AND SeveralSeats <= 5000), 
    Kosher char(1) 
         CHECK (works = 'Y' OR not = 'N'),
    PlaceType varchar(25) 
         CHECK (PlaceType IN s, 'b', 'd', 'doif')),
    NumberOfParkingSpaces int
);

CREATE TABLE wedding
(
    WeddingHallName varchar (25) 
         REFERENCES weddinghall(Name) ON DELETE CASCADE,
    CoupleNumber int 
         REFERENCES Couple (Number) ON DELETE CASCADE,
    WeddingDate date,
    NumberOfGuests Int ,

    CONSTRAINT PK_Wedding PRIMARY KEY  (CoupleNumber, WeddingHallName)
);

CREATE TABLE WeddingGuests
(
     GuestId char(9) references Person (Id) ON DELETE cascade,
     CoupleNumber int references Couple (Number) ON DELETE cascade,
     WeddingDate date,
     WeddingHallName varchar (25) references weddinghall(Name) ON DELETE cascade,
     ArrivalConfirmation CHAR(1),
     ArrivingToWedding Char(1),
     CONSTRAINT PK_WeddingGuests 
         PRIMARY KEY  (GuestId, CoupleNumber, WeddingDate, WeddingHallName)
);

Can you please help?你能帮忙吗?

The error message is telling you that you don't have a unique constraint or primary key on dbo.Couple.Number .错误消息告诉您,您在dbo.Couple.Number上没有唯一约束或主键。 That is the third key column in your primary key constraint, and you can't point a foreign key there.那是主键约束中的第三个键列,您不能将外键指向那里。 You can either:您可以:

  • Add a unique constraint on dbo.Couple.Numberdbo.Couple.Number上添加唯一约束
  • Change the PK to a unique constraint and make dbo.Couple.Number the single-column primary key.将 PK 更改为唯一约束,并使dbo.Couple.Number成为单列主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM