【问题标题】:Setting up foreign key constraint in MySQL for use with Yii framework在 MySQL 中设置外键约束以与 Yii 框架一起使用
【发布时间】:2011-07-23 04:58:21
【问题描述】:

所以——我有两张表要关联:锦标赛和比赛。它们可以概括如下:

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)

我正在尝试使用以下 SQL 在匹配表上添加外键约束:

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

但是,我从 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

我在 MySQL 网站上查看了添加 FK 约束的语法,一切都对我来说是正确的。有什么想法吗?

第一个建议(manuelpedrera):

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

结果:

#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

【问题讨论】:

    标签: php mysql sql yii constraints


    【解决方案1】:

    原来“匹配”是一个保留字。

    【讨论】:

      【解决方案2】:

      引用表格时不能使用正则引号。

      而不是ALTER TABLE 'match'

      使用

      ALTER TABLE match 
      

      ALTER TABLE `match`
      

      编辑 1:

      试试这个

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

      【讨论】:

      • 您能否发布您在更改后使用的完整 SQL 查询?
      • 尝试了您的编辑:#1005 - 无法创建表 'sarena.#sql-1618_1b1' (errno: 150) (详情...)
      • 这很奇怪,我已经用我自己的 phpMyAdmin 试过了,它确实有效。确保您的表设置为 InnoDB 而不是 MyISAM,然后重试!
      • 好吧,我不知道那是什么问题。我会检查表名和字段名是否正常。如果您使用的是 phpMyAdmin,您还可以通过单击表格然后在底部的 Relationships 视图 链接中手动建立关系...
      • Relationships 视图在我的 PhpMyadmin 中被禁用,但表名和列名是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多