Question

I am trying to add a foreign key to a table. I have created two tables.

CREATE TABLE madeupbusiness.staff
(
staffnum int NOT NULL,
forename varchar(30) NOT NULL,
surname varchar(30) NOT NULL,
meeting int NOT NULL,
PRIMARY KEY (staffnum),
)
GO

meeting should use the PK from from the meeting table to create a FK :

CREATE TABLE madeupbusiness.meeting
(
meetingnum int NOT NULL,
room varchar(30) NOT NULL,
PRIMARY KEY (meetingnum),
)
GO

To create the foreign key I run this query

ALTER TABLE madeupbusiness.staff
WITH CHECK
ADD CONSTRAINT FK_staff_meetingnum FOREIGN KEY (meetingnum)
REFERENCES madeupbusiness.meeting(meetingnum)
ON DELETE CASCADE
ON UPDATE CASCADE
;
GO

The query runs but when I create a database diagram there is a square loop the staff table from the staffnum key back onto it. Sorry but I don't really know how to describe it. There is no relationship between the two tables. What am I doing wrong?

I have tried to add the relationship from design view but the foreign key table is greyed out.

Was it helpful?

Solution

If you can rebuild the Table do this:

   CREATE TABLE madeupbusiness.meeting
   (meetingnum int NOT NULL PRIMARY KEY REFERENCES madeupbusiness.meeting(YourColumnYouWantItShouldBeReferenced),
   room varchar(30) NOT NULL);
   GO
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top