Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL CREATE TABLE Error

The Answer was that I was using incorrect quotation marks instead of backticks. Stupid syntax hilighter tricked me.

I've been stuck on this one simple(ish) thing for the last 1/2 hour so I thought I might try to get a quick answer here.

What exactly is incorrect about my SQL syntax, assuming I'm using mysql 5.1

CREATE TABLE 'users' (
    'id' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    'username' VARCHAR(20) NOT NULL,
    'password' VARCHAR(40) NOT NULL,
    'salt' VARCHAR(40) DEFAULT NULL,
    'email' VARCHAR(80) NOT NULL,
    'created_on' INT(11) UNSIGNED NOT NULL,
    'last_login' INT(11) UNSIGNED DEFAULT NULL,
    'active' TINYINT(1) UNSIGNED DEFAULT NULL,
) 
ENGINE InnoDB;

The error I get is:

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

CREATE TABLE 'users' (
    'id' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,' at line 3


Elapsed Time:  0 hr, 0 min, 0 sec, 0 ms.

Also, does anyone have any good tutorials about how to use Zend_Auth for complete noobs?

Thanks.

like image 992
Adam M-W Avatar asked Jan 09 '11 14:01

Adam M-W


People also ask

Why am I getting a syntax error in SQL?

Misspellings are the most common cause for error in SQL. Unfortunately, SQL will not autocorrect mistyped keywords, tables, columns, or values. Check keyword spelling by referring to the documentation for the type of SQL you are using.

What is #table in SQL?

Tables are database objects that contain all the data in a database. In tables, data is logically organized in a row-and-column format similar to a spreadsheet. Each row represents a unique record, and each column represents a field in the record.

How do I manually create a table in SQL Server?

In SSMS, in Object Explorer, connect to the instance of Database Engine that contains the database to be modified. In Object Explorer, expand the Databases node and then expand the database that will contain the new table. In Object Explorer, right-click the Tables node of your database and then click New Table.

What is meant by syntax error in SQL?

Overview. This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: Using a database-specific SQL for the wrong database (eg BigQuery supports DATE_ADD, but Redshift supports DATEADD) Typo in the SQL (missing comma, misspelled word, etc)


2 Answers

Table and column identifiers are quoted using backticks (or double quotes if you've configured them).

Additionally you have a comma at the end of your column list.

CREATE TABLE `users` (
    `id` MEDIUMINT( 8 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `username` VARCHAR( 20 ) NOT NULL,
    `password` VARCHAR( 40 ) NOT NULL,
    `salt` VARCHAR( 40 ) DEFAULT NULL,
    `email` VARCHAR( 80 ) NOT NULL,
    `created_on` INT( 11 ) UNSIGNED NOT NULL,
    `last_login` INT( 11 ) UNSIGNED DEFAULT NULL,
    `active` TINYINT( 1 ) UNSIGNED DEFAULT NULL
) ENGINE InnoDB
like image 83
jasonbar Avatar answered Oct 21 '22 11:10

jasonbar


You are using single quotes instead of backticks for your table and field names, which is wrong. There should also be an equals sign between ENGINE and InnoDB.

Here's the fixed SQL:

CREATE TABLE `users` (
    `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `username` VARCHAR(20) NOT NULL,
    `password` VARCHAR(40) NOT NULL,
    `salt` VARCHAR(40) DEFAULT NULL,
    `email` VARCHAR(80) NOT NULL,
    `created_on` INT(11) UNSIGNED NOT NULL,
    `last_login` INT(11) UNSIGNED DEFAULT NULL,
    `active` TINYINT(1) UNSIGNED DEFAULT NULL
) 
ENGINE = InnoDB;
like image 2
BoltClock Avatar answered Oct 21 '22 10:10

BoltClock