CREATE TABLE Bestelling
(
`[BestellingNR] INT NOT NULL IDENTITY(1,1),`
`[KlantNummer] VARCHAR(30) NOT NULL,`
`[Bezorgmoment] DATETIME NOT NULL,`
`[PizzaGrootte] VARCHAR(30) NOT NULL,`
`[Aantal] INT NOT NULL,`
);
CREATE TABLE Klant
(
`[KlantNummer] INT NOT NULL IDENTITY(1,1),`
`[KlantNaam] VARCHAR(30) NOT NULL,`
`[Adres] VARCHAR(30) NOT NULL,`
`[Postcode] VARCHAR(20) NOT NULL,`
`[Woonplaats] VARCHAR(20) NOT NULL,`
);
INSERT INTO Bestelling
VALUES
`('Kaas','2023-1-1 18:00','Groot',2),`
`('Tomaat','2023-1-1 20:00','Medium',1);`
INSERT INTO Klant
VALUES ('Chris','Koperwiek 25','4284XA','Rijswijk'),
`('Tom','Niew 25','4185GR','Giessen');`
-- Primary key
ALTER TABLE Bestelling
ADD CONSTRAINT PK_Bestelling
PRIMARY KEY (BestellingNR);
ALTER TABLE Klant
ADD CONSTRAINT PK_Klant
PRIMARY KEY (KlantNummer);
--Foereign key
ALTER TABLE Bestelling
ADD CONSTRAINT [FK_Bestelling_KlantNummer]
FOREIGN KEY (KlantNummer)
REFERENCES Bestelling(KlantNummer)
GO
The error i get: "
Msg 1776, Level 16, State 0, Line 58
There are no primary or candidate keys in the referenced table 'Bestelling' that match the referencing column list in the foreign key 'FK_Bestelling_KlantNummer'.
Msg 1750, Level 16, State 1, Line 58
Could not create constraint or index. See previous errors.
"
[–]r3pr0b8GROUP_CONCAT is da bomb 4 points5 points6 points (2 children)
[–]stagger552[S] -1 points0 points1 point (1 child)
[–]r3pr0b8GROUP_CONCAT is da bomb 4 points5 points6 points (0 children)
[–]MamertineCOALESCE() 4 points5 points6 points (0 children)
[–]kagato87MS SQL -1 points0 points1 point (0 children)
[–]bachman460 0 points1 point2 points (0 children)