Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using backquote/backticks for mysql queries

Tags:

sql

mysql

I have building MYSQL queries with backticks. For example,

SELECT `title` FROM `table` WHERE (`id` = 3) 

as opposed to:

SELECT title FROM table WHERE (id = 3) 

I think I got this practice from the Phpmyadmin exports, and from what I understood, even Rails generates its queries like this.

But nowadays I see less and less queries built like this, and also, the code looks messier and more complicated with backticks in queries. Even with SQL helper functions, things would be simpler without them. Hence, I'm considering to leave them behind.

I wanted to find out if there is other implication in this practice such as SQL (MySQL in my case) interpretation speed, etc. What do you think?

like image 467
treznik Avatar asked Oct 12 '09 18:10

treznik


People also ask

Can you use Backticks in SQL?

Backticks are used in MySQL to select columns and tables from your MySQL source. In the example below we are calling to the table titled Album and the column Title . Using backticks we are signifying that those are the column and table names.

Can I use double quotes in MySQL?

Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.

When should I use quotes in MySQL?

QUOTE() : This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash.

Can I use double quotes in SQL?

Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes.


1 Answers

Backticks also allow spaces and other special characters (except for backticks, obviously) in table/column names. They're not strictly necessary but a good idea for safety.

If you follow sensible rules for naming tables and columns backticks should be unnecessary.

like image 58
James Cronen Avatar answered Sep 23 '22 18:09

James Cronen