mysql - 如何在 MariaDB 10.1.37 / Ver 15.1 中进行适当的约束?
问题描述
我正在使用 MariaDB,但在引入约束时遇到了麻烦。
我的 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)
);
我的约束条目:
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.
感谢您的见解。
编辑:添加内容。更正了错误消息。
解决方案
以下工作:
CREATE INDEX CustomerName ON customer (customerName);
显示引擎 INNODB 状态;帮助很大。
推荐阅读
- javascript - 如何在 Internet Explorer 中下载时显示对话框,如 FireFox 所示?
- asp.net - 使用 DataGrid 上的更新/删除命令更新会话数组列表
- debugging - 如何在谷歌操作日志中获取堆栈跟踪,因为必须设置“final_response”是无用的
- javascript - Symfony 中的日期范围过滤器使用 fosjsrouting Bundle
- assembly - 如何在 PIC24F 汇编代码中实现循环
- sql-server - 如何在动态sql中使用表变量?或者从用户定义的表类型创建临时表?
- javascript - 在另一个文本框或 div 中写入内容后清除一个文本框或 div
- field - 在主题中显示自定义字段 (processwire)
- powershell - 运行后台作业的 PowerShell 任务 (Start-Job)
- r - R的省略号忽略了传递的参数