SQL Syntax rules
- SQL statements always start with a keyword.
- The SQL statement ends with a semicolon.
- SQL is case insensitive, meaning update is the same as update
Database tables
A database typically contains one or more tables. Each table is identified by a name (for example, "Customers" or "Orders"). The table contains records with data (rows).
In this tutorial, we will use the famous Northwind sample database (including MSAccess and MSSQLServer).
The following is the data selected from the "Customers" table:
CustomerID |
CustomerName |
ContactName |
Address |
| City
PostalCode |
Country |
1
|
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. De la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4
|
Around the Horn |
Thomas Hardy |
Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbk?p |
Christina Berglund |
Berguvsv?gen 8 |
Lule? |
S-958 22 |
Sweden |
The table above contains five records (one for each customer) and seven columns (CustomerID, CustomerName, ContactName, Address, City, PostalCode, and country).
SQL statements
Most of the operations you need to perform on the database are done using SQL statements.
The following SQL statement selects all records in the "Customers" table:
Instance select * from Customers;
In this tutorial, we'll explain a variety of different SQL statements to you.
Please remember ...
- SQL is not case sensitive: Select is the same as select.
- In this tutorial, we will write all the SQL keywords in uppercase.
Semicolon after the SQL statement?
- Some database systems need to use semicolons at the end of each SQL statement.
- Semicolons are a standard way to separate each SQL statement in a database system so that you can execute multiple SQL statements in the same request to the server.
- In this tutorial, we will use semicolons at the end of each SQL statement.
Some of the most important SQL commands
- SELECT -Extract data from the database
- Update-Updates the data in the database
- Delete-deletes data from the database
- INSERT INTO-inserts new data into the database
- CREATE database-creating new databases
- alter database-Modify Databases
- CREATE TABLE-Creates a new table
- ALTER TABLE -Change (change) database table
- Drop Table-delete tables
- CREATE index-Creating indexes (search key)
- Drop Index-delete indexes
SELECT statement
Syntactic:
SELECT column_name(s) FROM table_name
SELECT statement and WHERE clause
Syntactic:
SELECT [*] FROM [TableName] WHERE [condition1]
Select statement with where and/or clauses
Syntactic:
SELECT [*] FROM [TableName] WHERE [condition1] [AND [OR]] [condition2]...
SELECT statement and ORDER BY
Syntactic:
SELECT column_name()FROM table_nameORDER BY column_name() ASC or DESC
INSERT into statement
Syntactic:
INSERT INTO table_name (column, column1, column2, column3, ...)VALUES (value, value1, value2, value3 ...)
UPDATE statement
Syntactic:
UPDATE table_nameSET column=value, column1=value1,...WHERE someColumn=someValue
Delete statement
Syntactic:
DELETE FROM tableNameWHERE someColumn = someValue
2. SQL syntax