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 (`)
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 1 year ago.
I'm trying to create a table in my schema (mydb) called employee, but whenever I try running the script to create the table I get error #1064.
I'm new to SQL so I was looking at examples on how to create tables and went off those examples to create my own until I bumped into this error. Could anyone explain why I am getting this error message please?
MySQL Workbench Community 8.0 Version 8.0.26:
CREATE TABLE 'mydb'.'employee'
(
'emp#' INT PRIMARY KEY,
'firstname' VARCHAR(20) NOT NULL,
'surname' VARCHAR(20) NOT NULL,
'salary' INT default 0,
'city' VARCHAR(20),
UNIQUE(firstname, surname)
);
Error:
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 ''mydb'.'employee' ( 'emp#' INT Primary key, 'firstname' VARCHAR(20) NOT NULL, 's' at line 1
Your issue is the because you are adding ' single quotes in your CREATE TABLE statement, you need to do it like this:
CREATE TABLE mydb.employee
(
emp INT PRIMARY KEY,
firstname VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
salary INT default 0,
city VARCHAR(20),
UNIQUE(firstname, surname)
);
If you need the quotes in Mysql you need to use the curly single quote like this:
CREATE TABLE `mydb`.`employee`
(
`emp_id` INT PRIMARY KEY,
`firstname` VARCHAR(20) NOT NULL,
`surname` VARCHAR(20) NOT NULL,
`salary` INT default 0,
`city` VARCHAR(20),
UNIQUE(firstname, surname)
);
Also instead of using #, use _id or no you can check the following reference about MySQL Naming Rules
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
This question already has answers here:
Equivalent of MSSQL IDENTITY Column in MySQL
(2 answers)
Closed 9 years ago.
I have a sql-server query and i want to run it (or create an equivalent query) on mysql. But currently i'm getting syntax error. Can anyone help me to create a mysql equivalent of the below mentioned sql-server query?
create table Emp(EmpName varchar(20) not null,keyword varchar(20) not null,
DOB datetime not null,Comments text(65535),EmpId int primary key IDENTITY(1,1));
Following is the 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 'IDENTITY(1,1))' at line 1
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535), PRIMARY KEY (EmpId)
);
You can use auto_increment .By default it will increase by 1.If you want any other increment you can specify your own.
It should be defined as auto_incremented primary key instead. For example:
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535),
PRIMARY KEY (EmpId)
);
You can use an auto-increment field
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')
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