简体   繁体   English

MySQL InnoDB外键问题

[英]MySQL InnoDB Foreign Key Problems

I have the following tables and I am trying to run them in MySQL but I keep getting an errno 150. Just not sure why, but MySQL cannot seem to create the foreign key constraint. 我有下面的表,我试图在MySQL中运行它们,但我不断得到errno150。只是不确定为什么,但是MySQL似乎无法创建外键约束。 I have already looked at the rules for setting FKs for InnoDB and everything seems ok. 我已经看过为InnoDB设置FK的规则,一切似乎都还可以。 Could someone please lend me another set of eyes and expertise? 有人可以借给我另一套眼睛和专业知识吗?

-- Table publication_type (parent table)
CREATE TABLE publication_type (
  id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  publication_type varchar(55)  NOT NULL,
  tstamp timestamp  NOT NULL  DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT publication_type_pk PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

-- Table publication (child table)
CREATE TABLE publication (
  id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  authors varchar(255)  NOT NULL,
  publication_title varchar(100)  NOT NULL,
  publication_type_id int(11) UNSIGNED NOT NULL,
  user_id int(11) UNSIGNED NOT NULL,
  CONSTRAINT publication_pk PRIMARY KEY (id),
  CONSTRAINT publication_type_fk FOREIGN KEY (publication_type_id)  REFERENCES publication_type(id) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

The types do not match: 类型不匹配:

publication_type.id is of type int UNSIGNED publication_type.id的类型为int UNSIGNED

publication.publication_type_id is of type int publication.publication_type_id的类型为int

See also the docs: http://dev.mysql.com/doc/refman/5.1/en/create-table-foreign-keys.html , specifically: 另请参阅文档: http : //dev.mysql.com/doc/refman/5.1/en/create-table-foreign-keys.html ,特别是:

Corresponding columns in the foreign key and the referenced key must have similar data types. 外键和引用键中的对应列必须具有相似的数据类型。 The size and sign of integer types must be the same . 整数类型的大小和符号必须相同 The length of string types need not be the same. 字符串类型的长度不必相同。 For nonbinary (character) string columns, the character set and collation must be the same. 对于非二进制(字符)字符串列,字符集和排序规则必须相同。

emphasis mine. 强调我的。

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

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