表创建失败:(1064)您的SQL语法中有错误

时间:2015-10-25 11:37:00

标签: mysql

我的代码出了什么问题?

$query = "CREATE TABLE IF NOT EXISTS 'data_table' (
    'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
    'name' VARCHAR(255) NOT NULL,
    'active' TINYINT NOT NULL DEFAULT '1',
    'create_date' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    'income' INT DEFAULT NULL,
    PRIMARY KEY ('id')
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";

$createTable = mysqli_query($connect, $query);

2 个答案:

答案 0 :(得分:1)

表格和列名称不需要单引号而不用:您可以使用" or backticks代替。

CREATE TABLE IF NOT EXISTS data_table(
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
name VARCHAR( 255 ) NOT NULL ,
active TINYINT NOT NULL DEFAULT  '1',
create_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
income INT DEFAULT NULL ,
PRIMARY KEY ( id )
) ENGINE = MYISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT =1

答案 1 :(得分:0)

尝试:

$query = "CREATE TABLE IF NOT EXISTS `data_table` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `active` TINYINT NOT NULL DEFAULT 1,
    `create_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `income` INT DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";

请注意,单引号将替换为反引号