MySQL语法错误求助:CREATE TABLE users语句故障排查
Let’s walk through the issues in your statement and get it working smoothly:
1. Wrong Quote Type for Column Identifiers
The biggest syntax mistake here is using single quotes (') around column names (like 'id' or 'first_name'). In MySQL, single quotes are reserved for string literal values (think the 'Active' in your ENUM definition). For identifiers like column or table names, you need to use backticks (`)—or skip quotes entirely if your names don’t clash with MySQL’s reserved words (none of your column names do here, so quotes are optional either way).
2. Incomplete Statement & Redundant Constraint
Your code cuts off at P..., so I’ll assume you intended to wrap up the table definition. Also, for an AUTO_INCREMENT column, it’s standard practice to set it as the PRIMARY KEY—since primary keys inherently enforce uniqueness, you don’t need to add a separate UNIQUE constraint here.
Corrected Full SQL Statement
CREATE TABLE users ( `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `first_name` VARCHAR(250) NOT NULL, `last_name` VARCHAR(250) NOT NULL, `user_name` VARCHAR(50) NOT NULL UNIQUE, `email` VARCHAR(250) NOT NULL UNIQUE, `password` VARCHAR(250) NOT NULL, `last_access` DATETIME DEFAULT NULL, `phone` VARCHAR(250) DEFAULT NULL, `cellphone` VARCHAR(250) DEFAULT NULL, `status` ENUM('Active', 'Inactive', 'Blocked', 'Deleted', 'Newreg') DEFAULT 'Active' );
Quick Additional Notes
- If you prefer, you can drop the backticks entirely (e.g.,
idinstead of`id`) since none of your column names are MySQL reserved words. - The
PRIMARY KEYonidhandles the uniqueness requirement, so removing the redundantUNIQUEconstraint cleans up the code. - I filled in the truncated end of your statement to make it valid—tweak it if you had additional columns or constraints planned.
内容的提问来源于stack exchange,提问作者Alfre Cano

