我现在正在尝试创建一个SQL Server数据库但是我遇到的错误似乎无法解决。我是一名学生,他试图通过我在互联网上找到的作业来改善自己,这个让我难过。以下是我与数据库部分相关的错误
Msg 547,Level 16,State 0,Line 221
INSERT语句与FOREIGN KEY约束冲突" TPatients_TRandomCodes_FK"。冲突发生在数据库" Final",table" dbo.TRandomCodes",column' intRandomCodeID'。Msg 547,Level 16,State 0,Line 294
INSERT语句与FOREIGN KEY约束冲突" TPatientVisits_TPatients_FK"。冲突发生在数据库" Final",table" dbo.TPatients",column' intPatientID'。
以下是我的表格和记录的插入声明
ALTER TABLE TSites
ADD CONSTRAINT TSites_TStudies_FK
FOREIGN KEY(intStudyID) REFERENCES TStudies(intStudyID)
ALTER TABLE TPatients
ADD CONSTRAINT TPatients_TSites_FK
FOREIGN KEY(intSiteID) REFERENCES TSites(intSiteID)
ALTER TABLE TVisitTypes
ADD CONSTRAINT TVisitTypes_TPatients_FK
FOREIGN KEY (intPatientID) REFERENCES TPatients(intPatientID)
ALTER TABLE TPatientVisits
ADD CONSTRAINT TPatientVisits_TPatients_FK
FOREIGN KEY(intPatientID) REFERENCES TPatients(intPatientID)
ALTER TABLE TRandomCodes
ADD CONSTRAINT TRandomCodes_TStudies_FK
FOREIGN KEY(intStudyID) REFERENCES TStudies(intStudyID)
ALTER TABLE TDrugKits
ADD CONSTRAINT TDrugKits_TSites_FK
FOREIGN KEY(intSiteID) REFERENCES TSites(intSiteID)
ALTER TABLE TPatientVisits
ADD CONSTRAINT TPatientVisits_TWithdrawReasons_FK
FOREIGN KEY(intWithdrawReasonID) REFERENCES TWithdrawReasons(intWithdrawReasonID)
ALTER TABLE TPatients ADD CONSTRAINT TPatients_TGenders_FK
FOREIGN KEY ( intGenderID ) REFERENCES TGenders ( intGenderID )
ALTER TABLE TDrugKits ADD CONSTRAINT TDrugKits_TPatientVisits_FK
FOREIGN KEY ( intVisitID ) REFERENCES TPatientVisits ( intVisitID )
ALTER TABLE TPatients ADD CONSTRAINT TPatients_TRandomCodes_FK
FOREIGN KEY ( intRandomCodeID ) REFERENCES TRandomCodes ( intRandomCodeID )
-- Inserts for TStudies
INSERT INTO TStudies (intStudyID, strStudyDesc)
VALUES (12345, 'Study 1'), (54321, 'Study 2')
-- Inserts for TGenders
INSERT INTO TGenders (intGenderID, strGender)
VALUES (1, 'Female'), (2, 'Male')
-- Inserts for TSites
INSERT INTO TSites (intSiteID, intSiteNumber, intStudyID, strName, strAddress,strCity, strState, strZip, strPhone)
VALUES (1, 101, 12345, 'Dr. Stan Heinrich', '123 E. Main St', 'Atlanta', 'GA', '25869', '1234567890'),
(2, 111, 12345, 'Mercy Hospital', '3456 Elmhurst Rd.', 'Secaucus', 'NJ', '32659', '5013629564'),
(3, 121, 12345, 'St. Elizabeth Hospital', '976 Jackson Way', 'Ft. Thomas', 'KY', '41258', '3026521478'),
(4, 131, 12345, 'Dr. Jim Smith', '32454 Morris Rd.', 'Hamilton', 'OH', '45013', '3256847596'),
(5, 141, 12345, 'Dr. Dan Jones', '1865 Jelico Hwy.', 'Knoxville', 'TN', '34568', '2145798241'),
(6, 501, 54321, 'Dr. Robert Adler', '9087 W. Maple Ave.', 'Cedar Rapids', 'IA', '42365', '6149652574')
,( 7, 511, 54321, 'Dr. Tim Schmitz', '4539 Helena Run', 'Johnson City', 'TN', '34785', '5066987462' )
,( 8, 521, 54321, 'Dr. Lawrence Snell', '9201 NW. Washington Blvd.', 'Bristol', 'VA', '20163', '3876510249' )
,( 9, 531, 54321, 'Cedar Sinai Medical Center', '40321 Hollywood Blvd.', 'Portland', 'OR', '50236', '5439510246' )
,( 10,541, 54321, 'Vally View Hospital', '398 Hampton Rd.', 'Seattle', 'WA', '41203', '7243780036' )
-- Inserts for TPatients
INSERT INTO TPatients ( intPatientID, intPatientNumber, intSiteID, dtmDOB, intGenderID, intWeight, intRandomCodeID )
VALUES ( 1, 101001, 1, '01/02/1956', 1, 155, 1000 )
,( 2, 102001, 2, '01/02/1960', 2, 255, 1001 )
,( 3, 103001, 3, '01/02/1970', 1, 105, 1002 )
,( 4, 104001, 4, '01/02/1980', 2, 175, 1003 )
,( 5, 105001, 5, '01/02/1990', 1, 115, 1004 )
,( 6, 106001, 6, '01/02/1993', 2, 195, 1005 )
,( 7, 107001, 7, '01/02/1974', 1, 125, 5000 )
,( 8, 108001, 8, '01/02/1969', 2, 225, 5001 )
,( 9, 109001, 9, '01/02/1943', 1, 113, 5002 )
,( 10, 110001, 10, '01/02/1984', 2, 163, 5003 )
,( 11, 111001, 11, '01/02/1988', 1, 100, 5004 )
,( 12, 112001, 12, '01/02/1977', 2, 203, 5005 )
-- --------------------------------------------------------------------------------
-- Inserts for TRandomCodes
-- --------------------------------------------------------------------------------
INSERT INTO TRandomCodes ( intRandomCodeID ,intRandomCode ,intStudyID, strTreatment, blnAvailable )
VALUES ( 1,1000, 12345, 'A', 'T' )
,( 2,1001, 12345, 'P', 'T' )
,( 3,1002, 12345, 'A', 'T' )
,( 4, 1003, 12345, 'P', 'T' )
,( 5, 1004, 12345, 'P', 'T' )
,( 6, 1005, 12345, 'A', 'T' )
,( 7, 1006, 12345, 'A', 'T' )
,( 8, 1007, 12345, 'P', 'T' )
,( 9, 1008, 12345, 'A', 'T' )
,( 10, 1009, 12345, 'P', 'T' )
,( 11, 1010, 12345, 'P', 'T' )
,( 12, 1011, 12345, 'A', 'T' )
,( 13, 1012, 12345, 'P', 'T' )
,( 14, 1013, 12345, 'A', 'T' )
,( 15, 1014, 12345, 'A', 'T' )
,( 16, 1015, 12345, 'A', 'T' )
,( 17, 1016, 12345, 'P', 'T' )
,( 18, 1017, 12345, 'P', 'T' )
,( 19, 1018, 12345, 'A', 'T' )
,( 20, 1019, 12345, 'P', 'T' )
,( 21, null, null, 'NULL', 'NULL' )
,( 22, 5000, 54321, 'A', 'T' )
,( 23, 5001, 54321, 'A', 'T' )
,( 24, 5002, 54321, 'A', 'T' )
,( 25, 5003, 54321, 'A', 'T' )
,( 26, 5004, 54321, 'A', 'T' )
,( 27, 5005, 54321, 'A', 'T' )
,( 28, 5006, 54321, 'A', 'T' )
,( 29, 5007, 54321, 'A', 'T' )
,( 30, 5008, 54321, 'A', 'T' )
,( 31, 5009, 54321, 'A', 'T' )
,( 32, 5010, 54321, 'P', 'T' )
,( 33, 5011, 54321, 'P', 'T' )
,( 34, 5012, 54321, 'P', 'T' )
,( 35, 5013, 54321, 'P', 'T' )
,( 36, 5014, 54321, 'P', 'T' )
,( 37, 5015, 54321, 'P', 'T' )
,( 38, 5016, 54321, 'P', 'T' )
,( 39, 5017, 54321, 'P', 'T' )
,( 40, 5018, 54321, 'P', 'T' )
,( 41, 5019, 54321, 'P', 'T' )
-- --------------------------------------------------------------------------------
-- Inserts for TVisitTypes
-- --------------------------------------------------------------------------------
INSERT INTO TVisitTypes ( intVisitTypeID, strVisitDesc )
VALUES ( 1, 'Screening' )
,( 2, 'Randomization' )
,( 3, 'Withdrawal' )
-- --------------------------------------------------------------------------------
-- Inserts for TPatientVisits
-- --------------------------------------------------------------------------------
INSERT INTO TPatientVisits ( intVisitID, intPatientID, dtmVisit, intVisitTypeID,intWithdrawReasonID )
VALUES ( 1, 1, '01/01/2017', 1, 1 )
,( 2, 2, '02/01/2017', 2, 2 )
,( 3, 3, '03/01/2017', 3, 3 )
,( 4, 4, '04/01/2017', 1, 4 )
,( 5, 5, '05/01/2017', 2, 5 )
,( 6, 6, '06/01/2017', 3, 6 )
,( 7, 7, '07/01/2017', 1, 1 )
,( 8, 8, '08/01/2017', 2, 2 )
,( 9, 9, '09/01/2017', 3, 3 )
,( 10, 10, '10/01/2017', 1, 4 )
,( 11, 11, '11/01/2017', 2, 5 )
,( 12, 12, '12/01/2017', 3, 6 )
-- --------------------------------------------------------------------------------
-- Inserts for TDrugKits
-- --------------------------------------------------------------------------------
INSERT INTO TDrugKits ( intDrugKitID, intSiteID, strTreatment, intVisitID )
VALUES ( 10000, 101, 'A', 'NULL' )
,( 10001, 101, 'A', 'NULL' )
,( 10002, 101, 'A', 'NULL' )
,( 10003, 101, 'A', 'NULL' )
,( 10004, 101, 'P', 'NULL' )
,( 10005, 101, 'P', 'NULL' )
,( 10006, 101, 'P', 'NULL' )
,( 10007, 101, 'P', 'NULL' )
,( 10008, 111, 'A', 'NULL' )
,( 10009, 111, 'A', 'NULL' )
,( 10010, 111, 'A', 'NULL' )
,( 10011, 111, 'A', 'NULL' )
,( 10012, 111, 'P', 'NULL' )
,( 10013, 111, 'P', 'NULL' )
,( 10014, 111, 'P', 'NULL' )
,( 10015, 111, 'P', 'NULL' )
,( 10016, 121, 'A', 'NULL' )
,( 10017, 121, 'A', 'NULL' )
,( 10018, 121, 'A', 'NULL' )
,( 10019, 121, 'A', 'NULL' )
,( 10020, 121, 'P', 'NULL' )
,( 10021, 121, 'P', 'NULL' )
,( 10022, 121, 'P', 'NULL' )
,( 10023, 121, 'P', 'NULL' )
,( 10024, 131, 'A', 'NULL' )
,( 10025, 131, 'A', 'NULL' )
,( 10026, 131, 'A', 'NULL' )
,( 10027, 131, 'A', 'NULL' )
,( 10028, 131, 'P', 'NULL' )
,( 10029, 131, 'P', 'NULL' )
,( 10030, 131, 'P', 'NULL' )
,( 10031, 131, 'P', 'NULL' )
,( 10032, 141, 'A', 'NULL' )
,( 10033, 141, 'A', 'NULL' )
,( 10034, 141, 'A', 'NULL' )
,( 10035, 141, 'A', 'NULL' )
,( 10036, 141, 'P', 'NULL' )
,( 10037, 141, 'P', 'NULL' )
,( 10038, 141, 'P', 'NULL' )
,( 10039, 141, 'P', 'NULL' )
,( 10040, 501, 'A', 'NULL' )
,( 10041, 501, 'A', 'NULL' )
,( 10042, 501, 'A', 'NULL' )
,( 10043, 501, 'A', 'NULL' )
,( 10044, 501, 'P', 'NULL' )
,( 10045, 501, 'P', 'NULL' )
,( 10046, 501, 'P', 'NULL' )
,( 10047, 501, 'P', 'NULL' )
,( 10048, 511, 'A', 'NULL' )
,( 10049, 511, 'A', 'NULL' )
,( 10050, 511, 'A', 'NULL' )
,( 10051, 511, 'A', 'NULL' )
,( 10052, 511, 'P', 'NULL' )
,( 10053, 511, 'P', 'NULL' )
,( 10054, 511, 'P', 'NULL' )
,( 10055, 511, 'P', 'NULL' )
,( 10056, 521, 'A', 'NULL' )
,( 10057, 521, 'A', 'NULL' )
,( 10058, 521, 'A', 'NULL' )
,( 10059, 521, 'A', 'NULL' )
,( 10060, 521, 'P', 'NULL' )
,( 10061, 521, 'P', 'NULL' )
,( 10062, 521, 'P', 'NULL' )
,( 10063, 521, 'P', 'NULL' )
,( 10064, 531, 'A', 'NULL' )
,( 10065, 531, 'A', 'NULL' )
,( 10066, 531, 'A', 'NULL' )
,( 10067, 531, 'A', 'NULL' )
,( 10068, 531, 'P', 'NULL' )
,( 10069, 531, 'P', 'NULL' )
,( 10070, 531, 'P', 'NULL' )
,( 10071, 531, 'P', 'NULL' )
,( 10072, 541, 'A', 'NULL' )
,( 10073, 541, 'A', 'NULL' )
,( 10074, 541, 'A', 'NULL' )
,( 10075, 541, 'A', 'NULL' )
,( 10076, 541, 'P', 'NULL' )
,( 10077, 541, 'P', 'NULL' )
,( 10078, 541, 'P', 'NULL' )
,( 10079, 541, 'P', 'NULL' )
-- --------------------------------------------------------------------------------
-- Inserts for TWithdrawReasons
-- --------------------------------------------------------------------------------
INSERT INTO TWithdrawReasons ( intWithdrawReasonID, strWithdrawDesc )
VALUES (1, 'Patient withdrew consent' )
,(2, 'Adverse event' )
,(3, 'Health issue-related to study' )
,(4, 'Health issue-unrelated to study' )
,(5, 'Personal reason' )
,(6, 'Completed the study' )
答案 0 :(得分:1)
例如,大多数公司不允许在Customer表中不存在CustomerID的情况下输入订单。大多数学校不允许在学生和班级表中各自的身份证不同的情况下发生学生级别的学习。
答案 1 :(得分:0)
如果按此顺序运行这些查询,则首先将数据放入TPatients,然后放入TRandomCodes。但是在创建外键约束的第一部分中,为TPatients表中的intRandomCodeID列添加外键约束,以引用TRandomCodes表中的intRandomCodeID。当您将数据放入TPatients表时,服务器会检查TRandomCodes表中是否存在intRandomCodeID列的数据,因为存在约束。当服务器发现不存在这样的行(数据)时,它会抛出错误。