Introduction To: What Is SQL?
Introduction To: What Is SQL?
What is SQL?
• SQL stands for Structured Query 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!
• SQL
• HTML / CSS
RDBMS
RDBMS stands for Relational Database Management System.
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.
SQL Syntax
« Previous Next Chapter »
Database Tables
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:
SELECT * FROM Persons
In this tutorial we will teach you all about the different SQL
statements.
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:
This chapter will explain the SELECT and the SELECT * statements.
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
Tip: The asterisk (*) is a quick way of selecting all columns!
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.
The DISTINCT keyword can be used to return only distinct (different) values.
This is wrong:
This is wrong:
The AND & OR operators are used to filter records based on more than one condition.
The OR operator displays a record if either the first condition or the second condition is true.
Now we want to select only the persons with the last name equal to "Svendson" AND the first name
equal to "Tove" OR to "Ola":
If you want to sort the records in a descending order, you can use the DESC keyword.
The first form doesn't specify the column names where the data will be
inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be
inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
The following SQL statement will add a new row, but only add data in
the "P_Id", "LastName" and the "FirstName" columns:
INSERT INTO Persons (P_Id, LastName, FirstName)
VALUES (5, 'Tjessem', 'Jakob')
or
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
The COUNT() function returns the number of rows that matches a specified criteria.
SQL Dates
The most difficult part when working with dates is to be sure that the format of the date you are
trying to insert, matches the format of the date column in the database.
As long as your data contains only the date portion, your queries will work as expected. However, if
a time portion is involved, it gets complicated.
Before talking about the complications of querying for dates, we will look at the most important
built-in functions for working with dates.
SQL Joins
« Previous Next Chapter »
SQL joins are used to query data from two or more tables, based on a relationship
between certain columns in these tables.
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a
relationship between certain columns in these tables.
A primary key is a column (or a combination of columns) with a unique value for each row. Each
primary key value must be unique within the table. The purpose is to bind data together, across
tables, without repeating all of the data in every table.
Notice that the relationship between the two tables above is the "P_Id" column.
• 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
Each table should have a primary key, and each table can have only one primary key.
MySQL:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on
multiple columns, use the following SQL syntax:
MySQL:
ALTER TABLE Persons
DROP PRIMARY KEY
SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
Let's illustrate the foreign key with an example. Look at the following two tables:
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy link between tables.
The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key column,
because it has to be one of the values contained in the table it points to.
MySQL:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
SQL Views
« Previous Next Chapter »
A view contains rows and columns, just like a real table. The fields in a view are fields from one or
more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the
data were coming from one single table.
The view "Current Product List" lists all active products (products that are not discontinued) from
the "Products" table. The view is created with the following SQL:
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
We can query the view above as follows:
SELECT * FROM [Current Product List]
Another view in the Northwind sample database selects every product in the "Products" table with a
unit price higher than the average unit price:
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)
We can query the view above as follows:
SELECT * FROM [Products Above Average Price]
Another view in the Northwind database calculates the total sale for each category in 1997. Note
that this view selects its data from another view called "Product Sales for 1997":
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName
We can query the view above as follows:
SELECT * FROM [Category Sales For 1997]
We can also add a condition to the query. Now we want to see the total sale only for the category
"Beverages":
SELECT * FROM [Category Sales For 1997]
WHERE CategoryName='Beverages'