我在构造多个数据库表结构时遇到了错误。 我有'用户'表,它是其他表的父表。
- USER -
email VARCHAR(50) DEFAULT '' NOT NULL;
login_flag VARCHAR(45) DEFAULT '' NOT NULL;
session_id VARCHAR(40) DEFAULT '0' NOT NULL;
name VARCHAR(20) DEFAULT '' NOT NULL;
...
PRIMARY KEY (email, login_flag);
当我执行下面的查询时,它们会很好地执行。
CREATE TABLE ARTICLE
(
id INT(13) PRIMARY KEY NOT NULL AUTO_INCREMENT,
email VARCHAR(50) DEFAULT '0' NOT NULL,
login_flag VARCHAR(45) DEFAULT '' NOT NULL,
content TEXT NOT NULL,
...
write_date DATETIME NOT NULL,
CONSTRAINT community_article_user_email_login_flag_fk FOREIGN KEY
(email, login_flag) REFERENCES user (email, login_flag)
);
CREATE INDEX community_article_user_email_login_flag_fk ON community_article (email, login_flag);
CREATE TABLE ARTICLE_LIKE
(
article_id INT(13) DEFAULT '0' NOT NULL COMMENT 'content id(target id)',
email VARCHAR(50) DEFAULT '0' NOT NULL,
login_flag VARCHAR(45) DEFAULT '' NOT NULL,
make_date DATETIME DEFAULT 'CURRENT_TIMESTAMP',
CONSTRAINT community_article_like_community_article_id_fk FOREIGN KEY (article_id) REFERENCES community_article (id),
CONSTRAINT community_article_like_user_email_login_flag_fk FOREIGN KEY (email, login_flag) REFERENCES user (email, login_flag)
);
CREATE INDEX community_article_like_community_article_id_fk ON community_article_like (article_id);
CREATE INDEX community_article_like_user_email_login_flag_fk ON community_article_like (email, login_flag);
但是,我执行下面的查询来构建表,它失败并显示错误消息[[HY000] [1215]无法添加外键约束]。
CREATE TABLE BOOKMARK
(
item_id INT(13) NOT NULL,
email VARCHAR(50) DEFAULT '' NOT NULL,
login_flag VARCHAR(45) DEFAULT '' NOT NULL,
make_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT FK FOREIGN KEY (email, login_flag) REFERENCES user (email, login_flag)
)
我完全不知道为什么会失败。我认为后者与前者没有区别。