Can';t在mysql数据库上创建外键
我无法在mysql数据库中创建任何外键。我以前使用过外键,但最近我不得不重新格式化并重新安装所有内容,在mysql服务器恢复后,外键不再工作 我尝试创建以下简单表格:Can';t在mysql数据库上创建外键,mysql,xampp,Mysql,Xampp,我无法在mysql数据库中创建任何外键。我以前使用过外键,但最近我不得不重新格式化并重新安装所有内容,在mysql服务器恢复后,外键不再工作 我尝试创建以下简单表格: create table `order` ( order_id int not null AUTO_INCREMENT primary key, customer varchar(100) ); create table `order_item` ( order_item_id int not null
create table `order` (
order_id int not null AUTO_INCREMENT primary key,
customer varchar(100)
);
create table `order_item` (
order_item_id int not null AUTO_INCREMENT primary key,
order_id int not null,
product varchar(100),
foreign key (order_id) references `order`
);
订单表已成功创建,但当我尝试创建订单项目表时,收到消息错误代码:1005。无法创建表
mydb
order\u item(错误号:150“外键约束格式不正确”)
。我已经检查了我的代码多次,但没有发现错误。有人知道会出什么问题吗
我正在使用XAMPP安装和启动apache和mysql
编辑: @LV98的回答解决了这个错误,但当我添加第二个外键时,得到了相同的消息:
create table `order` (
order_id int not null AUTO_INCREMENT primary key,
customer varchar(100)
);
create table product (
product_id int not null auto_increment primary key,
name varchar(100)
);
create table `order_item` (
order_item_id int not null AUTO_INCREMENT primary key,
order_id int not null,
product_id int not null,
product varchar(100),
foreign key (order_id) references `order`(order_id),
foreign key (product_id) references product (product_id)
);
你忘了引用一列
foreign key (order_id) references `order`(order_id)
这很有效,谢谢,但是当我添加第二个外键时,我得到了相同的错误。我将编辑问题以发布。@Marlon尝试
product(product\u id)
删除中间的空格。已经尝试过了,但没有效果。我还删除了“外键”和字段之间的空白:foreign key(order_id)
@Marlon您的查询有效您的代码是否有其他内容,上面或下面?我单击了您发送的链接,但当我运行它时,它不会显示任何内容,如果我添加一个select。。。之后没有,我的代码里没有其他内容。我甚至选择了每个CREATETABLE语句并分别执行它。它是否与mysql版本有关,因为它与XAMPP捆绑在一起