У меня проблема с использованием двух ограничений внешнего ключа в одной таблице.
Я создал все необходимые таблицы.1) Клиент 2) Продукт 3) Заказы и возникает проблема синтаксической ошибки в четвертой таблице, где я пытаюсь использовать два поля таблицы в качестве ограничений внешнего ключа.
Для таблицы Customer
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);
Для таблицы Product
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);
Для таблицы Orders
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) ссылается на product (id)