SQL Is A Standard - BUT...
SQL Is A Standard - BUT...
Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE,
INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!
To build a web site that shows some data from a database, you will need the following:
SQL
HTML / CSS
RDBMS
RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
A table is a collections of related data entries and it consists of columns and rows.
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records
(rows) with data.
The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName, Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
In this tutorial we will teach you all about the different SQL statements.
Some database systems require a semicolon at the end of each SQL statement.
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed
in the same call to the server.
We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement, but some database
programs force you to use it.
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys), specify links between tables, and
impose constraints between tables. The most important DDL statements in SQL are:
SELECT column_name(s)
FROM table_name
and
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
Navigation in a Result-set
Most database software systems allow navigation in the result-set with programming functions, like: Move-To-First-Record, Get-Record-
Content, Move-To-Next-Record, etc.
Programming functions like these are not a part of this tutorial. To learn about accessing data with function calls, please visit our ADO
tutorial or our PHP tutorial.
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at least one of the columns
The AND & OR operators are used to filter records based on more than one condition.
The AND & OR Operators
The AND operator displays a record if both the first condition and the second condition is true.
The OR operator displays a record if either the first condition or the second condition is true.
If you want to sort the records in a descending order, you can use the DESC keyword.
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
ORDER BY Example
Now we want to select all the persons from the table above, however, we want to sort the persons by their last name.
Now we want to select all the persons from the table above, however, we want to sort the persons descending by their last name.
The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on
performance.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
Now we want to select the persons living in a city that starts with "s" from the table above.
The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
The IN Operator
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
SQL Alias
You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table
names or column names.
SELECT column_name(s)
FROM table_name
AS alias_name
Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data
types. Also, the columns in each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.
PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.
The SELECT INTO statement selects data from one table and inserts it into a different table.
The SELECT INTO statement is most often used to create backup copies of tables.
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_tablename
Or we can select only the columns we want into the new table:
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER
TABLE statement).
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record
without adding a value to this field.
The following SQL enforces the "P_Id" column and the "LastName" column to not accept NULL values: