Getting an error within a query to create a database, not sure how to fix?

Michael Willach 20 Reputation points
2023-05-01T06:12:26.54+00:00

Hello there, getting an error message like this:
Msg 547, Level 16, State 0, Line 148 The INSERT statement conflicted with the FOREIGN KEY constraint "FK__assignedR__roomI__440B1D61". The conflict occurred in database "HospitalRegistry", table "dbo.Rooms", column 'roomID'.

Was wondering what I've done to cause this or if there is a typo in the code that has cause this to happen,
Any help is appreciated.

Here is the code:

USE master;
GO

IF  DB_ID('HospitalRegistry') IS NOT NULL
    DROP DATABASE HospitalRegistry;
GO

CREATE DATABASE HospitalRegistry;
GO

USE HospitalRegistry
/**Department Table**/
CREATE TABLE [dbo].[Departments]
(
	[departmentID]		INT IDENTITY(1,1)										PRIMARY KEY,
	[departmentName]	VARCHAR(30)												NOT NULL,
);

/**Employees Table**/
CREATE TABLE [dbo].[Employees]
(
	[employeeID]		INT IDENTITY(1,1)										PRIMARY KEY,
	[departmentID]		INT	FOREIGN KEY REFERENCES Departments(departmentID)	NOT NULL,
	[FirstName]			NVARCHAR(50)											NULL,
	[LastName]			NVARCHAR(50)											NULL,
	[Phone]				CHAR(10)												NULL,
	[Address]			VARCHAR(50)												NULL,
);

/**Patient Table**/
CREATE TABLE [dbo].[Patients]
(
	[patientID]			INT IDENTITY(1,1)										PRIMARY KEY,
	[FirstName]			NVARCHAR(50)											NULL,
	[LastName]			NVARCHAR(50)											NULL,
	[Sex]				TINYINT													NOT NULL,
	[DateOfBirth]		DATE													NULL,
	[DateAdmitted]		SMALLDATETIME											NULL,
);

/**Rooms Table**/
CREATE TABLE [dbo].[Rooms]
(
	[roomID]			INT IDENTITY(1,1)										PRIMARY KEY,
	[roomNum]			INT														NULL,
	[roomName]			VARCHAR(50)												NULL,
);

/**Patient Case File Table**/
CREATE TABLE [dbo].[patientCase]
(
	[caseID]			INT IDENTITY(1,1)										PRIMARY KEY,
	[patientID]			INT	FOREIGN KEY REFERENCES Patients(patientID)			NULL,
	[employeeID]		INT FOREIGN KEY REFERENCES Employees(employeeID)		NULL,
	[diagnosis]			VARCHAR(50)												NULL,
);

/**Room Assignment Table**/

CREATE TABLE [dbo].[assignedRooms]
(
	[assignedRoomID]	INT IDENTITY(1,1)										PRIMARY KEY,
	[roomID]			INT	FOREIGN KEY REFERENCES Rooms(RoomID)				NOT NULL,
	[employeeID]		INT	FOREIGN KEY REFERENCES Employees(employeeID)		NULL,
	[patientID]			INT	FOREIGN KEY REFERENCES Patients(patientID)			NULL,
);

/**Table Value Assignment**/
SET IDENTITY_INSERT [dbo].[Departments] ON 

INSERT [dbo].[Departments] ([departmentID], [DepartmentName]) 
VALUES 
(1, 'Administration'),
(2, 'Doctors'),
(3, 'Nurses'),
(4, 'Miscellaneous')
SET IDENTITY_INSERT [dbo].[Departments] OFF
GO

SET IDENTITY_INSERT [dbo].[Rooms] ON 

INSERT [dbo].[Rooms] ([roomID], [roomNum], [roomName]) 
VALUES 
(1, 101, N'Lobby'),
(2, 102, N'Reception Office'),
(3, 103, N'West Wing Hall'),
(4, 104, N'East Wing Hall'),
(5, 105, N'Patient Room 105'),
(6, 106, N'Patient Room 106'),
(7, 107, N'Patient Room 107'),
(8, 108, N'Patient Room 108'),
(9, 109, N'Patient Room 109'),
(10, 110, N'Patient Room 110'),
(11, 111, N'Patient Room 111'),
(12, 112, N'Patient Room 112'),
(13, 113, N'ICU 1'),
(14, 114, N'ICU 2'),
(15, 115, N'Radiology'),
(16, 116, N'MRI Lab'),
(17, 117, N'Lab'),
(18, 118, N'Triage'),
(19, 119, N'Emergency Room'),
(20, 120, N'Server Room'),
(21, 121, N'Break Room'),
(22, 122, N'Head of Medicine''s Office'),
(23, 123, N'Director of Nursing''s Office'),
(24, 124, N'Patient Records Office')
SET IDENTITY_INSERT [dbo].[Rooms] OFF
GO

SET IDENTITY_INSERT [dbo].[Employees] ON 

INSERT [dbo].[Employees] ([employeeID], [departmentID], [FirstName], [LastName], [Phone], [Address]) 
VALUES 
(1, 1, N'Aaron', N'Zenith', N'1234567890', N'asmith@hospital.com'),
(2, 1, N'Richard', N'Mercer', N'0987654321', N'rmercer@hospital.com'),
(3, 1, N'Linda', N'Simmons', N'5045550123', N'lsimmons@hospital.com'),
(4, 2, N'Ravi', N'Kanagala', N'9852230451', N'rkanagala@hospital.com'),
(5, 2, N'Ricardo', N'Colón', N'9852964512', N'rcolon@hospital.com'),
(7, 2, N'Jonathan', N'Payne', N'9858393389', N'jpayne@hospital.com'),
(10, 2, N'George', N'Baudin', N'9858228224', N'gbaudin@hospital.com'),
(11, 3, N'Pamela', N'Barber', N'9852089021', N'preid@hospital.com'),
(12, 3, N'Jo''Ann', N'Carrier', N'9857322914', N'jcarrier@hospital.com'),
(13, 3, N'Rose', N'Newman', N'9859924160', N'rnewman@hospital.com'),
(14, 3, N'Carol', N'Fortenberry', N'7762984087', N'cfortenberry@hospital.com'),
(15, 3, N'Albert', N'Rosenfield', N'7763858881', N'arosenfield@hospital.com'),
(16, 3, N'Gordon', N'Cole', N'6869352298', N'gcole@hospital.com'),
(17, 3, N'Dale', N'Cooper', N'6869916115', N'dbcooper@hospital.com'),
(18, 4, N'Lucy', N'Moran', N'6867852291', N'lmoran@hospital.com'),
(20, 4, N'Denise', N'Bryson', N'5041552066', N'dbryson@hospital.com'),
(21, 4, N'James', N'Hurley', N'5048865876', N'jhurley@hospital.com')
SET IDENTITY_INSERT [dbo].[Employees] OFF
GO

SET IDENTITY_INSERT [dbo].[Patients] ON 

INSERT [dbo].[Patients] ([patientID], [FirstName], [LastName], [Sex], [DateOfBirth], [DateAdmitted]) VALUES 
(1, N'John', N'Sickman', 1, CAST(N'1993-12-07' AS Date), CAST(N'2023-12-06T09:43:00' AS SmallDateTime)),
(2, N'Ronette', N'Pulaski', 2, CAST(N'1989-02-21' AS Date), CAST(N'2023-11-21T23:04:00' AS SmallDateTime)),
(3, N'Laura', N'Palmer', 2, CAST(N'1988-12-04' AS Date), CAST(N'2023-12-07T04:52:00' AS SmallDateTime)),
(4, N'Leo', N'Johnson', 1, CAST(N'1976-05-28' AS Date), CAST(N'2023-12-21T08:21:00' AS SmallDateTime)),
(5, N'Jacques', N'Renault', 1, CAST(N'1973-08-03' AS Date), CAST(N'2023-12-20T02:06:00' AS SmallDateTime)),
(6, N'Norman', N'Jennings', 1, CAST(N'1985-09-29' AS Date), CAST(N'2023-12-01T15:41:00' AS SmallDateTime))
SET IDENTITY_INSERT [dbo].[Patients] OFF
GO

SET IDENTITY_INSERT [dbo].[assignedRooms] ON
INSERT [dbo].[assignedRooms] ([assignedRoomID], [roomID], [employeeID], [patientID]) 
VALUES 
(1, 1, NULL, NULL),
(2, 2, 18, NULL),
(3, 3, NULL, NULL),
(4, 4, NULL, NULL),
(5, 5, 16, 1),
(6, 6, 11, 5),
(7, 7, 11, NULL),
(8, 8, 13, NULL),
(9, 9, 13, NULL),
(10, 10, 16, NULL),
(11, 11, 14, NULL),
(12, 12, 14, NULL),
(13, 13, 12, 2),
(14, 14, 15, NULL),
(15, 15, 17, NULL),
(16, 16, 17, NULL),
(17, 17, NULL, NULL),
(18, 18, NULL, NULL),
(19, 19, NULL, NULL),
(20, 20, 20, NULL),
(21, 21, NULL, NULL),
(22, 13, 12, 3),
(23, 123, 2, NULL),
(24, 124, 3, NULL),
(25, 20, 21, NULL),
(28, 122, 1, NULL)
/**(29, 123, 2, NULL),
(30, 124, 3, NULL)
(23, 5, 16, 4),
(24, 6, 11, 6),**/
SET IDENTITY_INSERT [dbo].[assignedRooms] OFF
GO

SET IDENTITY_INSERT [dbo].[patientCase] ON
INSERT [dbo].[patientCase] ([caseID], [patientID], [employeeID], [diagnosis]) 
VALUES 
(1, 1, 10, N'Severe Pneumonia'),
(2, 2, 7, N'Lacerations'),
(3, 3, 7, N'Internal Bleeding'),
(4, 4, 10, N'Concussion'),
(5, 5, 5, N'Cardiac Arrhythmia'),
(6, 6, 4, N'Possible Melanoma')
SET IDENTITY_INSERT [dbo].[patientCase] OFF
GO
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,865 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.6K Reputation points
    2023-05-01T06:25:12.58+00:00

    For example, you are trying to insert to assignedRooms a room ID that does not exist: 123.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.