MYSQL syntax error - why is this happening? - mysql

I use mysql in my php scripts for more than 6 years but i never encountered error like this.
When i execute this SQL command:
SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (show=1) LIMIT 1
it throws me 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 'show=1) LIMIT 1' at line 1
The table structure is:
CREATE TABLE IF NOT EXISTS `discount_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`image` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`discount` float NOT NULL,
`price1` float NOT NULL,
`price2` float NOT NULL,
`bought` int(11) NOT NULL,
`target` int(11) NOT NULL,
`desc` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`link` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL,
`active` tinyint(1) NOT NULL,
`start` int(11) NOT NULL,
`end` int(11) NOT NULL,
`position` int(11) NOT NULL DEFAULT '1',
`show` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I don't get whats wrong. Obviously the 'show' field cause the problem but i already tried everything.. (there must be something wrong with show field because:
SELECT `discount_items`.* FROM `discount_items` WHERE (show=1) AND (active=1) AND (end<=1344007212) AND (position=1) LIMIT 1
throws
#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 'show=1) AND (active=1) AND (end<=1344007212) AND (position=1) LIMIT 1' at line 1
So the problem moves with the show field.
I am sorry if this is common problem but i googled and found nothing. This error is too global and doesn't explain anything to me.
Thanks for any help and tips!

show is a MySQL reserved word. If you are going to use it to reference a field name, you must use backticks around it like this:
SELECT `discount_items`.*
FROM `discount_items`
WHERE
(position=1)
AND (active=1)
AND (end<=1344007212)
AND (`show`=1) /* backticks added here */
LIMIT 1

show is a reserved word. Either change it or place it in ticks
SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (`show`=1) LIMIT 1

show is a MySQL reserved word. Enclose it in backticks to make it work.

Related

Sql syntax issue with adding values [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 2 years ago.
I have an error in my sql on second addSql() line:
$this->addSql('CREATE TABLE user (id INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, value VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (id,key,value) VALUES (1,'theKey','14')");
SQLSTATE[42000]: Syntax error or access violation: 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 'key,value) VALUES (1,'theKey','14')' at line 1
Can someone help to locate it..
The 'key` word is reserved word in MySQL. I advice to avoid using reserved as column names. Any way is it necessary use backticks to quote them:
$this->addSql('CREATE TABLE user (`id` INT AUTO_INCREMENT NOT NULL, `key` VARCHAR(60) NOT NULL, `value` VARCHAR(60) NOT NULL, UNIQUE INDEX UNIQ_E545A0C58A90ABA9 (`key`), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql("INSERT INTO user (`id`, `key`, `value`) VALUES (1,'theKey','14')");
Test it on SQLize.online
changing VALUE to VALUES should do the trick

mysql insert value in column : int DEFAULT 0 causing error

I am not able to find out what is the exact issue. When I remove index param from below mentioned query and try to insert the data it works fine, but trying to add index cause an error.
Here is my table schema:
CREATE TABLE IF NOT EXISTS `address_list` (
`email` varchar(100) NOT NULL,
`currency_name` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`label` varchar(100) NOT NULL,
`index` int DEFAULT 0,
`address_type` varchar(50) NOT NULL,
`balance` varchar(500) DEFAULT "0.0",
`timestamp` TIMESTAMP default CURRENT_TIMESTAMP
)
and here is my insert query :
insert into address_list (email,currency_name,address,label,index,address_type) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X','test address',3,'wallet');
error :
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 'index) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X'' at line 1
index is a reserved word in mysql; rename your column to something else, or else place it in backticks where you want to use it:
insert into address_list (email,currency_name,address,label,`index`,address_type) Values ('abc#gmail.com','bitcoin','mgbtUQt7ppCEhxWmvicxrbEDYSom5kNk8X','test address',3,'wallet');
See https://dev.mysql.com/doc/refman/8.0/en/keywords.html

MySQL Workbench giving Error 1064

I'm toying around with MySQL Workbench, using its tools to create my database. When attempting to forward engineer the database, I keep getting this error.
Executing SQL script in server
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 '-01-01,
`PlateNum` CHAR(7) NOT NULL DEFAULT 'ABCDEFG',
`CellPhone` INT(10) U' at line 9
SQL Code:
-- -----------------------------------------------------
-- Table `MetalDelivery`.`Drivers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `MetalDelivery`.`Drivers` (
`DriverID` INT NOT NULL AUTO_INCREMENT,
`FName` CHAR(12) NOT NULL DEFAULT 'First',
`LName` CHAR(12) NOT NULL DEFAULT 'Last',
`Sex` CHAR(1) NOT NULL DEFAULT 'M',
`DOB` DATE NOT NULL DEFAULT 1900-01-01,
`PlateNum` CHAR(7) NOT NULL DEFAULT 'ABCDEFG',
`CellPhone` INT(10) UNSIGNED NOT NULL DEFAULT 5550000000,
PRIMARY KEY (`DriverID`))
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
In the SQL script preview, it does show a semicolon after InnoDB
Any kind of date needs to be specified as if a string:
`DOB` DATE NOT NULL DEFAULT '1900-01-01'
It's also worth expanding this schema a little. Names are frequently over 12 letters long. VARCHAR(255) is a good default for "string" fields.
You may find that these defaults are a huge mistake. It's possible someone's actual last name is "Last" in which case it's like they're using a default, or they simply don't have a last name. NULL values serve a purpose, so embrace them.

SQL newbie needs to know what is wrong

Just started playing with MySQL and I've already made stupid mistake which is somewhere in there; that's what I need to figure out:
CREATE TABLE `txts` (
`ID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(75) NOT NULL,
`content` VARCHAR(MAX) NOT NULL,
`lastupdate` DATE NOT NULL default '0000-00-00',
PRIMARY KEY (`ID`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
It gives:
#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 'MAX) NOT NULL,
`lastupdate` DATE NOT NULL default '0000-00-00', PRIMARY KEY ' at line 4
I know it's extremely stupid but I can't see a single mistake in it.
VARCHAR(MAX)
MySQL does not support the use of MAX.
Use an real number value.
Per Google:
The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions.

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