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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据