I’ve tried to create a new table relation but I seem to have gotten an error for which I’m not sure why. The first relation (one to many) I made was last week and it works good, maybe I forgot the correct procedure since then?
Simple explanation:
I have three tables – events, music_styles, venues
So far, I correctly linked events and venues in the way that one venue can have multiple events linked to it. I tried making a new table that has rows for each musical style and an ID that would be used in table ‘events’ so that I can link each event to a musical style id.
However this is the error I got:
SQL query:
ALTER TABLE
eventsADD FOREIGN KEY (MUSIC_STYLE_ID) REFERENCES
nightl7_complete.music_styles(MUSIC_ID) ON DELETE NO ACTION
ON UPDATE NO ACTION ;MySQL said: Documentation
1452 – Cannot add or update a child row: a foreign key constraint
fails (
nightl7_complete/#sql-2721_c0dcfd, CONSTRAINT
#sql-2721_c0dcfd_ibfk_2FOREIGN KEY (MUSIC_STYLE_ID) REFERENCES
music_styles(MUSIC_ID) ON DELETE NO ACTION ON UPDATE NO ACTION)
Documentation Error ALTER TABLEeventsADD FOREIGN KEY (
MUSIC_STYLE_ID) REFERENCESnightl7_complete.music_styles(
MUSIC_ID) ON DELETE NO ACTION ON UPDATE NO ACTION ;
And this is the procedure I did 🙂

I uploaded a full sized image here
Thanks everyone 🙂
Edit:
So I did this:
$query = 'SELECT e.* FROM events e '.
'LEFT JOIN nightl7_complete.music_styles ms ON ms.ID = e.MUSIC_STYLE_ID'.
'WHERE ms.id IS NULL';
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
and got this:
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ms.id IS NULL’ at line 1
Don’t you want to link
MUSIC_STYLE_IDtoID, notMUSIC_ID?Also, you need to either pre-populate
MUSIC_STYLE_IDwith valid ids frommusic_styles, or set them to null before creating the foreign key.To verify this, try:
If that query returns any records, those are entries in
eventsthat don’t contain corresponding records inmusic_styles