This is a further discussion on this question:
This works:
CREATE TABLE products (
id integer unsigned auto_increment primary key
) ENGINE=INNODB;
CREATE TABLE orders (
id integer PRIMARY KEY auto_increment,
product_id integer unsigned,
quantity integer,
INDEX product_id_idx (product_id),
FOREIGN KEY (product_id) REFERENCES products (id)
) ENGINE=INNODB;
But these 2 not:
A
CREATE TABLE products (
id integer unsigned auto_increment primary key
) ENGINE=INNODB;
CREATE TABLE orders (
id integer PRIMARY KEY auto_increment,
product_id integer unsigned REFERENCES products (id),
quantity integer,
INDEX product_id_idx (product_id)
);
B
CREATE TABLE products (
id integer auto_increment primary key
) ENGINE=INNODB;
CREATE TABLE orders (
id integer PRIMARY KEY auto_increment,
product_id integer unsigned,
quantity integer,
INDEX product_id_idx (product_id),
FOREIGN KEY (product_id) REFERENCES products (id)
) ENGINE=INNODB;
For B,it's because integer primary key
is the same as integer unsigned primary key
Can you explain why A and B are not working?