MySQL报错Key column 'product_id' doesn't exist in table问题求助
问题原因
你在定义product_orders关联表时出现了字段名拼写错误:
- 字段定义语句误写为
product_is int(11) NOT NULL - 后续主键声明、外键约束引用的字段名都是
product_id,和实际定义的字段名不匹配,因此触发「Key column 'product_id' doesn't exist in table」报错。
修正后的SQL代码
create database if not exists customers; use customers; drop table if exists phonenumbers; drop table if exists product_orders; drop table if exists products; drop table if exists orders; drop table if exists customers; CREATE TABLE customers ( id int(11) NOT NULL auto_increment, first_name varchar(20) NOT NULL, last_name varchar(20) NOT NULL, street varchar(25), state char(2) NOT NULL, zip char(5), city varchar(20), PRIMARY KEY(id) ); CREATE TABLE phonenumbers ( id int(11) NOT NULL auto_increment, customer_id int(11) NOT NULL, phone_number varchar(10) NOT NULL, PRIMARY KEY(id), FOREIGN KEY(customer_id) references customers(id) ); create table orders ( id int(11) NOT NULL auto_increment, customer_id int(11) NOT NULL, date_ordered datetime default current_timestamp, date_delivered datetime, total decimal(7, 2), PRIMARY KEY(id), FOREIGN KEY(customer_id) references customers(id) ); create table products ( id int(11) NOT NULL auto_increment, name varchar(50) NOT NULL, description varchar(255), price decimal(5, 2) NOT NULL, qty int(10) NOT NULL, PRIMARY KEY(id) ); create table product_orders ( product_id int(11) NOT NULL, order_id int(11) NOT NULL, qty int(10) NOT NULL, PRIMARY KEY(product_id, order_id), FOREIGN KEY(product_id) references products(id), FOREIGN KEY(order_id) references orders(id) );
内容的提问来源于stack exchange,提问作者user14343607
相关产品推荐
相关产品推荐

