donglu9445 2015-03-24 14:58
浏览 84
已采纳

MySQL - 无法添加外键约束 - 拉丁语/西里尔语

This is my last resort, since I have tried everything I could find on Google and here on Stack Overflow.

I have thoroughly searched for an answer before asking this question. I have found the people have been getting the same error message from MySQL, but their solution did not worked for me, because I am not doing that particular thing (different column types) wrong.

I have a problem with foreign keys, where they are the same type (varchar(xx), where xx is same length in both cases; NOT NULL), but still gives me an error, which is described by the PHPMyAdmin's command SHOW ENGINE INNODB STATUS as:

2015-03-24 16:04:21 66c Error in foreign key constraint of table arhiva/privilegija:
FOREIGN KEY (korisnik_ime) REFERENCES korisnik(ime) ,
FOREIGN KEY (firma_naziv) REFERENCES firma(naziv)
):
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.

Here is my database schema, sorry for the non English names (by the way, the first time I encountered this problem the column names were in Cyrillic characters, but even after I converted them in Latin - what you are seeing here - my problem was not solved):

DROP DATABASE IF EXISTS arhiva;
CREATE DATABASE arhiva
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_general_ci;
USE arhiva;

CREATE TABLE `korisnik` (
  ime VARCHAR(30) NOT NULL ,
  lozinka CHAR(60) NOT NULL
);

CREATE TABLE `firma` (
  naziv VARCHAR(50) NOT NULL
);

CREATE TABLE `privilegija` (
  korisnik_ime VARCHAR(30) NOT NULL ,
  firma_naziv VARCHAR(50) NOT NULL ,
  pristap CHAR(1) NOT NULL ,
  FOREIGN KEY (korisnik_ime) REFERENCES korisnik(ime) ,
  FOREIGN KEY (firma_naziv) REFERENCES firma(naziv)
);

Can you please guide me in the right direction of solving this problem. I thought that using Cyrillic characters was the problem, but it turns out that the problem lies somewhere else.

  • 写回答

1条回答 默认 最新

  • dongle19863 2015-03-24 15:06
    关注

    add PRIMARY KEY (), like this:

    CREATE TABLE `korisnik` (
    ime VARCHAR(30) NOT NULL ,
    lozinka CHAR(60) NOT NULL,
    PRIMARY KEY(ime)
    );
    
    CREATE TABLE `firma` (
    naziv VARCHAR(50) NOT NULL,
    PRIMARY KEY(naziv)
    );
    

    Now you have an index so the foreign key constraint should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100