我目前正在尝试学习一两件关于数据库的知识,所以我已经通过W3Schools的PHP和SQL课程了,而且我现在开始创建自己的小应用程序,论坛,一个简单的。
但是,我遇到了外键问题。我已经通过了MariaDB关于外键和约束的文档,我只是不知道为什么我会遇到这个错误。最可能的原因是我在代码中盯得太多,现在我再也看不到我的错误了。
所以现在我正在寻求帮助,当我在我的XAMPP PHPMyAdmin上运行这段SQL时,我收到以下错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''fk_topics_to_titles'
FOREIGN KEY (titleID) REFERENCES titles(id)
' at line 8
这就是语法错误,我已经评论了发生错误的区域,我的完整脚本如下所示:
DROP DATABASE IF EXISTS eForum;
CREATE DATABASE eForum;
CREATE TABLE IF NOT EXISTS eForum.titles(
id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
titleName varchar(50) NOT NULL,
creator varchar(100) NOT NULL,
createDate TIMESTAMP,
lastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS eForum.topics(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
topicName VARCHAR(50) NOT NULL,
creator VARCHAR(100) NOT NULL,
createDate TIMESTAMP,
lastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
titleID INT UNSIGNED NOT NULL, /*after this is where the error occurs*/
CONSTRAINT 'fk_topics_to_titles'
FOREIGN KEY (titleID) REFERENCES titles(id)
ON DELETE CASCADE
ON UPDATE RESTRICT
)ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS eForum.threads(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
threadName VARCHAR(50) NOT NULL,
threadText TEXT NOT NULL,
threadImage VARCHAR(100) NOT NULL DEFAULT 'sloth',
creator VARCHAR(100) NOT NULL,
creatorCorp VARCHAR(10) NOT NULL DEFAULT 'Pikachu',
createDate TIMESTAMP,
lastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
topicID INT UNSIGNED NOT NULL,
CONSTRAINT 'fk_threads_to_topics'
FOREIGN KEY (topicID) REFERENCES topics(id)
ON DELETE CASCADE
ON UPDATE RESTRICT
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS eForum.posts(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
postName VARCHAR(50) NOT NULL,
postText TEXT NOT NULL,
postCreator varchar(40) NOT NULL,
postCorp varchar(30) NOT NULL DEFAULT 'get in corp',
createDate TIMESTAMP,
lastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
threadID INT UNSIGNED NOT NULL,
CONSTRAINT 'fk_posts_to_threads'
FOREIGN KEY (threadID) REFERENCES threads(id)
ON DELETE CASCADE
ON UPDATE RESTRICT
)ENGINE = InnoDB;
我确定我已经设法从这里的文档中复制了结构:https://mariadb.com/kb/en/mariadb/foreign-keys/据我所知,如果我使用tinyints,shortints并不重要或定期投注。
请在这里帮助我,我完全迷失了,我已经完成了这篇文章的所有相关问题(有人建议,我似乎找不到有类似问题的人
提前致谢
答案 0 :(得分:0)
围绕CONSTRAINT
名称的背景,而不是引号。