Foreign key constraint code ?
DROP Table Sales;
CREATE Table Sales
(
Purchase_number INT AUTO_INCREMENT,
data_of_purchase DATE,
customer_id INT,
item_code VARCHAR(10),
PRIMARY KEY (purchase_number)
);
ALTER Table Sales
ADD FOREIGN KEY (customer_id) references customers(customer_id) ON DELETE CASCADE ;
i'mn getting an error fix the error ?
error?
19:35:31 ALTER Table Sales ADD FOREIGN KEY (customer_id) references customers(customer_id) ON DELETE CASCADE Error Code: 1824. Failed to open the referenced table 'customers' 0.0049 sec
CREATE Table Sales
(
Purchase_number INT AUTO_INCREMENT,
data_of_purchase DATE,
customer_id INT,
item_code VARCHAR(10),
PRIMARY KEY (purchase_number)
);
ALTER Table Sales
ADD FOREIGN KEY (customer_id) references customers(customer_id) ON DELETE CASCADE ;
i'mn getting an error fix the error ?
error?
19:35:31 ALTER Table Sales ADD FOREIGN KEY (customer_id) references customers(customer_id) ON DELETE CASCADE Error Code: 1824. Failed to open the referenced table 'customers' 0.0049 sec
1 answers ( 0 marked as helpful)
Hi Harshita!
Thanks for reaching out.
The error occurs because the table customers does not exist or hasn't been created yet. You need to ensure that the customers table exists before creating the foreign key constraint.
To fix the issue:
1) Create the customers table first with the appropriate structure (including customer_id as the primary key).
2) Then apply the foreign key constraint on the Sales table.
Hope this helps.
Best,
Tsvetelin
Thanks for reaching out.
The error occurs because the table customers does not exist or hasn't been created yet. You need to ensure that the customers table exists before creating the foreign key constraint.
To fix the issue:
1) Create the customers table first with the appropriate structure (including customer_id as the primary key).
2) Then apply the foreign key constraint on the Sales table.
Hope this helps.
Best,
Tsvetelin