MySQL syntax error, underscore - mysql

Executing this query
INSERT INTO classes( '_fkUserID', 'date', 'time' )
VALUES (
'1', '2017-07-04', '8:15'
)
gives me the following 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 ''_fkUserID', 'date', 'time') VALUES ('1', '2017-07-04', '8:15')' at line 1
I'm assuming the error has something to do with parsing the underscore, but I can't manage to find a way around it.

Change
classes('_fkUserID', 'date', 'time')
To
classes(`_fkUserID`, `date`, `time`)
Single quotes makes the fields as strings
You don't necessarily need to add the backticks on the column names unless they are Reserved Words to the MySql. In your sql statement, date and time are reserved words, so you must use the backticks on it, which means that it would also work as:
classes(_fkUserID, `date`, `time`)
Backticks are Identifier Quote Characters which means that its purpose is to make MySql understand that it should identify whatever is within it as an identifier, in your case a column name.

It is actually:
INSERT INTO classes(_fkUserID,date,time)
VALUES (
'1', '2017-07-04', '8:15'
)

Try removing the single quotes and use back tick symbol instead (

Related

Insert data partially into a table

I am trying to insert into my table something with partial data and leaving the rest default but somehow it keeps saying wrong syntax
My query is like this:
INSERT INTO day
(1030, 1100, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
#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 '1030, 1100, date, tech) VALUES('356-635-3633', '356-635-3633', '2019-04-07', 'Th' at line 1
A query like this works though:
INSERT INTO day (date, tech) VALUES ('2019-04-07', 'Thy')
The datatype for all those columns are varchar(30)
You would need to quotes these all-digits identifiers, using backticks;
INSERT INTO day
(`1030`, `1100`, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
From the documentation:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Demo on DB Fiddle (you can uncomment the original column list to generate the error).
NB: day and date correspond to names of MySQL functions. Using them like you do do not generate error, but it would still be a good idea to surround them with backticks a well, just to avoid any ambiguity, hence:
INSERT INTO `day`
(`1030`, `1100`, `date`, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')

Mysql error in my syntax [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 7 years ago.
Hey i have an error in my mysql syntax, and i dont know why i get it. I can't see an error in my code and i have checked the web for help...
INSERT INTO downloads_log
(file,by,time) VALUES
(1,1, NOW())
The error:
'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 'by, time) VALUES (1,1,NOW())
by,file and time are reserved keywords used in MYSQL. Use a back tick (`) to escape the keywords in your query. Change your query to :
INSERT INTO downloads_log
(`file`,`by`,`time`) VALUES
(1,1, NOW())
by is a reserved keyword. Try:
INSERT INTO downloads_log
(`file`, `by`, `time`) VALUES
(1, 1, NOW())
You're using reserved keywords as column names. Try to avoid using reserved keywords as column names, but if you haven't choice you have to pass them to backticks `` in following:
INSERT INTO downloads_log (`file`,`by`,`time`) VALUES (1,1, NOW())

#1064 - You have an error in your SQL syntax

SQL query i'm running in phpMyAdmin :
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES ([1],[Example Company],[image.jpg])
phpMyAdmin generates this, i'm just changing the values.
SQL database:
1 companyId int(100)
2 companyName varchar(100)
3 companyImage varchar(100)
Error I get:
#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 '[1],[Example Company],[image.jpg])' at line 1
Use quotes, not brackets, around strings. Integers and floats don't require anything but MySQL allows quotes around those as well.
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES (1, 'Example Company', 'image.jpg')
Use backticks to escape column and table name.
Use quotes to limit strings.
Use nothing to limit numbers.
Don't use brackets at all in MySQL.

You have an error in your SQL syntax

I am getting 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 '"insert into hall_booking(name,address,event_type,hallNo,arrival_time,arrival_da' at line 1
When running this query:
insert into hall_booking (
name,address,event_type,hallNo,arrival_time,arrival_date,dep_time,dep_date,identity,emailid,contact,total_members,desc,catering,service,decoration,other
) values (
'$nm','$add','$typ','$roomno','$arrv','$arrivaldate','$departure','$dDate','$idt','$emailid','$cont','$desc','200','200','200','200')
Again a problem occured in sql statement. What mistake i did?
You look as if you're using protected keywords as column names (you shouldn't really do this).
Try escaping them with `
insert into hall_booking (
name,address,event_type,hallNo,arrival_time,
arrival_date,dep_time,dep_date,`identity`,
emailid,contact,total_members,`desc`,catering,service,decoration,other)
values(
'$nm','$add','$typ','$roomno','$arrv',
'$arrivaldate','$departure','$dDate','$idt',
'$emailid','$cont','$desc','200','200','200','200')
Try this:
insert into hall_booking (name,address,event_type,hallNo,arrival_time,arrival_date,dep_time,dep_date,identity,emailid,contact,total_members,desc,catering,service,decoration,other)
values ( '".$nm."','".$add."','".$typ."','".$roomno."','".$arrv."','".$arrivaldate."','".$departure."','".$dDate."','".$idt."','".$emailid."','".$cont."','".$desc."','200','200','200','200')"
You are getting this error because you are using a reserved keyword desc as a column name. To overcome this error you can replace your query with the following one:
insert into hall_booking (name,address,event_type,hallNo,arrival_time,arrival_date,dep_time,dep_date,`identity`,emailid,contact,total_members,`desc`,catering,service,decoration,other) values ('$nm','$add','$typ','$roomno','$arrv','$arrivaldate','$departure','$dDate','$idt','$emailid','$cont','$desc','200','200','200','200')"
Also you can refer this article for more clarification:
How do I escape reserved words used as column names? MySQL/Create Table
I think you have not mentioned last value
"INSERT INTO `hall_booking`(`name`,`address ,`event_type`,`hallNo`,`arrival_time`,`arrival_date`,`dep_time`,`dep_date`,`IDENTITY`,`emailid`,`contact`,`total_members`,`DESC`,`catering`,`service`,`decoration`,`other`)
VALUES('".$nm."',
'".$add."',
'".$typ."',
'".$roomno."',
'".$arrv."',
'".$arrivaldate."',
'".$departure."',
'".$dDate."',
'".$idt."',
'".$emailid."',
'".$cont."',
'".$desc."',
'200',
'200',
'200',
'200',
'Last Value')"
You have a field which is a reserved word... desc.
Try to enclosed it with '`' character so it should be `desc`.
mysql>
insert into hall_booking
(name,address,event_type,hallNo,arrival_time,
arrival_date,dep_time,dep_date,identity,emailid,contact,total_members,
`desc`,catering,service,decoration,other) values
('$nm','$add','$typ','$roomno','$arrv','$arrivaldate','$departure','$dDate',
'$idt','$emailid','$cont','$desc','200','200','200','200')

query not work for single quote apostrophe

phpmyadmin query not work for single quote / apostrophe.
Not Work
ALTER TABLE 'about_team' CHANGE 'position' 'pp' INT( 11 ) NOT NULL
Work:
ALTER TABLE `about_team` CHANGE `position` `pp` INT( 11 ) NOT NULL
Same query but not work, gives 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 ''about_team' CHANGE 'position' 'pp' INT(11) NOT NULL' at line 1
it is because when you using single quote, it just means it is a STRING. Whereas BACTICK (the second query) means escaping a column.
'about_team' is not equal with `about_team`
'about_team' is STRING
`about_team` is a Table Name
Actually backticks enclosing the names are optional since the names used where not on MySQL Reserved Keyword List.
MySQL Reserved Keywords
Usually, single quotes are used around values while backticks are for table names and column names.