2014-04-13 4 views
-2

여기 내 테이블이 있는데이 오류가 계속 발생합니다. 내가 밑바닥에 굵은 글씨로 쓴 코드를 실행할 때까지는 모든 것이 좋다. 임씨는이 테이블을 완료하는 데 정말로 가깝지만 필자는이 오류를 해결하기 위해 모든 것을 시도했지만 계속오고 있습니다.MySql Workbench 오류 1452가 외래 키 제약 조건이 실패한 하위 행을 추가하거나 업데이트 할 수 없습니다.

You` 감사 나에게

도와주세요

나는 당신의 남자의 제안을 가져다가 삽입 된 데이터가 이제 열 나던 일치 값 오류 1136를 받고 메신저 행 1 셀 변경

UPDATE하는 ID를 방문하므로 데이터 유형 문제는 있지만 모든 것을 시도한 결과 알 수 없습니다. 닫기가 완료 될 예정입니다.

제발 도와주세요!

Create Schema Visit; 


create table roomtableS(
RoomID char (2)  not null, 
RoomNum char (2) not null, 
Charge integer not null, 
CONSTRAINT RoomTable_PK Primary Key(RoomID)); 


Insert into roomtableS values 
('01','1A',125.00), 
('02','1A',150.00), 
('03','1A',100.00), 
('04','1A',200.00), 
('05','2B',150.00), 
('06','2B',125.00), 
('07','3C',200.00), 
('08','3C',125.00), 
('09','3C',100.00); 





SELECT * FROM ROOMTABLES; 


create table PATIENT(
PatientID char(5) not null, 
PatientName Char(25) not null, 
PatientEmail  Char(30) null, 
PatientPhoneNumber Char(10) null, 
PatientAddress Char(100) null, 
constraint PATIENT_PK Primary key(PatientID)); 

insert PATIENT values 
('P1', 'Bruce Willis', '[email protected]', '2022223333', '1111 Cosmic dr'), 
('P2', 'Demi Moore', '[email protected]', '2021113333', '1112 Cosmic dr'), 
('P3', 'Andre Agassi', '[email protected]', '2023333333', '1113 Cosmic dr'), 
('P4', 'Jet Lee', '[email protected]', '2023334444', '1114 Chinatown ct'), 
('P5', 'Jim Carey', '[email protected]', '2023335555', '1115 United dr'), 
('P6', 'Bruce Lee', '[email protected]', '2023336666', '1115 Chinatown ct'); 

select* From PATIENT; 




Create table SERVICETable(
ServiceID  Char (5) not null, 
ServiceTreatment Char(25) not null, 
ServiceCost  numeric not null, 
constraint SERVICE_PK Primary Key(ServiceID)); 

insert SERVICETable values 
('S1','Sore throat', 10.00), 
('S2', 'Fever', 15.00), 
('S3', 'Headache', 10.00), 
('S4', 'Blood pressusre', 20.00), 
('S5', 'Yearly checkup', 30.00), 
('S6', 'Common cold', 15.00); 

select* from SERVICETable; 


Create Table doctortable(
DocID char (5) NOT NULL, 
DoctorFirstName char(15) Not NULL, 
DoctorLastName char (15) Not Null, 
DoctorPhone char (15) Not Null, 
CONSTRAINT DoctorTable_PK Primary Key(DocID)); 


INSERT INTO doctortable values 
('D1','Tim','Edward','555-123-4567'), 
('D2','Andy','Smith','888-777-6666'), 
('D3','John','Smith','222-321-7654'); 






Select * From doctortable; 



Create Table visit(
VisitID char (2) not Null, 
PatientID Char (5) not null, 
DocID Char (5) not null, 
ServiceID Char (5) not Null, 
RoomID char (2) not Null, 
Visit date not null, 
CONSTRAINT VISIT_PK PRIMARY KEY (VisitID)); 



Alter table Visit 
add foreign key (PatientID) 
references Patient (PatientID); 


Alter table Visit 
add foreign key (DocID) 
references doctortable (DocID); 

Alter table Visit 
add foreign key (ServiceID) 
references ServiceTable (ServiceID); 

Alter table Visit 
add foreign key (RoomID) 
references roomtableS (RoomID); 


Insert into Visit (VisitID,RoomID,ServiceID,PatientID,Visit) values 
**('1','P1','D1','S1','05','2014-01-03'), 
('2','P2','D2','S2','01','2014-01-10'), 
('3','P3','D1','S3','02','2014-01-10'), 
('4','P4','D2','S4','07','2014-01-15'), 
('5','P1','D3','S2','08','2014-01-10'), 
('6','P5','D3','S5','03','2014-02-02'), 
('7','P4','D1','S6','06','2014-01-10'), 
('8','P3','D2','S5','03','2014-02-03'), 
('9','P2','D3','S6','01','2014-02-04'), 
('10','P3','D1','S2','06','2014-02-04'), 
('11','P5','D2','S4','04','2014-02-05'), 
('12','P4','D1','S5','09','2014-02-06');** 
Select * from Visit; 

감사합니다!

+1

잊어 버렸습니다. INTO -> INSERT INTO 방문 VALUE ... –

+0

은 여전히 ​​저에게 같은 오류를줍니다. – user3529435

+0

@ INKIO : INTO는 선택 사항입니다. 그러나 플랫폼 간 호환성을 위해 사용하는 것이 좋습니다. –

답변

0

Visit 테이블로 삽입 쿼리는 다음과 같다 :

Insert Visit Values 
('1','P1','D1','S1','2B','2014-01-03'), 

값은

(VisitID, RoomID, ServiceID, PatientID, DocID, Visit) 

의 순서로 예상하지만 다른 순서로 보일 수 있습니다.

을 포함하도록 insert 문을 변경하십시오.

Insert Visit 
(VisitID, PatientID, DocID, ServiceID, RoomID, Visit) 
Values 
('1','P1','D1','S1','2B','2014-01-03'), 

그리고 값이 '2B''RoomID' 열에 삽입되는 값은 'RoomNum' 아니지만 'RoomID' 값 것으로 보인다. 숫자를 '05' 또는 '06'으로 변경하십시오. 이러한 변경은 열에 대한 다른 모든 입력에서 수행되어야합니다.

+0

나는 당신이 나에게 말했던 것을했지만 이제는 1136 번 에러가 발생합니다. 이는 열 개수가 1 번 방문 ID와 일치하지 않는다고 말합니다. 따라서 ID가 데이터 유형이지만 ive가 모든 것을 시도 했으므로 동일한 오류가 계속 발생합니다. – user3529435

+0

내 'Insert'문을 살펴보십시오. 입력 된 열 수와 일치하는 정확한 수의 열이 있습니다. 너는 어딘가에서 누락 될 수있다. 그것을 게시하십시오. –

관련 문제