2012-04-11 72 views
1

我創建表與phpMyAdmin的語法:SQL外鍵

DROP TABLE IF EXISTS users; 
DROP TABLE IF EXISTS info; 

CREATE TABLE users (
    user_id int unsigned NOT NULL auto_increment, 
    email varchar(100) NOT NULL default '', 
    pwd varchar(32) NOT NULL default '', 
    isAdmin int(1) unsigned NOT NULL, 
    PRIMARY KEY (user_id) 
) TYPE=INNODB; 

CREATE TABLE info (
    info_id int unsigned NOT NULL auto_increment, 
    first_name varchar(100) NOT NULL default '', 
    last_name varchar(100) NOT NULL default '', 
    address varchar(300) NOT NULL default '', 
    zipcode varchar(100) NOT NULL default '', 
    personal_phone varchar(100) NOT NULL default '', 
    mobilephone varchar(100) NOT NULL default '', 
    faxe varchar(100) NOT NULL default '', 
    email2 varchar(100) NOT NULL default '', 
    country varchar(100) NOT NULL default '', 
    sex varchar(1) NOT NULL default '', 
    birth varchar(1) NOT NULL default '', 

    email varchar(100) NOT NULL default '', 
    PRIMARY KEY (info_id), 
    FOREIGN KEY (email) REFERENCES users(email) ON UPDATE CASCADE ON DELETE CASCADE 
) TYPE=INNODB; 

但顯示了錯誤:

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=INNODB' at line 11 "

如果我刪除TYPE = INNODB在創建表的末尾,它會顯示錯誤:

"#1005 - Can't create table 'curriculo.info' (errno: 150) ".

回答

1

使用ENGINE = INNODB而不是TYPE = InnoDB

從MySQL的文檔,5.1版,CREATE TABLE syntax

Note

The older TYPE option was synonymous with ENGINE . TYPEhas been deprecated since MySQL 4.0 but is still supported for backward compatibility in MySQL 5.1 (excepting MySQL 5.1.7). Since MySQL 5.1.8, it produces a warning. It is removed in MySQL 5.5. You should not use TYPE in any new applications, and you should immediately begin conversion of existing applications to use ENGINE instead. (See Section D.1.64, 「Changes in MySQL 5.1.8 (Not released)」.)


如果你想有這樣的外鍵:

FOREIGN KEY (email) REFERENCES users(email) 

users(email)應該有一個UNIQUE約束。所有外鍵都應引用主鍵或唯一鍵。您需要info表中的一行來引用(通過FK)一個且只有一個用戶(用戶表中的行)。


儘管(由於各種原因)最佳做法是將窄(小)和恆定寬度的列作爲外鍵。您的email是100個字符,可能是100到300個字節(取決於字符集)。所以,你可以考慮使用user_id作爲FK,它只有4個字節寬:

CREATE TABLE info (
    ... 
      ---- email varchar(100) NOT NULL default '', 
      ---- removed 
    user_id int unsigned NOT NULL, 
    PRIMARY KEY (info_id), 
    FOREIGN KEY (user_id) REFERENCES users(user_id) 
     ON UPDATE CASCADE ON DELETE CASCADE 
) TYPE=INNODB; 
+0

如果我沒有指定他默認使用的引擎,但錯誤「#1005 - 無法創建表'curriculo.info'(errno:150)」 – 2012-04-11 23:25:29

+0

可能因爲您沒有'UNIQUE KEY(email)在'users'表中。 – 2012-04-11 23:29:33

+0

是的,就是這樣!謝謝 – 2012-04-11 23:35:36

2

嘗試「發動機= INNODB」在的創建結束。 (不是TYPE)

如果您沒有它,它可能無法正常工作,因爲它取決於您的默認引擎類型在mysql首選項中... MyIsam(最常見的默認引擎)不支持外鍵。

+0

我執行命令「show table status;」並且顯示該類型是INNODB。 – 2012-04-11 23:09:36

+0

嘗試:顯示創建表,這可能會給你更多的信息。不知道爲什麼mysql在這種情況下混淆了類型和引擎..但如果你看看:http://dev.mysql.com/doc/refman/5.1/en/create-table.html你會看到引擎關鍵字在那裏。 – 2012-04-11 23:11:58

+0

顯示「ENGINE = InnoDB DEFAULT CHARSET = latin1」,但繼續出現錯誤「」#1005 - 無法創建表'curriculo.info'(errno:150)「女巫我認爲它是一個國外錯誤, – 2012-04-11 23:23:47