https://hackernoon.com/mysql-tutorial-example-relation-foreign-key-database-f...
DROP TABLE IF EXISTS product_details;
DROP TABLE IF EXISTS products;
CREATE TABLE IF NOT EXISTS products (
productID INT UNSIGNED NOT NULL AUTO_INCREMENT,
productCode CHAR(3) NOT NULL DEFAULT '',
name VARCHAR(30) NOT NULL DEFAULT '',
quantity INT UNSIGNED NOT NULL DEFAULT 0,
price DECIMAL(7,2) NOT NULL DEFAULT 99999.99,
-- supplierID INT UNSIGNED NOT NULL,
PRIMARY KEY (productID)
);
CREATE TABLE IF NOT EXISTS product_details (
productID INT UNSIGNED NOT NULL,
comment TEXT NULL,
PRIMARY KEY (productID),
FOREIGN KEY (productID) REFERENCES products (productID)
);
ALTER TABLE products ADD COLUMN supplierID INT UNSIGNED NOT NULL;
ALTER TABLE products ADD FOREIGN KEY (supplierID) REFERENCES suppliers (supplierID);
mysql -u root -p example_db < example_db.sql
Enter password:
ERROR 1005 (HY000) at line 26: Can't create table `example_db`.`#sql-1c5d_36` (errno: 150 "Foreign key constraint is incorrectly formed")