1

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

电子邮件引用电子邮件中vrijwilliger,电子邮件是一个varchar(80)

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

错误信息:

#1005 - 无法创建表 '.\c5g4westpopintranet#sql-1494_145.frm' (errno: 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));
4

2 回答 2

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
于 2012-06-19T09:31:23.783 回答
-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));
于 2012-06-19T11:00:37.127 回答