Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up foreign key constraint in MySQL for use with Yii framework

So -- I have two tables that I'm trying to relate: tournament and match. They can be outlined as followed:

tournament   
    -id (int)   
    -league (varchar)   
    -status (varchar)  
    -create_time (datetime)   
    -update_time (datetime)

match   
    -id (int)   
    -tournament_id (int)   
    -status (varchar)  
    -create_time (datetime)   
    -update_time (datetime)

I'm attempting to add a foreign key constraint on the match table with the following SQL:

ALTER TABLE 'match' ADD CONSTRAINT
('FK_match_tournament') FOREIGN KEY
('tournament_id') REFERENCES
'tournament' ('id') ON DELETE CASCADE
ON UPDATE RESTRICT;

however, I am getting the following error message from MySQL:

#1064 - 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
''match' ADD CONSTRAINT ('FK_match_tournament') FOREIGN KEY ('tournament_id') REF'
at line 1

I looked at the syntax for adding FK constraints on the MySQL website, and everything looks right to me. Any ideas?

First Suggestion (manuelpedrera):

ALTER TABLE `match` ADD CONSTRAINT ('FK_match_tournament') FOREIGN KEY ('tournament_id') REFERENCES `tournament` ('id') ON DELETE CASCADE ON UPDATE RESTRICT;

Results:

#1064 - 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 '('FK_match_tournament') FOREIGN KEY ('tournament_id') REFERENCES `tournament` ('' at line 1
like image 400
ThinkingInBits Avatar asked Dec 28 '25 22:12

ThinkingInBits


1 Answers

Turns out 'match' is a reserved word.

like image 137
ThinkingInBits Avatar answered Dec 30 '25 14:12

ThinkingInBits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!