【发布时间】:2018-04-03 18:03:32
【问题描述】:
我现在正在尝试创建 SQL Server 数据库,但我遇到了似乎无法解决的错误。我是一名学生,试图通过完成我在互联网上找到的作业来提高自己,这让我很难过。以下是我在部分数据库中遇到的错误
消息 547,第 16 级,状态 0,第 221 行
INSERT 语句与 FOREIGN KEY 约束“TPatients_TRandomCodes_FK”冲突。冲突发生在数据库“Final”、表“dbo.TRandomCodes”、列“intRandomCodeID”中。消息 547,第 16 级,状态 0,第 294 行
INSERT 语句与 FOREIGN KEY 约束“TPatientVisits_TPatients_FK”冲突。冲突发生在数据库“Final”、表“dbo.TPatients”、列“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' )
【问题讨论】:
标签: sql sql-server alter-table insert-into