简体   繁体   中英

Strange MySQL CREATE TABLE behavior

I've been stuck on this issue for about an hour now, and I can't solve it. Please help!

Here is my query:

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

How is it possible that this query works in PHPMyAdmin and on the command line, but not in a PHP script? This is the 3rd of 6 tables that should be created. The first 2 work perfectly, but nothing after that. Any help would be appreciated.

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

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

It refers to your error message, but usually this kind of error caused by foreign keys! maybe there are differences between foreign key column and the references. for example maybe your reference column type is different with your foreign key!

Ha... Turns out the changes I made in my IDE workspace were not being reflected in my /www/ directory (probably a Linux noob mistake). The code didn't run correctly the first time because this newer version of MySQL apparently doesn't allow DATETIME columns to have a default value returned by a function. TIMESTAMP s can still have a default value of NOW() , though. Anyways, thanks a bunch everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM