尝试添加外键关系时 mysql 中的外键约束

Foreign key constrains in mysql when trying to add foreign key relationship

我有两个 table 叫 activity 和用户。我需要与这两个 table 建立外键关系。 username 是我的用户 table 的主键,它是一个 varchar 值。但是当我试图建立关系时,我得到一个外键约束错误,错误代码为 mysql 1215。下面是我提到的两个 table 相关的 DDL 语句。

谁能帮我解决这个问题。

谢谢

CREATE TABLE `user` (
  `username` varchar(50) NOT NULL,
  `user_id` int(11) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `password` varchar(500) DEFAULT NULL,
  `activated` tinyint(1) DEFAULT '0',
  `activationkey` varchar(50) DEFAULT NULL,
  `resetpasswordkey` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`username`),
  UNIQUE KEY `user_id_UNIQUE` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE activity (
  activity_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  activity_type_id     INT UNSIGNED,
  activity_property_id  INT UNSIGNED,
  added_by             varchar(50),
  updated_by           varchar(50),
  activity_code        VARCHAR(50),
  activity_description VARCHAR(100),
  start_date           TIMESTAMP NULL,
  end_date             TIMESTAMP NULL,
  start_time           VARCHAR(10),
  end_time             VARCHAR(10),
  added_date           TIMESTAMP NULL,
  updated_date         TIMESTAMP NULL,
  is_available         TINYINT(1),
  PRIMARY KEY (activity_id),

  CONSTRAINT fk_ADDED_BY_FOR_ACTIVITY FOREIGN KEY (added_by) REFERENCES user (username),
  CONSTRAINT fk_UPDATED_BY_FOR_ACTIVITY FOREIGN KEY (updated_by) REFERENCES user (username)
)
  ENGINE = InnoDB;

只需添加 DEFAULT CHARSET=latin1。试试下面的查询。

    CREATE TABLE activity (
  activity_id          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  activity_type_id     INT UNSIGNED,
  activity_property_id  INT UNSIGNED,
  added_by             VARCHAR(50) DEFAULT NULL,
  updated_by           VARCHAR(50) DEFAULT NULL,
  activity_code        VARCHAR(50),
  activity_description VARCHAR(100),
  start_date           TIMESTAMP NULL,
  end_date             TIMESTAMP NULL,
  start_time           VARCHAR(10),
  end_time             VARCHAR(10),
  added_date           TIMESTAMP NULL,
  updated_date         TIMESTAMP NULL,
  is_available         TINYINT(1),
  PRIMARY KEY (activity_id),
  CONSTRAINT fk_ADDED_BY_FOR_ACTIVITY FOREIGN KEY (added_by) REFERENCES user (username),
  CONSTRAINT fk_UPDATED_BY_FOR_ACTIVITY FOREIGN KEY (updated_by) REFERENCES user (username)
)
  ENGINE = InnoDB DEFAULT CHARSET=latin1;

您的错误是 FK 列类型与 PK 列类型不匹配。特别是,字符集不同。阅读有关外键的文档部分。

13.1.18.6 Using FOREIGN KEY Constraints

  • 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.

所以activity需要

ENGINE=InnoDB DEFAULT CHARSET=latin1;

你还会发现

In addition to SHOW ERRORS, in the event of a foreign key error involving InnoDB tables (usually Error 150 in the MySQL Server), you can obtain a detailed explanation of the most recent InnoDB foreign key error by checking the output of SHOW ENGINE INNODB STATUS.