Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There are no Primary or Candidate Keys in the referenced table

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         )     ) 

Could someone please tell me whats happening here and how i can fix it. Same problem occurs with all the other keys i want to make a foreign key. Except if i want to reference a primary key.

like image 505
user1638362 Avatar asked Aug 31 '12 10:08

user1638362


People also ask

What happens if there is no primary key in SQL?

Every table should have some column (or set of columns) that uniquely identifies one and only one row. It makes it much easier to maintain the data. It's true, without a primary key (or some unique key), you don't have an insertion anomaly if you go to insert the same data multiple times.

How many primary key and candidate key exist in a table?

Primary key values should be unique and non-null. There can be multiple Super keys and Candidate keys in a table, but there can be only one Primary key in a table. Alternate keys are those Candidate keys that were not chosen to be the Primary key of the table.

What is primary key and candidate in the table?

Definition. Primary Key is a unique and non-null key which identify a record uniquely in table. A table can have only one primary key. Candidate key is also a unique key to identify a record uniquely in a table but a table can have multiple candidate keys.

How do I add a candidate key to a table?

You declare a column as a candidate key by using the keyword UNIQUE . Precede the UNIQUE keyword with the NOT NULL specification. Like a primary key, a candidate key also uniquely identifies a row in a table. Note that a table can have only one primary key, but can have any number of candidate keys.


2 Answers

If you want to create a foreign key, it must reference either the primary key, or a field with a unique constraint.

If you want to display the customer's name, make the foreign key reference the CustomerID, and display the results with a join.

like image 138
podiluska Avatar answered Sep 19 '22 06:09

podiluska


I ran into this error when specifying key columns in the referenced table that were out of the same order that the primary key or candidate key was defined with on the reference table.

Switching the order the columns were specified fixed it.

Ex:

ALTER TABLE [dbo].[MCL_item_vendor_override_mst]  WITH CHECK ADD   CONSTRAINT [FK_MCL_item_vendor_override_mst_item_mst] FOREIGN KEY([item],[site_ref])     REFERENCES [dbo].[item_mst] ([item],[site_ref]) 

failed while

ALTER TABLE [dbo].[MCL_item_vendor_override_mst]  WITH CHECK ADD   CONSTRAINT [FK_MCL_item_vendor_override_mst_item_mst] FOREIGN KEY([site_ref],[item])     REFERENCES [dbo].[item_mst] ([site_ref],[item]) 

worked.

like image 27
J. Polfer Avatar answered Sep 22 '22 06:09

J. Polfer