5

我已经在这个问题上停留了大约一个小时,但我无法解决它。请帮忙!

这是我的查询:

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);
}
4

3 回答 3

0

它指的是你的错误信息,但通常这种错误是由外键引起的!外键列和引用之间可能存在差异。例如,您的参考列类型可能与您的外键不同!

于 2013-09-30T13:40:24.827 回答
0

尝试删除外键并稍后使用 ALTER TABLE 添加它们。

于 2013-09-30T13:31:18.873 回答
0

哈...原来我在 IDE 工作区中所做的更改没有反映在我的 /www/ 目录中(可能是 Linux 新手错误)。代码第一次没有正确运行,因为这个较新版本的 MySQL 显然不允许DATETIME列具有函数返回的默认值。TIMESTAMP不过, s 仍然可以有一个默认值NOW()。无论如何,谢谢大家。

于 2013-09-30T15:54:48.463 回答