如图,经我测试,错误应该发生在c_commodity char(30), foreign key(c_commodity) references stock(commodity),这句上,但是我找不出错误,求指导。这图是stock的结构。

解决方案 »

  1.   

    请检查以关联相关的两个表创建时的先后顺序。务必先创建主表、后创建从表。
    此外,着重检查
    1)字符集
    2)外键列是否有默认值以下是常见的10个错误原因,    The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
        One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
        The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this. (Thanks to Niels for this tip)
        One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
        You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL.  You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
        Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns. (Thanks to FRR for this tip)
        You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
        One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint. (Thanks to Alex for this tip)
        You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)10.  The name of your foreign keyexceeds the max length of 64 chars.  (Thanks to Nyleta for the tip)
      

  2.   

    http://wenku.baidu.com/view/7a0a1f1b10a6f524ccbf85e6.html
    慢慢检查  和字符集没啥关系
      

  3.   

    字符集的处理方式:
    ALTER TABLE `xxx` CONVERT TO CHARACTER SET utf8;由字符集引起的error 150 常见于这种场景:
      已经部署的系统在升级过程中,引入了新表。引入的表与运行实例的某一个表出现了关联。但是由于blabla...原因,我们之前的系统并未同一字符集。
      可能在调试过程中,用 show create table "xxx"(主表名、历史表)。获取了该表的字符编码与现有建表的字符编码不一致。  话说,如果不是编码引起的。你会如何调试error 150 错误。一般情况下,我的做法是:
    找出与此错误相关联的表。然后按业务流程依据时序对实体进行排序,之后再进行字段类型、
    字段长度、blabla...的检测。
      不过看你的代码上大致是客户订单与库存进行关联。貌似是进销存系统。关联的范围先缩小再复核。
      
      

  4.   

    谢谢,确实是字符集问题,我把库设成gbk就好了。
      

  5.   

    类似问题,下次直接检查 show create table ...就会很容易看到问题了。