如何在MariaDB 10.1.37 / Ver 15.1中进行适当的约束?

时间:2019-02-01 19:01:03

标签: mysql mariadb

我正在使用MariaDB,在引入CONSTRAINT时遇到麻烦。

我的MariaDB版本:

Ver 15.1 Distrib 10.1.37-MariaDB

我的错误消息:

ERROR 1005 (HY000): Can't create table `schedulingGUI`.`#sql-1043_1a` (errno: 150 "Foreign key constraint is incorrectly formed")

我在城市的表格条目:

CREATE TABLE city
(
  cityId INT unsigned NOT NULL AUTO_INCREMENT,
  city VARCHAR(50),
  countryId INT unsigned,
  customerName VARCHAR(50),
  address VARCHAR(50),
  postalCode VARCHAR(50),
  phone VARCHAR(50),
  createDate VARCHAR(50),
  createdBy VARCHAR(50),
  lastUpdateBy VARCHAR(50),
  PRIMARY KEY (cityId)
);

我的客户表条目:

CREATE TABLE customer
(
  customerId INT unsigned NOT NULL AUTO_INCREMENT,
  customerName VARCHAR(50),
  addressId INT unsigned,
  active INT unsigned,
  address VARCHAR(50),
  city VARCHAR(50),
  postalCode VARCHAR(50),
  phone VARCHAR(50),
  createDate VARCHAR(50),
  createdBy VARCHAR(50),
  lastUpdateBy VARCHAR(50),
  PRIMARY KEY (customerId)
);

我的CONSTRAINT条目:

ALTER TABLE city
  ADD CONSTRAINT customerNameChange01
  FOREIGN KEY (customerName)
  REFERENCES customer (customerName)
  ON UPDATE CASCADE
  ON DELETE CASCADE;

我最近偶然发现了SHOW ENGINE INNODB STATUS。它指出以下内容:

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

感谢您的见识。

编辑:添加了内容。更正了错误消息。

1 个答案:

答案 0 :(得分:1)

以下工作有效:

CREATE INDEX CustomerName ON customer (customerName);

SHOW ENGINE INNODB的状态; <​​/ strong>提供了很多帮助。