-1

I have created tables in SQL as shown below, but when I try to insert values, I get the following error and I'm not sure why:

SQL Error [1452] [23000]: Cannot add or update a child row: a foreign key constraint fails (N01581969.Attending, CONSTRAINT Attending_ibfk_1 FOREIGN KEY (patientID) REFERENCES Patient (patientID))

Physician table

CREATE TABLE Doctor 
(
    employeeID INT PRIMARY KEY AUTO_INCREMENT, 
    first_name varchar (255),
    last_name varchar (255), 
    position varchar (255)
);

Patients

CREATE TABLE Patient 
(
    patientID INT PRIMARY KEY AUTO_INCREMENT, 
    first_name varchar (255),
    last_name varchar (255), 
    HCN INT, 
    PCP varchar(255)
); 

Nurses

CREATE TABLE Nurse 
(
    nurseID INT PRIMARY KEY AUTO_INCREMENT,  
    first_name varchar (255),
    last_name varchar (255), 
    n_position varchar (255)
);

Medical History

CREATE TABLE MedicalHistory 
(
    historyID INT PRIMARY KEY AUTO_INCREMENT, 
    med_condition varchar (255),
    medication varchar (255)
);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tara
  • 15
  • 3
  • 1
    Did you create the Doctor table? Can you do `SHOW CREATE TABLE Doctor` ? It might help to see your insert statement too. – blobtub Dec 12 '22 at 03:43
  • 2
    We need to see your insert statement so we can work out which table has the constraint that's causing your error. Take out any personal information though. – blobtub Dec 12 '22 at 04:23
  • Debug questions require a [mre]--cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. [ask] [Help] When you get a result you don't expect, pause your overall goal, chop to the 1st subexpression with unexpected result & say what you expected & why, justified by documentation. (Debugging fundamental.) – philipxy Dec 12 '22 at 05:58

1 Answers1

0

I'm not sure which table you are adding a value with, but it looks like you're adding one in the first table. If you are, one reason could be because patient_id is supposed to be auto increment so you don't have to add value to it. When you are inserting value to that table you will start at first_name instead of the patient_id.

Selaka Nanayakkara
  • 3,296
  • 1
  • 22
  • 42