Create table Order using MySQL & MariaDB giving 1064 error - mysql

I'm trying to create a table called Order using MySQL and MariaDB. I've stripped down everything except an id for the table. If I change the table name to something like Test, it works, but creating the table as Order, 'Order', or "Order" does not work.
SQL:
CREATE TABLE IF NOT EXISTS Order (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE = InnoDB;
Error message:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Order ( id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE = InnoDB' at line 1
But this works:
CREATE TABLE IF NOT EXISTS Test (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE = InnoDB;
MySQL version output:
mysql Ver 15.1 Distrib 5.5.33a-MariaDB, for debian-linux-gnu (x86_64) using readline 5.1

Use backticks, because order is a reserved word:
CREATE TABLE `Order` ....
or better, don't do this. It will create confusion.

It's a reserved word. See the list of reserved words at http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
If you need to use a reserved word as a table name or column name, put it in back quotes. But you're better off avoiding reserved words, because they will cause much confusion and many mistakes in the future.

Related

Problems with mysql CREATE TABLE syntax

I try to create table in mysql with the following command:
CREATE TABLE keys (id INT(10), key VARCHAR(100));
and it always gives me an error like this:
ERROR 1064 (42000): 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 'keys (id INT(10), key VARCHAR(100))' at line 1`
So both the table name keys and the field key are reserved in the Mysql namespace. If you choose a different table name (e.g. keys_tbl) and rename the second field something like key_id, your code will work.
You should be very carefully with table and columns naming, you might face a lot of problems in the future.
I suggest find names that are not MySQL reserved words
If you still want to keep the names you should put in in backticks.
mysql> CREATE TABLE keys (id INT(10), key VARCHAR(100));
ERROR 1064 (42000): 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 'keys (id INT(10), key VARCHAR(100))' at line 1
mysql> CREATE TABLE `keys` (id INT(10), `key` VARCHAR(100));
Query OK, 0 rows affected, 1 warning (0.97 sec)
mysql> show create table keys;
ERROR 1064 (42000): 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 'keys' at line 1
mysql>
mysql>
mysql> show create table `keys`;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| keys | CREATE TABLE `keys` (
`id` int DEFAULT NULL,
`key` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
Note that you have to use backticks when using the table

mysql error for a create table statement from shell client

Im trying to run the following from the mysql shell client:
MySQL localhost:33060+ ssl plaything SQL > CREATE TABLE areas (id SERIAL PRIMARY KEY, area_name VARCHAR);
and keep getting the following error:
ERROR: 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 ')' at line 1
where could I be going wrong?
solved by yoss:
try this: CREATE TABLE areas ( id int NOT NULL AUTO_INCREMENT, area_name varchar(255), PRIMARY KEY (id) );
varchar did not have number of characters specified in the original statement

Error 1064 when updating a table

When updating a table I get an error:
My table is
speech
values:
no speec check
1 45 0
and the structure is :
1 -no int(100) No None AUTO_INCREMENT Browse distinct values Change Drop Primary Unique Index Fulltext
2- speec varchar(100) latin1_swedish_ci No None Browse distinct values Change Drop Primary Unique Index Fulltext
3 - check int(100) No None Browse distinct values Change Drop Primary Unique Index Fulltext
when updating using the command :
UPDATE speech set check=1 where no=1
I get the error:
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 'check=1 where no=1' at line 1
Please help me in this issue
CHECK is a reserved, word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
You need to escape it using backtics as
UPDATE speech set `check`=1 where no=1

Insert Column Name into Table if Column Name does not already exist

IF COL_LENGTH('Characters', 'name') IS NULL
BEGIN
ALTER TABLE `Characters` ADD `name` INT(32) UNSIGNED NOT NULL;
END
I am trying to write some sql that will insert a column name into the table "Characters" if it is not already existant, however I am getting errors which I do not understand (quite new to SQL) and I could use some help understanding why it is not working. I have gotten the IF part from another Question, and the ALTER part from the DBMS I'm using, PhpMyAdmin 2.11.4.
It is using MySQL 5.6 apparently and this is the error:
MySQL said: Documentation
#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 'IF COL_LENGTH('Characters', 'name') IS NULL
BEGIN
ALTER TABLE `Characters` AD' at line 1

MySQL create Table error on phpMyAdmin

Creating a database for a class in PHP and MYSQL. Running into an error when creating the database, phpMyAdmin gives me a useless error message. (Software that gives a better error message would be awesome BTW)
phpMyAdmin error:
#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 'CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL auto increment, fir' at line 1
USE isys288_gottfrk
CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL
auto increment, first_name carchar(20) NOT NULL, last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id), INDEX full_name (last_name, first_name) ) engine =
innodb;
There were several errors in your code.
USE isys288_gottfrk;
CREATE TABLE customers
(
customer_id INT UNSIGNED NOT NULL auto_increment,
first_name varchar(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id),
INDEX full_name (last_name, first_name)
) engine = innodb;
I suggest you use a DB tool like MySQL Workbench to generate your tables. Then you can see the problems right away.
Juergen d edited your question for formatting, this might have helped you as well: Your initial statement was all on one line, so the error was flagged in that one line. If you edited it on multi lines (like in your question, or better in his answer) you might have gotten a better message, e.g. one that flags that the line containing carchar(20) contains a problem (it's even better the way he has it in his answer, but it correctly uses varchar(20), not flagging a problem)
Yes, it would be nice if error messages would immediately flag the problem, but sometimes it's already better when the input is formatted in a more human-readable way. I've tested the same with a multiline statement, mysql stated
ERROR 1064 (42000): 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 'carchar(20)
Try this:
CREATE TABLE customers (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, first_name VARCHAR (20) NOT NULL
, last_name VARCHAR (40) NOT NULL
, PRIMARY KEY (customer_id)
, INDEX full_name (last_name, first_name)
) ENGINE = INNODB ;
You can use some GUI clients like SQLyog for this.