0

Possible Duplicate:
Using backticks around field names
What are the differences between backtick and single quote? Can I use IF statement in a query as above?

I just spend a long time figuring out an issue i had qith an sql query and i wanted some more information as i cant seem to find any information on the topic elsewhere.

I had a query

INSERT INTO prep_items ('id' ,'description' ,'price' ,'gst' ,'job' ,'stores' ,'item_number' ,'decimal') 
VALUES ('NULL', 'TESTPRODUCT', '22.99', '1', 'PK', 'AB', '666', '1'
);

however this kept returning a syntax error with the query. After racking my brain for a while and comparing the query to the query that phpmyadmin generates to insert to the db i noticed that the apostrophes in the field name were actually ` the symbol on the same key as the tilda (~).

What is the difference between the two symbols? And where should the ` be used as opposed to the ' (apostrophe).

Thanks in advance.

(i also noticed that if i change the ''s to ` in the values list i get an error saying unknown collumn 'valuename' which leads me to believe it is only for the column list, but where can i find more information?)

Thanks again

Community
  • 1
  • 1

1 Answers1

2

The backtick ` quotes identifiers such as table names and column names. It is usually optional to quote identifiers, but it is necessary if the name contains special characters (e.g. spaces) or if the name is a reserved word (e.g. desc).

SELECT id, `desc` FROM yourtable

The single quote ' delimits string literals.

SELECT * FROM yourtable WHERE name = 'Foo'
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • use backtick-backtick-space-backtick ;-) – zerkms Oct 03 '12 at 09:58
  • That explains it! Added a new column named 'decimal' to my sql table. Previously i hadn't had any back ticks and everything was working fine and then everything stopped working... Perfect response. Thanks again. As it turns out 'decimal' is a MySql reserved word! – Ahron אהרון Eisman Oct 04 '12 at 15:55