What am I missing? MySQL syntax error - mysql

I am having this 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 ''tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'd' at line 1
From this statement:
CREATE TABLE 'tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
); ENGINE=MyISAM;
Why?

You need the backtick instead of the single quote ('). The backtick is this character:
`
Better yet - don't bother with either:
CREATE TABLE tablename (
id MEDIUMINT ...
Important: also see the comments below from tadman; they round out this answer nicely by explaining the backticks and pointing out another syntax issue.

You are using incorrect notation.
In your create table statement, you are using the single quote ( ').
You can't use that here for table names and column names.
Alternatives would be the tick mark (`). Or just completely remove all notation altogether.
Here is your code, fully functional:
CREATE TABLE tablename (
`id` MEDIUMINT NOT NULL,
`content`TEXT NOT NULL,
`date_added` DATETIME NOT NULL,
`user` VARCHAR (16) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`id`)
);

You are missing a space between the columnname and the type of the first two columns. Also, you have a ; to many at the end
CREATE TABLE 'tablename'(
'id' MEDIUMINT NOT NULL AUTO_INCREMENT,
'content' TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
) ENGINE=MyISAM;

Related

Table cannot be created in mysql -Error 1064

I am trying to create a table in MySQL with the query
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rank TINYINT NOT NULL,
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rank),
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.
Error coming up 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 'rank TINYINT NOT NULL, groupName
VARCHAR at line 3
MySQL 8.0.2 added support for the window rank function, making it a reserverd word.
You could escape it using backticks (`):
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
`rank` TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, `rank`), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rosterRank TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rosterRank), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);

Error creating table: You have an error in your SQL syntax;

I'm trying to make some tables in my database using this.
but I get this error:
Error creating table: 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 'UNSIGNED AUTO_INCREMENT, ip VARCHAR(65) NOT NULL, attempts INT(11) ' at line 2
on this:
$dbprefix = 'duh_';
$prefixloginAttempts = $dbprefix . 'loginAttempts';
$table = array();
$table[] = "CREATE TABLE IF NOT EXISTS $prefixloginAttempts (
id INT(11) NOT NULL UNSIGNED AUTO_INCREMENT,
ip VARCHAR(65) NOT NULL,
attempts INT(11) NOT NULL,
lastLogin DATETIME NOT NULL,
username VARCHAR(65) NOT NULL,
mod_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE KEY 'id_UNIQUE' ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
foreach ($table as $sql) {
$query = $conn->query($sql);
}
Someone that can see where the fail is? I cant..
The error messages provided by MySQL contain the part of the query where the parsing stopped. The error is usually either on the first word from the part of the query listed in the ... near "..." message or just before it.
On this query, the problem is on the UNSIGNED keyword. It is part of the data type and it must stay right after with INT(11):
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
Read about the syntax of the CREATE TABLE statement.
There are also errors on the definitions of indexes. The column names are enclosed in apostrophes and this makes them strings and not object names.
The object names can be left unquoted or, if they are also SQL keywords or MySQL reserved words then they must be enclosed in backticks (`):
PRIMARY KEY (id),
UNIQUE KEY id_UNIQUE (id)
or
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
Read when to use single quotes, double quotes or backticks in MySQL.

SQL query syntax error while creating a table

I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle

What's wrong with this MySQL query -- db won't accept?

I have the following MySQL query:
INSERT INTO 12:12:12:12:12(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip) VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0')
The name of the table is "12:12:12:12:12".
Here is the schema:
"CREATE TABLE IF NOT EXISTS `$mac` (
`timestamp` int(11) NOT NULL,
`niceTime` varchar(20) NOT NULL,
`temperature` float NOT NULL,
`relative_humidity` int(11) NOT NULL,
`wind_speed` float NOT NULL,
`gust_speed` float NOT NULL,
`rain_mm_per_hour` float NOT NULL,
`nsew` int(11) NOT NULL,
`str` varchar(1000) NOT NULL,
`ip` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;"
No matter what I do, I cannot get the query to be accepted ;(
Query failed: 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 '12:12:12:12:12(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_' at line 1
Many thanks in advance,
you will use backticks like that to your table name
12:12:12:12:12
try this
INSERT INTO `12:12:12:12:12`(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip) VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0'
EDIT.
Rules for naming objects, including tables in MySql:
http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
Identifiers may begin with a digit but
unless quoted may not consist solely
of digits.
The identifier quote character is the backtick (“`”):
Use backticks around identifiers, especially when using such unconventional table names:
INSERT INTO `12:12:12:12:12`(timestamp,niceTime,temperature,relative_humidity,wind_speed,gust_speed,rain_mm_per_hour,nsew,str,ip)
VALUES(1361707978,'2013-02-24T12:12:58+00:00',0.0,0,0.0,0.0,0.0,0,'1010101010101010','0')

MySQL issue with column name "range" [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
When I try to run this script I am getting an error at "range" if I remove or change the name I can run the script however the application need this column to store data. Any idea how to insert this into MySQL?
Error Code:
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 '"range" CHAR(5) NOT NULL, range_max_value NUMERIC(18,3)' at line 22
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
"range" CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
Range is a reserved keyword that needs to be escaped with backticks.
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
`range` CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
You should take a look at the reserved words list. Range is one of those words. You need to escape it with tick marks:
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
`range` CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
RANGE is a reserved word. http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html