简体   繁体   中英

Whats wrong in my sql query?

Here I'm trying to create a new table, but I don't know what I did wrong when creating the table.

SQL query:

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

When I run this I get an error. I've added ; after use wan_ecommerce ;, but I'm still getting:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '

user_name varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT,

user_passwo' at line 2

What am I doing wrong here?

How can I solve this?

Use semicolon to terminate each statement.

CREATE DATABASE IF NOT EXISTS wan_ecommerce;
USE wan_ecommerce; -- <----- semi colon was missing
CREATE TABLE IF NOT EXISTS users (
-- ....

The 'COMMENT' is a keyword. If you use it,you must to add a description to flowing 'COMMENT',like this code:

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

If you don't need this 'COMMENT',you can remove it.The code like this:

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

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