ALTER TABLE问题

时间:2012-06-19 09:24:14

标签: mysql sql create-table

我遇到问题,我想在表bericht中添加一个外键(电子邮件)。

vrijwilliger中通过电子邮件发送电子邮件,电子邮件为varchar(80)

ALTER TABLE bericht
ADD(
FOREIGN KEY (email) REFERENCES vrijwilliger(email));

错误讯息:

  

#1005 - 无法创建表'。\ c5g4westpopintranet#sql-1494_145.frm'(错误号:150)

我不明白,希望你能帮助我。

第一张表

CREATE TABLE bericht(
berichtID int(8),
onderwerp varchar(255),
datum date,
tijd time,
inhoudbericht,
email varchar(80),
PRIMARY KEY(berichtID),
FOREIGN KEY(email) REFERENCES vrijwilliger(email));

第二张表

 CREATE TABLE vrijwilliger(
 vrijwilligerID int(8),
 email varchar(80),
 Primary key(vrijwilligerID));

2 个答案:

答案 0 :(得分:0)

尝试使用以下查询来更改表

ALTER TABLE bericht ADD CONSTRAINT email FOREIGN KEY(email) REFERENCES vrijwilliger(email) ON DELETE SET NULL ON UPDATE SET NULL;

Alter表的语法

ALTER TABLE tbl_name
    ADD [CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name,...)
    [ON DELETE reference_option]
    [ON UPDATE reference_option]

参见

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

答案 1 :(得分:-1)

尝试以下查询,它将适合您

CREATE TABLE `vrijwilliger` (  
vrijwilligerID INT(8),  
email VARCHAR(80),  
PRIMARY KEY(vrijwilligerID),
UNIQUE KEY (email)
);

CREATE TABLE bericht( 
berichtID INT(8), 
onderwerp VARCHAR(255), 
datum DATE, 
tijd TIME, 
inhoudbericht INT(8), 
email VARCHAR(80), 
PRIMARY KEY(berichtID), 
FOREIGN KEY(email) 
REFERENCES vrijwilliger(email));