SQL Syntax

Database table

A database typically contains one or more tables. Each table is identified by a name (such as "Customer" or "Order"). Tables contain records (rows) with data.

The following example is a table named "Persons":

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

The table above contains three records (each corresponding to a person) and five columns (Id, Last Name, First Name, Address, and City).

SQL statements

Most of the work you need to do on the database is completed by SQL statements.

The following statement selects the data from the LastName column in the table:

SELECT LastName FROM Persons

The result set looks like this:

LastName
Adams
Bush
Carter

In this tutorial, we will explain various SQL statements to you.

Important Matters

Be sure to remember that,SQL is case-insensitive!

Semicolon after SQL statements?

Some database systems require the use of a semicolon at the end of each SQL command. In our tutorial, we do not use the semicolon.

The semicolon is the standard method for separating each SQL statement in a database system, allowing multiple statements to be executed in the same request to the server.

If you are using MS Access and SQL Server 2000, you do not need to use a semicolon after each SQL statement, but some database software requires that a semicolon must be used.

SQL DML and DDL

SQL can be divided into two parts: Data Manipulation Language (DML) and Data Definition Language (DDL).

SQL (Structured Query Language) is a syntax used for executing queries. However, the SQL language also includes syntax for updating, inserting, and deleting records.

Query and update statements make up the SQL DML part:

  • SELECT - Retrieve Data from Database Table
  • UPDATE - Update Data in Database Table
  • DELETE - Delete Data from Database Table
  • INSERT INTO - Insert Data into Database Table

The SQL (Structured Query Language) DDL part allows us to create or delete tables. We can also define indexes (keys), specify links between tables, and impose constraints on tables.

The most important DDL statement in SQL:

  • CREATE DATABASE - Create New Database
  • ALTER DATABASE - Modify Database
  • CREATE TABLE - Create New Table
  • ALTER TABLE - Modify (Change) Database Table
  • DROP TABLE - Delete Table
  • CREATE INDEX - Create Index (Search Key)
  • DROP INDEX - Delete Index