这里我正在尝试创建一个新表,但我不知道在创建表时我做错了什么。
SQL查询:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT,
user_last_failed_login int(10) DEFAULT NULL COMMENT,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
当我运行时,我收到错误。我在;
;之后添加了use wan_ecommerce
,但我仍然得到:
1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法 靠近'
user_name varchar(64)COLLATE utf8_unicode_ci NOT NULL COMMENT,
第2行user_passwo'
我在这里做错了什么?
我该如何解决这个问题?
答案 0 :(得分:2)
使用分号终止每个语句。
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce; -- <----- semi colon was missing
CREATE TABLE IF NOT EXISTS users (
-- ....
答案 1 :(得分:0)
'COMMENT'是关键字。如果您使用它,则必须向流动的“COMMENT”添加描述,如下代码:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'name',
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'password_hash',
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'email',
user_active tinyint(1) NOT NULL DEFAULT '0' COMMENT 'active',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'activation_hash',
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'password_reset_hash',
user_password_reset_timestamp bigint(20) DEFAULT NULL COMMENT 'password_reset_timestamp',
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'rememberme_token',
user_failed_logins tinyint(1) NOT NULL DEFAULT '0' COMMENT 'failed_logins',
user_last_failed_login int(10) DEFAULT NULL COMMENT 'last_failed_login',
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';
如果你不需要这个'评论',你可以删除它。代码如下:
CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce
CREATE TABLE IF NOT EXISTS users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_password_hash varchar(255) COLLATE utf8_unicode_ci NOT NULL,
user_email varchar(64) COLLATE utf8_unicode_ci NOT NULL,
user_active tinyint(1) NOT NULL DEFAULT '0',
user_activation_hash varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_hash char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
user_password_reset_timestamp bigint(20) DEFAULT NULL,
user_rememberme_token varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
user_failed_logins tinyint(1) NOT NULL DEFAULT '0',
user_last_failed_login int(10) DEFAULT NULL,
user_registration_datetime datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_registration_ip varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY user_name (`user_name`),
UNIQUE KEY user_email (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data';