0

I have child table my_contacts and parent table profession. profession table is created with this code:

CREATE TABLE profession(
    prof_id INT NOT NULL,
    profession VARCHAR(20) NOT NULL
);

now I am trying to add a foreign key in my_contacts table by this command:

ALTER TABLE my_contacts
ADD COLUMN prof_id INT NOT NULL,
ADD CONSTRAINT profession_prof_id_fk
FOREIGN KEY (prof_id)
REFERENCES profession (prof_id);

I get this error: Error 1052 (23000): Cannot add or update a child row: a foreign key constraint fails.

hassan javed
  • 25
  • 1
  • 8

1 Answers1

0

I removed the prof_id column which was foreign key in my_contacts table and added it as a column which can accept null values and I could make it a foreign key.

hassan javed
  • 25
  • 1
  • 8