我已经被这个问题困了大约一个小时了,我无法解决它。请帮忙!
这是我的查询:
CREATE TABLE IF NOT EXISTS snippets (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
code TEXT NOT NULL,
lang_id INT(3) UNSIGNED NOT NULL,
dev_id INT(11) UNSIGNED NOT NULL,
post_date TIMESTAMP NOT NULL DEFAULT NOW(),
views INT UNSIGNED NOT NULL DEFAULT 0,
FOREIGN KEY (lang_id) REFERENCES languages (id),
FOREIGN KEY (dev_id) REFERENCES developers (id),
PRIMARY KEY (id)
);
这个查询怎么可能在 PHPMyAdmin 和命令行中工作,而不是在 PHP 脚本中工作?这是应该创建的 6 个表中的第 3 个。前两个工作完美,但之后就没有了。任何帮助,将不胜感激。
$link = new PDOConfig();
$link->query("CREATE DATABASE IF NOT EXISTS ratemycode");
$link->connect($link, 'ratemycode');
$queries['tables'] = array(
"CREATE TABLE IF NOT EXISTS developers (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(42) NOT NULL,
password VARCHAR(64) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
)",
"CREATE TABLE IF NOT EXISTS languages (
id INT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(42) NOT NULL,
PRIMARY KEY (id)
)",
"CREATE TABLE IF NOT EXISTS snippets (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
code TEXT NOT NULL,
lang_id INT(3) UNSIGNED NOT NULL,
dev_id INT(11) UNSIGNED NOT NULL,
post_date TIMESTAMP NOT NULL DEFAULT NOW(),
views INT UNSIGNED NOT NULL DEFAULT 0,
FOREIGN KEY (lang_id) REFERENCES languages (id),
FOREIGN KEY (dev_id) REFERENCES developers (id),
PRIMARY KEY (id)
)",
"CREATE TABLE IF NOT EXISTS comments (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
body TEXT NOT NULL,
post_date TIMESTAMP NOT NULL DEFAULT NOW(),
snip_id INT(11) UNSIGNED NOT NULL,
dev_id INT(11) UNSIGNED NOT NULL,
FOREIGN KEY (snip_id) REFERENCES snippets (id),
FOREIGN KEY (dev_id) REFERENCES developers (id),
PRIMARY KEY (id)
)",
"CREATE TABLE IF NOT EXISTS upvotes (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
snip_id INT(11) UNSIGNED NOT NULL,
dev_id INT(11) UNSIGNED NOT NULL,
FOREIGN KEY (snip_id) REFERENCES snippets (id),
FOREIGN KEY (dev_id) REFERENCES developers (id),
PRIMARY KEY (id)
)",
"CREATE TABLE IF NOT EXISTS downvotes (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
snip_id INT(11) UNSIGNED NOT NULL,
dev_id INT(11) UNSIGNED NOT NULL,
FOREIGN KEY (snip_id) REFERENCES snippets (id),
FOREIGN KEY (dev_id) REFERENCES developers (id),
PRIMARY KEY (id)
)"
);
foreach ($queries['tables'] as $table) {
$link->query($table);
}
最佳答案
它指的是您的错误消息,但通常是由外键引起的这种错误!
外键列和引用之间可能存在差异。
例如,您的引用列类型可能与外键不同!
关于php - 奇怪的MySQL CREATE TABLE行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19095529/