Sql syntax issue with adding values [duplicate] - mysql

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

Related

How to create a mysql table [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
what's wrong with this? I'm new to MySQL/MariaDB.
CREATE TABLE 'keys_table'(
'_key' CHAR(8) NOT NULL,
'_product_id' VARCHAR(100) NOT NULL,
'_seller' VARCHAR(50) NOT NULL,
'_created_at' timestamp NOT NULL,
PRIMARY KEY ( '_key' )
);
ERROR 1064 (42000) at line 2: 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_table'('_key_code' CHAR(8), '_product_id' VARCHAR(100) NOT NULL, '_sel' at line 1
Thank you :)
Remove all single quotes:
CREATE TABLE keys_table(
_key CHAR(8) NOT NULL,
_product_id VARCHAR(100) NOT NULL,
_seller VARCHAR(50) NOT NULL,
_created_at timestamp NOT NULL,
PRIMARY KEY ( _key )
);
You shouldn't use quotes (') for the names of the table and columns, you can just do without or you can use backticks if you want (`)

MySQL Error Code 1064 from workbench when create a table

I am trying to create a table using MySQL workbench, but the error msg shows this error. Check online, it shows " often due to using reserved words, missing data in the database, or mistyped/obsolete commands. " But, I did not use any reserved words or missing data. So not sure why this error happens. Thanks!
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `assign`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`email` VARCHAR(128) NOT NULL,
`nickname` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`created_at` VARCHAR(45) NOT NULL DEFAULT 'CURRENT_TIMESTAMP()',
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ERROR 1064: 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 ')
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8' at line 8

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

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 5.3 - some error, but query seems ok

mysql query - 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 'a', '1') ON DUPLICATE KEY UPDATE count=count+1' at line 1
The query that fails:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'krwiopijcy', '1')
ON DUPLICATE KEY UPDATE count=count+1;
Is there anything wrong in my query?
count is a built in function and as such, may require care to be used as such. However, as the docs say, it is perfectly fine as column name. You might want to escape it using
`count`
That being said, I had no problem with your exact query (no escaping) using this table structure:
CREATE TABLE `tags` (
`ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`tag` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`count` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Your example from the comments has one apostrophe too much (behind 'umacku'), which doesn't seem to be the problem (as it is not given in the error message), but you should paste the exact queries:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'umacku'', '1')
ON DUPLICATE KEY UPDATE count=count+1;