使用外键约束时,如何解决表示SQL语法错误的错误?

时间:2019-02-11 17:24:32

标签: mysql sql

我在一个表中使用两个外键约束时遇到问题。

我已经制作了所有必需的表。 1)客户 2)产品 3)订单 并且在第四张表中出现语法错误问题,我试图将表的两个字段用作外键约束。

用于客户表

CREATE TABLE customer (id int not null auto_increment primary key, customer_name varchar(64) not null, 
customer_address varchar(64) not null, created_date DateTime, modified_date DateTime);

INSERT INTO customers (customer_name, customer_address, created_date, modified_date)
VALUES ('Pratit Raj Giri', 'Banepa-6, Kavrepalanchowk', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

对于产品表

CREATE TABLE product (id int not null auto_increment primary key, product_name varchar(64) not null, 
product_description varchar(128) not null, created_date DateTime, modified_date DateTime);

INSERT INTO product (product_name, product_description, created_date, modified_date)
VALUES ('Samsung Galaxy S9+', 'This is the latest model of Samsung smartphone in the market’, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

“订单”表

CREATE TABLE orders (id int not null auto_increment primary key, customer_id int, status int not null default 1, constraint fk_customer_id foreign key (customer_id) references customer(id)
ON DELETE CASCADE ON UPDATE CASCADE);

INSERT INTO orders (customer_id, status)
VALUES (1, 1);

对于Order_Products表

CREATE TABLE order_products (id int not null auto_increment primary key, order_id int not null,  product_id int not null, quantity decimal(9,3) not null, price decimal(9,3) not null,
constraint fk_order_id foreign key (order_id) references orders (id)
ON DELETE CASCADE ON UPDATE CASCADE),
constraint fk_product_id foreign key (product_id) references product (id)
ON DELETE CASCADE ON UPDATE CASCADE), ordered_date DateTime);

错误::您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以获取在'附近使用的正确语法 约束fk_product_id外键(product_id)引用产品(id)

0 个答案:

没有答案