drop table if exists Users;
drop table if exists Item;
drop table if exists Bid;
drop table if exists Category;
create table Users (
userID varchar(200),
rating integer,
location varchar(255),
country varchar(150),
PRIMARY KEY(userID)
);
create table Item (
itemID integer,
name varchar(100),
currently float,
buy_price float,
first_bid float,
started timestamp,
ends timestamp,
userID varchar(200),
description varchar(255),
PRIMARY KEY(itemID),
FOREIGN KEY (userID) REFERENCES Users(userID)
);
create table Bid (
itemID integer,
userID varchar(200),
time varchar(180),
amount float,
PRIMARY KEY(itemID,time),
FOREIGN KEY (userID) REFERENCES Users(userID),
FOREIGN KEY (itemID) REFERENCES Item(itemID)
);
create table Category(
itemID integer,
categoryID varchar(220),
PRIMARY KEY(itemID,categoryID),
FOREIGN KEY (itemID) REFERENCES Item(itemID)
);
我有一个名为klyu的数据库。当我运行
时,上面是一个名为create.sql的文件source create.sql
在mysql中,它返回
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.17 sec)
ERROR 1050 (42S01): Table 'Users' already exists
ERROR 1050 (42S01): Table 'Item' already exists
Query OK, 0 rows affected (0.03 sec)
Query OK, 0 rows affected (0.02 sec)
我看到有人说丢失失败后通过使用SHOW ENGINE INNODB STATUS检查这个,但是,当我运行它时,它说ERROR 1227(42000):访问被拒绝;您需要此操作的PROCESS权限
我仔细检查并阅读了外键手册,但我仍然找不到问题所在。
答案 0 :(得分:2)
当您尝试使用用户ID作为外键创建表项和引用并且仍未创建用户表时,您将面临此错误。请参阅此内容 - MySQL - FOREIGN KEY Constraints Documentation:
您必须重新排序脚本。
drop table if exists Item;
drop table if exists Users;
drop table if exists Bid;
drop table if exists Category;
create table Users (
userID varchar(200),
rating integer,
location varchar(255),
country varchar(150),
PRIMARY KEY(userID)
) ENGINE=INNODB;
create table Item (
itemID integer,
name varchar(100),
currently float,
buy_price float,
first_bid float,
started timestamp,
ends timestamp,
userID varchar(200),
description varchar(255),
PRIMARY KEY(itemID),
FOREIGN KEY (userID) REFERENCES Users(userID)
) ENGINE=INNODB;
create table Bid (
itemID integer,
userID varchar(200),
time varchar(180),
amount float,
PRIMARY KEY(itemID,time),
FOREIGN KEY (userID) REFERENCES Users(userID),
FOREIGN KEY (itemID) REFERENCES Item(itemID)
) ENGINE=INNODB;
create table Category(
itemID integer,
categoryID varchar(220),
PRIMARY KEY(itemID,categoryID),
FOREIGN KEY (itemID) REFERENCES Item(itemID)
) ENGINE=INNODB;