MYSQL 外键,无法创建 table (errno:150)
MYSQL Foreign Key, Cant create table (errno:150)
我正在尝试为我的系统构建数据库和表。但是我发现如果我不在代码中添加外键。没有错误。我用了很多方法尝试使代码正常工作,但仍然有错误。
Create table if not exists users_details_one
(
fname varchar(255),
lname varchar(255),
address varchar(255),
users_email varchar(255),
users_password varchar(255),
department varchar(255)
);
Create table if not exists users_one
(
users_email varchar(255),
users_password varchar(255) ,
FOREIGN KEY (users_email) REFERENCES users_details_one(users_email),
FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
);
如果您想将列作为外键,这些列必须是主键或必须具有唯一性约束,即在您的情况下 users_email 和 users_password of users_details_one 必须是唯一键或主键。
您的外键有错别字:
FOREIGN KEY (users_password) REFERENCES users_details_one(users_spassword)
应该是 FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
并且您还需要 table users_details_one
中 users_email
和 users_password
的索引,例如:
Create table if not exists users_details_one
(
fname varchar(255),
lname varchar(255),
address varchar(255),
users_email varchar(255),
users_password varchar(255),
department varchar(255),
index (users_email),
index (users_password)
);
Create table if not exists users_one
(
users_email varchar(255),
users_password varchar(255) ,
FOREIGN KEY (users_email) REFERENCES users_details_one(users_email),
FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
);
索引不必是唯一的。
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist.
我正在尝试为我的系统构建数据库和表。但是我发现如果我不在代码中添加外键。没有错误。我用了很多方法尝试使代码正常工作,但仍然有错误。
Create table if not exists users_details_one
(
fname varchar(255),
lname varchar(255),
address varchar(255),
users_email varchar(255),
users_password varchar(255),
department varchar(255)
);
Create table if not exists users_one
(
users_email varchar(255),
users_password varchar(255) ,
FOREIGN KEY (users_email) REFERENCES users_details_one(users_email),
FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
);
如果您想将列作为外键,这些列必须是主键或必须具有唯一性约束,即在您的情况下 users_email 和 users_password of users_details_one 必须是唯一键或主键。
您的外键有错别字:
FOREIGN KEY (users_password) REFERENCES users_details_one(users_spassword)
应该是 FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
并且您还需要 table users_details_one
中 users_email
和 users_password
的索引,例如:
Create table if not exists users_details_one
(
fname varchar(255),
lname varchar(255),
address varchar(255),
users_email varchar(255),
users_password varchar(255),
department varchar(255),
index (users_email),
index (users_password)
);
Create table if not exists users_one
(
users_email varchar(255),
users_password varchar(255) ,
FOREIGN KEY (users_email) REFERENCES users_details_one(users_email),
FOREIGN KEY (users_password) REFERENCES users_details_one(users_password)
);
索引不必是唯一的。
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.