简体   繁体   English

errno 150 mySQL外键

[英]errno 150 mySQL foreign key

This SQL is giving me Errno 150 when i'm trying to create the second table with the foreign key on UserID 当我尝试使用UserID上的外键创建第二个表时,此SQL给我Errno 150

Can anyone please advice me what am i doing wrong? 谁能告诉我我在做什么错?

CREATE DATABASE IF NOT EXISTS  OTA;
USE OTA;
CREATE TABLE IF NOT EXISTS Users
(
UserID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
UserName varchar(255) NOT NULL,
Email varchar(255) UNIQUE ,
PW varchar(255),
PN varchar(255),
Admin BIT
);


CREATE TABLE IF NOT EXISTS Notes
(
UID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
Note varchar (255) NOT NULL,
c_Date Date NOT NULL,
c_text varchar (255) NOT NULL,
FOREIGN KEY (UID) REFERENCES Persons(UserID)
);

Sorry for being a duplicate question but i can't find my answer between the related ones. 对不起,我是一个重复的问题,但我找不到相关问题之间的答案。

The problem is that you are trying to reference Persons(UserID) when your first table is called Users . 问题是,当您的第一个表称为Users时,您正在尝试引用Persons(UserID) Try this for your key: 试试这个作为您的密钥:

FOREIGN KEY (UID) REFERENCES Users(UserID)

However, you should have a separate column for the note ID. 但是,您应该在注释ID的单独一列。

Try declaring the primary key after: 在以下情况下尝试声明主键:

CREATE TABLE Orders
(O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))

Also, you auto-incremented both UID and UserID. 另外,您会自动增加UID和UserID。 This is fine if they are the same length, but if you try to reference a foreign key value that doesn't exist, you will get an error. 如果它们的长度相同,这很好,但是如果您尝试引用不存在的外键值,则会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM