gpt4 book ai didi

sql - 引用的表中没有主键或候选键

转载 作者:行者123 更新时间:2023-12-03 10:50:33 25 4
gpt4 key购买 nike

Error: There are no Primary or Candidate Keys in the referenced table 'dbo.Customers' that match the referencing column list in the foreign key 'FK_Reservation_Customers_FrstNme FOREIGN KEY'


DROP TABLE dbo.Customers;
DROP TABLE dbo.Staff;
DROP TABLE dbo.Rooms;
DROP TABLE dbo.Reservation;
GO
CREATE TABLE "Customers"(

CustomerID int IDENTITY (1,1) NOT NULL,
FirstName nvarchar(20) NULL,
LastName nvarchar(20) NULL,
StreetNo int NULL,
City nvarchar(20) NULL,
PostCode nvarchar(20) NULL,
Email nvarchar(50) NULL,

CONSTRAINT PK_Customers PRIMARY KEY
(
CustomerID
)
)
CREATE TABLE "Staff"(

StaffID nvarchar(20) NOT NULL,
Pass nvarchar(20) NOT NULL,

CONSTRAINT PK_Staff PRIMARY KEY
(
StaffID
)
)
CREATE TABLE "Rooms"(

RoomNo int NOT NULL,
RoomType nvarchar(20) NULL,
PricePerNight money NULL,
MaximumOccupancy int NULL,
No0fBeds int NULL,
NoOfBathrooms int NULL,
Entertainment bit NULL,
RoomService bit NULL,
Gym bit NULL,

CONSTRAINT PK_Rooms PRIMARY KEY
(
RoomNo
)
)
CREATE TABLE "Reservation"(

ReservationID int IDENTITY (1,1) NOT NULL,
CustomerID int NOT NULL,
FirstName nvarchar(20) NULL,
LastName nvarchar(20) NULL,
RoomType nvarchar(20) NULL,
RoomNo int NOT NULL,
CheckInDate date NULL,
CheckOutDate date NULL,

CONSTRAINT PK_Reservation PRIMARY KEY
(
ReservationID
),
CONSTRAINT FK_Reservation_Customers_CustID FOREIGN KEY
(
CustomerID
)
REFERENCES dbo.Customers
(
CustomerID
),
CONSTRAINT FK_Reservation_Customers_FrstNme FOREIGN KEY
(
FirstName
)
REFERENCES dbo.Customers
(
FirstName
)
)

有人可以告诉我这里发生了什么以及如何解决。我要作外键的所有其他键也发生同样的问题。除了我是否要引用主键。

最佳答案

如果要创建外键,则它必须引用主键或具有唯一约束的字段。

如果要显示客户的名称,请使外键引用CustomerID,并显示带有联接的结果。

关于sql - 引用的表中没有主键或候选键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com