尝试使用外键引用创建表的Mysql errno 150 [英] Mysql errno 150 trying to create table with foreign key references

查看:119
本文介绍了尝试使用外键引用创建表的Mysql errno 150的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在数据库A中:

  CREATE TABLE复制(
id varchar(255)NOT NULL PRIMARY KEY,
uid varchar(255)NOT NULL,
value int(11)NOT NULL,
FOREIGN KEY(uid)REFERENCES databaseB.users(username)
);

在数据库B中,有一个名为users的表如下:

  + ----------------- + -------------- +  - ----- + ----- + --------- + ------- + 
|字段|类型|空|密钥|默认|额外|
+ ----------------- + -------------- + ------ + ----- + --------- + ------- +
| id | varchar(255)| NO | | NULL | |
|用户名| varchar(255)| NO | PRI | NULL | |
+ ----------------- + -------------- + ------ + ----- + --------- + ------- +

当我尝试创建表复制,给我以下错误:

 错误1005(HY000):无法创建表'databaseA。复制(errno:150)

任何想法?提前感谢!

解决方案

你需要添加一个索引到uid,否则你将无法设置它来引用任何东西。

另外,通常在你的DatabaseB中,你的用户表有一个主键ID和一个唯一的用户名索引。然后你可以设置一个来自DatabaseA.replication的外键参考databaseB.users(id)


I'm trying to create a table in mysql with a foreign key reference, like this:

In database A:

CREATE TABLE replication (
  id varchar(255) NOT NULL PRIMARY KEY,
  uid varchar(255) NOT NULL,
  value int(11) NOT NULL,
  FOREIGN KEY (uid) REFERENCES databaseB.users(username)
);

In database B i have a table named users like this:

+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | varchar(255) | NO   |     | NULL    |       |
| username        | varchar(255) | NO   | PRI | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+

When i try to create table replication, gives me the following error:

ERROR 1005 (HY000): Can't create table 'databaseA.replication' (errno: 150)

Any idea? Thanks in advance!

解决方案

You need to add an index to uid or you will not be able to set it up to reference anything.

Also, typically in your DatabaseB you would have your users table have a Primary Key on ID and a unique index on username. Then you would set up a foreign key from DatabaseA.replication REFERENCES databaseB.users(id)

这篇关于尝试使用外键引用创建表的Mysql errno 150的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆