About Me

Thursday, 19 April 2012

SQL - Syntax - (Speaking SQL)

SQL - Syntax - (Speaking SQL)

Syntax, by definition, means the study of linguistic rules and patterns. Every programming language, including SQL, must follow a unique set of guidelines termed syntax. Punctuation, spaces, mathematical operators, and special characters have special meaning when used inside of SQL commands and query statements. For example, each and every SQL command will end with a semi colon (;).

Executing SQL commands that do not have proper syntax and formatting will result in a syntax error. Syntax errors might be the most common and first error messages new SQL developers will experience.
Let's now take a look at a very simple SQL command that will be used in just about every example contained in this tutorial from here on out.

Sample SQL Command:

use mydatabase;
This command identifies a database as a target database, meaning that any SQL command or query executed after this line will be run against the identified database. In this case, the database 'mydatabase' will be the target database. This is a good way to prevent mistakes and avoid potential data loss and a good reason to include this command into each and every authored SQL query.

SQL - Syntax: Capitalization and Spacing

In some programming languages, capitalizing commands or excessive spacing may or may not cause syntax code errors and cause the command to fail. SQL syntax is very loose when it comes to capitalization and spacing, allowing a lot of room for the developer to decide on his/her own preference in regards to capitalization and spacing.
Let's rewrite the same SQL command from the previous example and take advantage of SQL's loose syntax characteristics.

Sample SQL Command:

USE


mydatabase;
The example above, though it does look different due to the capitalization and spacing, will yield the same results as the first example.

SQL - Syntax: Building good habits

While coding in any language, it is important to develop good, consistent habits and maintain clean code. Clean code allows another SQL developer to step right in where you have left off without missing a beat. A developer with diligent coding habits will prevent many syntax errors before executing his/her scripts and also will be able to detect possibly syntax problems before they cause problems in a SQL Command.
Good habits include:
  • Consitency
  • Clean and Concise
  • Use of Comments (more on this later)
  • Scalability
Coding in any language is as much of an art form as authoring best selling novels and stories. Take pride in doing so and always do your best to follow good coding habits.

0 comments:

Post a Comment