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

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.

这是我的最后一招,因为我已经尝试了一些我可以在谷歌和Stack Overflow上找到的东西。 p>

在提出这个问题之前,我已经彻底搜索了一个答案。 我发现人们从MySQL那里得到了同样的错误信息,但是他们的解决方案不 strong>对我有用,因为我没有做那个特定的事情(不同的列类型)错误。 p>

我的外键有问题, strong>类型相同( varchar(xx) code>,其中xx长度相同 在这两种情况下; NOT NULL),但仍然给我一个错误,由PHPMyAdmin的命令 SHOW ENGINE INNODB STATUS code>描述为: p>

2015-03-24 16:04:21 66c表arhiva / privilegija的外键约束错误:
FOREIGN KEY(korisnik_ime)REFERENCES korisnik(ime),
FOREIGN KEY(firma_naziv)参考文献 firma(naziv)
):
在引用的表中找不到索引,其中 引用的列显示为第一列,或者列中的列类型为 而引用的表中没有 匹配约束。 p> blockquote>

这是我的数据库模式,抱歉非英文名称(顺便说一句,我第一次遇到这个问题时,列名称是 西里尔字符,但即使我用拉丁语转换它们 - 你所看到的 在这里 - 我的问题是不 strong>已解决): p>

  DROP DATABASE IF EXISTS arhiva; 
创建数据库arhiva 
默认字符集utf8mb4 
 DEFAULT COLLATE  utf8mb4_general_ci; 
USE arhiva; 
 
创建表`korisnik`(
 ime VARCHAR(30)NOT NULL,
 lozinka CHAR(60)NOT NULL 
); 
 
CREATE TABLE`firma`(
 naziv  VARCHAR(50)NOT NULL 
); 
 
CREATE TABLE`antitygija`(
 korisnik_ime VARCHAR(30)NOT NULL,
 
firma_naziv VARCHAR(50)NOT NULL,
 pristap CHAR(1)NOT NULL,\  n FOREIGN KEY(korisnik_ime)REFERENCES korisnik(ime),
 FOREIGN KEY(firma_naziv)REFERENCES firma(naziv)
); 
  code>  pre> 
 
 

你能指导我吗? 在解决这个问题的正确方向。 我认为使用西里尔字符是问题,但事实证明问题出在其他地方。 p> div>

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.