奇怪的MySQL CREATE TABLE行为

时间:2013-09-30 13:28:29

标签: php mysql sql

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

这是我的问题:

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个。前2个工作完美,但之后没有任何工作。任何帮助将不胜感激。

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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