1
這是對這個問題的進一步討論:幾個類似的DDL不同的結果外鍵
Why is it still possible to insert a foreign key that doesn't exist?
這工作:
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;
但這些2不是:
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)
);
乙
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;
對於乙,這是因爲integer primary key
相同integer unsigned primary key
你能解釋爲什麼一個和乙不工作?
但是,正如我在我的崗位,'主鍵說'暗示'無符號',對吧? – user198729 2010-02-23 21:19:15
@ user198729:沒有暗示。 MySQL數字數據類型默認是簽名的,所以你必須明確指定'unsigned'屬性:http://dev.mysql.com/doc/refman/5.1/en/numeric-type-overview.html – 2010-02-23 21:30:13
對於* * A **,如果內聯引用不被InnoDB識別,那麼它們用於什麼? – user198729 2010-02-23 21:40:38