简体   繁体   English

在MySQL中设置外键约束以与Yii框架一起使用

[英]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: 我正在尝试使用以下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: 但是,我从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. 我查看了在MySQL网站上添加FK约束的语法,一切看起来都对我而言。 Any ideas? 有任何想法吗?

First Suggestion (manuelpedrera): 第一个建议(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

原来'匹配'是一个保留字。

You cannot use regular quotes when referring to a table. 引用表时,不能使用常规引号。

Instead of ALTER TABLE 'match' 而不是ALTER TABLE 'match'

use 采用

ALTER TABLE match 

or 要么

ALTER TABLE `match`

Edit 1: 编辑1:

Try with this 试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM