0

I have table products and table brands.I want to do one to many relationship. brand has many products.

This are tables with columns enter image description here

And this sample of data in product enter image description here And this sample of data in brands enter image description here I try to add foreign key .by this mysql statment

ALTER TABLE products ADD CONSTRAINT fk_brand_id FOREIGN KEY (brand_id) REFERENCES brands(brand_id)

I get this error

 #1005 - Can't create table `larashop`.`#sql-dd4_77` (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)

Please anyone help me

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Azhar Nabil
  • 73
  • 11

1 Answers1

0

Try to check the storage engines on the tables match. For instance trying to set a FK between InnoDB and MyISAM will fail.

Edit answer.

you are unsigned datatype of brand_id from brands table & assign int datatype for foreign key brand_id in products table.try to change brand_id datatype of unsigned to integer in brands table.

please check this link MySQL Cannot Add Foreign Key Constraint

Community
  • 1
  • 1
  • when I write SHOW TABLE STATUS FROM larashop it show all tables in larashop and engine is InnoDB.But when I write SELECT products FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'larashop' AND engine = 'InnoDB'; I get error #1054 - Unknown column 'products' in 'field list' – Azhar Nabil May 16 '16 at 14:41
  • All tables in larashop in InnoDB engine – Azhar Nabil May 16 '16 at 15:18
  • information_schema is not your database.A schema is a database, so the SCHEMATA table provides information about databases. information_schema also saved your trigger,procedure etc information. – faheem ahmad May 17 '16 at 10:50