error in mysql statement - mysql

I cannot figure this one out. All the variables are ok. Printed out the sql statement before executing in php... This is the statement exactly as it is sent to be ran by php
INSERT INTO 'images' ('filename', 'creator', 'date', 'notes') VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Thank You!

You're enclosing the table/field names in single-quotes. You need to do it with back-ticks (or nothing, depending on the name).
INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES ('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs')
Just in case you're interested, a list of reserved names (which must be quoted if used as a table or column name) can be found here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.
Also, to find out what characters may be included in an unquoted name and which can appear only as part of a quoted name, see here: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html.
N.B. the relevant MySQL version.

Your table and field names should either be in backquotes (`) or unquoted.

INSERT INTO `images` (`filename`, `creator`, `date`, `notes`) VALUES
('cat.sdf', 'michaelamici', '2002-07-05', 'SDfdddfdffddffdfgs');
Try editing single quotes to backquotes for table name and field names

Related

Can a backticked identifier have a backtick in it?

Is it possible to escape a SQL identifier in either MySQL or BigQuery or any other RDBMS that allows quoting of identifiers with the ` character? For example:
select 1 as `select`
Works, but then How would I add a literal backtick to it, or is that just not allowed?
select 1 as `sel\`ect`
Yes. Use a doubled backtick, eg:
create table `my``table` (`my``id` int);
This is similar syntax for including quotes in text, eg 'O''Leary'
See live demo of
create table `my``table` (`my``id` int);
insert into `my``table` (`my``id`) values (1);
select * from `my``table`;
outputs:
my`id
1
By the way, the documentation says any UTF-8 character (except U+0000) is allowed in a back tick quoted identifier.

MySQL INSERT INTO "(" is not valid at this position for this server version, expecting SELECT, SET, VALUES, TABLE etc

I have created a database in MySQL and am trying to insert values into a table called Artist but keep receiving the SQL error in the title. This is the query I am trying to execute
insert Artist (ArtistId, name, genre, social media, G_id)
Values ('911', 'Alessandro Torlonia', 'Ancient Greek/Roman', '', '734')
I have also tried executing INSERT INTO before my table name and have run out of ideas to make it work. Do I need to update MySQL workbench or any other suggestions?
The standard insert syntax goes like:
insert into Artist (ArtistId, name, genre, social_media, G_id)
values (911, 'Alessandro Torlonia', 'Ancient Greek/Roman', NULL, 734)
Notes:
presumably, ArtistId and G_id are of a numeric datatype (such as INT), not strings (VARCHAR or the-like); if so, I would recommend not surrounding the values with single quotes. Using strings won't raise errors (the database handles the conversion for you under the hood) but it is good practice to use the proper datatype
social media probably needs an underscore in between; or, if you do have a space in the column name, you need to surround the column name with backticks. Note that I assigned a NULL value to instead of the empty string; this is usually the preferred way to represent the absence of data (and it is valid for all datatypes, unlike the empty string, that only string-like datatypes support)

DECODE Function in SQL

I am trying to insert the following query and I get syntax errors. Can you please help me with the below query:
INSERT INTO ABCTABLE (COLUMN1) values ('DECODE(MDSE_CD,NULL,'0000000000000000',LPAD(TO_NUMBER(MDSE_CD,'16',' '))');
Since you haven't really said anything other than "this query doesn't work, fix it", I have to take a stab in the dark what you want. From the query you have, I'm therefore guessing you want the value of the column to be DECODE(MDSE_CD,NULL,'0000000000000000',LPAD(TO_NUMBER(MDSE_CD,'16',' '))
In which case, you have to escape the single quotes within your string literal. Do this by doubling up the quotes:
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('DECODE(MDSE_CD,NULL,''0000000000000000'',LPAD(TO_NUMBER(MDSE_CD,''16'','' ''))')
Try properly escaping the inner single quotes
INSERT INTO ABCTABLE (COLUMN1)
VALUES ('**DECODE**(MDSE_CD,NULL,''0000000000000000'',**LPAD**(TO_NUMBER(MDSE_CD,''16'','' ''))');
The problem is the use of quote marks. If we tried to break up your query it would look like this:
INSERT INTO ABCTABLE
(COLUMN1)
values
(
'DECODE(MDSE_CD,NULL,'
0000000000000000
',LPAD(TO_NUMBER(MDSE_CD,'
16
','
'))'
);
...which clearly makes no sense.
You might want to think about how to escape a quote mark inside a string.
Sql Server:
DECOD function in Sql Server can be replaced with CASE construct
LPAD function in Sql Server has not a direct correspondence but you can pad your string using string manage function REPLACE (replicate a character a number of specified times)
My Sql:
DECOD function in MySql can be replaced with CASE construct
LPAD function in MySql is existent
What do you want to store... a string literal 'DECODE(MDSE...))', or did you want to call a function to derive a value?
To store a string literal containing single quotes, you need to "escape" each single quote within the string with an extra single quote, e.g.
O'Hare Int'l ==> 'O''Hare Int''l'
The DECODE function is Oracle specific. That expression will need to be rewritten using different functions in both MySQL and SQL Server.

Weird MYSQL backtick (Grave accent)

I have two tables in my database, info and comment and their structures are as follow:
info (id(int(10)), name(varchar(80)), ...19 other columns.., phone(int(16)));
comment (id(int(10)), name(varchar(80)), comment(varchar(80)), phone(int(16)));
When I execute these SQL queries:
INSERT INTO info (name, ...19 other columns.., phone) VALUES ('asa', ....., 123456)
It works perfectly fine.
But when it comes to INSERT INTO comment (name, address, phone) VALUES ('asa', 'asa', 123456), it reports SQL syntax error unless I surrounded phone the backtick like this `phone`
Honestly, I can get all the things done by adding the backtick, but I would like to why it works. Thanks in advance!
I would imagine there is a comma or something in the address that you are trying to insert and it is therefore trying to insert everything beyond that comma into the phone field.
Table structure name,comment, phone
Your sql command name,address, phone (address and comment are different)
Check that all columns and "insert into" names match between them at all.
The error message is important. Please post it if possible. It is also important to show the whole table column names and the whole "insert into" statement.

phpmyadmin sql apostrophe not working [duplicate]

This question already has answers here:
character for single quote
(1 answer)
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
hey guys was hoping you could help me out,
Not sure if I always had this problem or if its new, in phpmyadmin in the sql tab, the apostrophe it recognizes is different from what i type, for example,
when i type, it is
SELECT * FROM 'table'
this gives me an error, so instead I have to like copy/paste the inverted commas of some prebuilt query so that it looks like
SELECT * FROM `table`
see how the apostrophes are different? any way I can fix this?
also, i have seen many queries on the web, and i think even queries i call from php dont require table names to have apostrophes. But when write it in phpmyadmin, I can do queries without table names having apostrophes?
thanks in advance.
In MYSQL, table is a reserved keyword. If you want to use reserved keywords in mysql in query, you have to enclose them in backtick(`).
As table is reserved keyword you query should be
SELECT * FROM `table`
Regarding single quote ('), in mysql, it represents string value.
SELECT *, 'table' FROM `table`;
Demo
You should only need to quote table names when they conflict with a reserved word.
Also:
` = Grave accent, or (because someone needed to invent a word) backtick
' = Apostrophe, or straight single quote
You dont need apostrophe on table name.
You should use ` in cases that your table/field name is a reserve word eg:
SELECT `distinct`, myfields FROM mytable
note that distinct is an sql command so you need to put the `.
SELECT * FROM `table`
table here should be inside `.
There are two different characters, the backtick and the single quote. Table and column names can be surrounded by the backtick, strings can be surrounded by quotes. There is nothign to fix :D