MYSQL - Set/Update Syntax [duplicate] - mysql

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I have a table like this:
Why is this command not working:
UPDATE 'stitch' SET 'claim-time'='20' WHERE 'group'='010000'
I get the 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 ''stitch' SET claim-time='20' WHERE group='010000'' at line 1
Everything in the table is text.

group is a reserved keyword in mysql so use backticks to escape it
`group`
Also you are selecting the string as column name, correct format is
UPDATE `stitch` SET `claim-time`='20' WHERE `group`='010000'

Try removing the single quotes from stitch, claim-time and group. Either leave them out or use backquote `. The comma is used for strings, not table and field names.
Also, I don't know what data type claim-time and group are. If they are numeric (int, bigint, etc) and not string (varchar, text, etc) then you'll need to remove the single quotes from those too.
update stitch set claim-time=20 where group='0100000'; # assuming group is a string data type

Try this.
UPDATE TableName SET claim-time='20' WHERE group='010000';
That is considering if claim-time is a varchar datatype. If it's a number just remove the quotes.
Remember to avoid reserved names such as field names such as name, password, group, user and stuff just to be safe. Make it user1, group1 instead or something like these.

Related

SQL syntax inserting into table [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 months ago.
This line of code is no working, and I cannot for the life of me figure out what is wrong with it.
INSERT INTO teacher (email,password,admin) VALUES (deborah68#example.org,d%6AsQPq7y,1);
this wont run and says the error is near the end of the line
any help is appreciated, the schema is called at3
Why are you not enclosing the string/char data in single quotes ? Assuming they are of datatype varchar, you simply enclose them in single quotes and the command will work.
INSERT INTO teacher (email,password,admin) VALUES ('deborah68#example.org','d%6AsQPq7y',1)
Assuming your email and password are datatype of varchar, try putting '' between the values, or better you can use parameterized value if it's any backend code, try:
INSERT INTO teacher (email,password,admin) VALUES ('deborah68#example.org','d%6AsQPq7y',1);

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 'FROM [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I get this error at this code:
SELECT "LastUpdate" ;
FROM "xx_yy";
Is LastUpdate a reserved word ?
I tried to change " " to `` or delete them, I don't really know the perfect combination to make it work. I'm beginner in this.
Get rid of the quotes around your column identifier and tablename. That makes them strings instead of identifiers. Either use ticks or nothing at all. Also, ditch the semi-colon after the first line as it is terminating your query before it reaches the FROM clause.
SELECT `LastUpdate`
FROM `xx_yy`;
or
SELECT LastUpdate
FROM xx_yy;
A semicolon (;) signifies the end of a statement. So you actually have two separate, distinct statements:
SELECT "LastUpdate"
FROM xx_yy
The second statement is not valid, which is why you are seeing the error.
Solution: Remove the semicolon at the end of the first line:
SELECT "LastUpdate"
FROM "xx_yy";
Also note if the ANSI_QUOTES sqlmode is not enabled, MySQL treats double-quotes as string literals (the same as single quotes). You may need to change these to the MySQL-specific backtick, or remove them entirely:
SELECT `LastUpdate`
FROM `xx_yy`;
Remove the first semicolon.
SELECT FOO FROM BAR
The above is all one statement.
Most likely your query should look like
SELECT "LastUpdate" FROM "xx_yy";
; is marking an end of a query.

How to access table when the name is a keyword [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 8 years ago.
I just realized that i assigned a table name as "AS" and when i was trying to do a select query, i kept getting an 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 '"as"'.
So i looked up for reserved words and found out that "AS" is reserved. Well, i think i knew before (used for aliases) but just didn't consider it.
So to fix this will easily be to rename the table name. But assuming i don't want to, can i still access this table using some sort of symbol ? i tried putting it in quotes and double quotes but no success.
With mysql, you can wrap reserved words (or any words for that matter) in backticks to cause word to be parsed as a literal name rather than the keyword:
select * from `AS`
Read more about it in the on line documentation under "identifier quote character".
Yes use this(backtiks):
SELECT * FROM `as`
You should use backticks. Else MySQL will consider it as a Keyword. If we use backtick then they are called quoted identifiers and they tell the parser to handle the text between them as a literal string. They are useful for when you have a column or table that contains a keyword or space.
Please refer: http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html

Error in MySQL Query (Banned Word?)

I have an MySQL query, which returns an error message. I think it could be due to the word "out". Normally, I would just change the field name but I am working on some software that I am not used to and I don't know how much of a change that would be. So, I want to be sure if I have to.
Here is the query:
SELECT * FROM probid_bids WHERE auctionid=73 AND out=0 AND invalid=0
Here the error message:
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 'out=0 AND invalid=0' at line 1
OUT is indeed a reserved word. You can encase the column names in backticks to quote the names, and thus avoid this problem, like so:
SELECT * FROM probid_bids WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0
OUT is a reserved word (it is used to specify the type of parameters -- IN, OUT, INOUT -- when creating procedures). Try enclosing it inside backticks (`).
The rules regarding how and when to quote the identifiers (table names, column names, etc) are described here.
Note: certain MySQL configurations allow you to use double quotes as well but this should be avoided; stick with using backticks to quote identifiers and single quotes to quote strings.
Escape the keys:
SELECT * FROM `probid_bids` WHERE `auctionid`=73 AND `out`=0 AND `invalid`=0

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