SQL Syntax
The SQL syntax is quite an easy one to grasp. Most of the actions you need to perform are done with a SQL statement.
Generally, a SQL statement begins by stating what to do (for example, SELECT
), then states which object to do it to (for example, using the FROM
clause).
It may also have a condition added to the end (for example, with a WHERE
clause).
SQL is not case sensitive — the above examples could just have easily used all lowercase or all uppercase. Different programmers have their own preferences. For readability purposes, many SQL programmers prefer to use uppercase for SQL commands and lowercase for everything else.
The SQL syntax allows you to include line breaks at logical points without it breaking the statement. For example, the above example could have been written all on one line — or across 4 lines.
Also, a semicolon should be placed at the end of each SQL statement. There are some exceptions where a semicolon may be omitted, however, it's good practice to include them unless you have good reason not to.
DML & DDL
SQL is divided into two main categories; Data Manipulation Language (DML), and Data Definition Language (DDL). An explanation follows.
Data Manipulation Language (DML)
DML enables you to work with the data that goes into the database. DML is used to insert, select, update, and delete records in the database. Many of your SQL statements will begin with one of the following commands:
SELECT
- Retrieves data from the databaseINSERT
- Inserts new data into the databaseUPDATE
- Updates existing data in the databaseDELETE
- Deletes existing data from the database
Data Definition Language (DDL)
You may also occasionally need to create or drop a table or other datbase object. SQL enables you to do this programatically using DDL.
Examples of DDL commands:
CREATE DATABASE
- Creates a new databaseALTER DATABASE
- Modifies the databaseDROP DATABASE
- Drops (deletes) a databaseCREATE TABLE
- Creates a new tableALTER TABLE
- Modifies the tableDROP TABLE
- Drops (deletes) a table
These are just some of the object classes that can be defined using DDL. As you can probably guess, the syntax is generally the same for any object, although, each object will have properties specific to that object class.
As you can see, the SQL syntax is quite simple. It is also very powerful syntax — you can do a lot of damage with one line of code!