Foreign key error
I am getting this error when trying to add any data in the myphpadmin.
#1452 - Cannot add or update a child row: a foreign key constraint fails (`artdb/image`, CONSTRAINT `image_ibfk_1` FOREIGN KEY (`artid`) REFERENCES `artist` (`artid`))
I am using only one field to input so I know that I am using a valid artid.
After playing with around it seems as though I can only enter 1 artid and the rest gets kicked out, but that is only if I go directly thru the Insert on myphpadmin. If I re-add the tables and go with a different artid it works only for that one.
Here are the 2 tables I am working with -
I understand a bit of the error msg but not this part - `image_ibfk_1`
CREATE TABLE artist (artid varchar(11) NOT NULL PRIMARY KEY,
fullName tinytext Not Null,
firstName tinytext NULL,
middleName tinytext NULL,
lastName tinytext NULL,
otherName tinytext NULL,
called tinytext NULL,
dob_xt tinytext NULL,
dob tinytext NULL,
dod_xt tinytext NULL,
CREATE TABLE image (imgid varchar(11) NOT NULL PRIMARY KEY,
artid varchar(11) NOT NUll,
FOREIGN KEY (artid) REFERENCES artist (artid)
I am not really sure that I need to use a Foreign Key. But I can't get a query to work and was thinking this may be the issue.
I think that "image_ibfk_1" is the internally generated name given to the Foreign key constraint.
You must have a row in the artist table with an artID equal to the artID of the row you are inserting into the image table.
It's exactly that.
The role of the foreign key is to ensure that the information you enter correspond to something that is already existing.
In your case, the insert into the table image has an artId that does not exist in the artist table, making the exception fire.
You have to have an artist declared, before you can refer to it.
You don't need an FK if you want to allow your data to be corrupted.
A foreign key is an object, and as such, it needs a name. You didn't give it one, so the database did. Now, with its name, you can alter or drop it.