Database and Web Database Systems: SQL: Data Manipulation
Database and Web Database Systems: SQL: Data Manipulation
Systems
CT014-3-2
Learning Outcomes
At the end of this lesson, YOU should be
be able to:
Explain the purpose and importance of SQL.
Write SQL statements using SELECT to retrieve data
from database.
Write SQL statements using compound WHERE
conditions.
Write SQL statements using ORDER BY to sort query
results.
Write SQL statements using Aggregate functions.
Keywords
If you have mastered this topic, you should be able to use the
following terms correctly in your assignments and exams:
Keywords
If you have mastered this topic, you should be able
to use the following terms correctly in your
assignments and exams:
Objectives of SQL
Ideally, database language should allow user to:
create the database and relation structures;
perform insertion, modification, deletion of data from
relations;
perform simple and complex queries.
Objectives of SQL
SQL is a transform-oriented language with 2
major components:
A DDL for defining database structure.
A DML for retrieving and updating data.
Objectives of SQL
SQL is relatively easy to learn:
it is non-procedural - you specify what information you
require, rather than how to get it;
it is essentially free-format.
Objectives of SQL
Consists of standard English words:
1) CREATE TABLE Staff(staffNo VARCHAR(5),
lName VARCHAR(15),
salary DECIMAL(7,2));
Objectives of SQL
Can be used by range of users including DBAs,
management, application developers, and other types of
end users.
An ISO standard now exists for SQL, making it both the
formal and de facto standard language for relational
databases.
History of SQL
In 1974, D. Chamberlin (IBM San Jose Laboratory)
defined language called Structured English Query
Language (SEQUEL).
A revised version, SEQUEL/2, was defined in 1976 but
name was subsequently changed to SQL for legal
reasons.
History of SQL
Still pronounced see-quel, though official pronunciation
is S-Q-L.
IBM subsequently produced a prototype DBMS called
System R, based on SEQUEL/2.
Roots of SQL, however, are in SQUARE (Specifying
Queries as Relational Expressions), which predates
System R project.
History of SQL
In late 70s, ORACLE appeared and was probably first
commercial RDBMS based on SQL.
In 1987, ANSI and ISO published an initial standard for
SQL.
In 1989, ISO published an addendum that defined an
Integrity Enhancement Feature.
In 1992, first major revision to ISO standard occurred,
referred to as SQL2 or SQL/92.
In 1999, SQL3 was released with support for objectoriented data management.
Importance of SQL
SQL has become part of application architectures such
as IBMs Systems Application Architecture.
It is strategic choice of many large and influential
organizations (e.g. X/OPEN).
SQL is Federal Information Processing Standard (FIPS)
to which conformance is required for all sales of
databases to American Government.
Importance of SQL
SQL is used in other standards and even
influences development of other standards as a
definitional tool. Examples include:
ISOs Information Resource Directory System (IRDS)
Standard
Remote Data Access (RDA) Standard.
Literals
Literals are constants used in SQL statements.
All non-numeric literals must be enclosed in single
quotes (e.g. London).
All numeric literals must not be enclosed in quotes (e.g.
650.00).
SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM
TableName [alias] [, ...]
[WHERE condition]
[GROUP BY
columnList] [HAVING
condition]
[ORDER BY
columnList]
SELECT Statement
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
SELECT Statement
Order of the clauses cannot be changed.
Only SELECT and FROM are mandatory.
Example: Use of
COUNT(DISTINCT)
How many different properties viewed in
May 01?
SELECT COUNT(DISTINCT propertyNo) AS
count
FROM Viewing
WHERE viewDate BETWEEN 1-May-01AND
31-May-01;
and
column names
aggregate functions
constants
expression involving combinations of the above.
SUM(salary) AS sum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
Subqueries
Some SQL statements can have
SELECT embedded within them.
Subquery Rules
ORDER BY clause may not be used in a subquery
(although it may be used in outermost SELECT).
Subquery SELECT list must consist of a single
column name or expression, except for subqueries
that use EXISTS.
By default, column names refer to table name in
FROM clause of subquery. Can refer to a table in
FROM using an alias.
CT014-3-2 Database and Web Database Systems
Subquery Rules
When subquery is an operand in a
comparison, subquery must appear on
right-hand side.
A subquery may not be used as an
operand in an expression.
Multi-Table Queries
Can use subqueries provided result columns come from
same table.
If result columns come from more than one table must
use a join.
To perform join, include more than one table in FROM
clause.
Use comma as separator and typically include WHERE
clause to specify join column(s).
CT014-3-2 Database and Web Database Systems
Multi-Table Queries
Also possible to use an alias for a table
named in FROM clause.
Alias is separated from table name with a
space.
Alias can be used to qualify column
names when there is ambiguity
CT014-3-2 Database and Web Database Systems
Equivalent
algebra.
to
equi-join
in
relational
Computing a Join
Procedure for generating results of a join are:
1. Form Cartesian product of the tables named in
FROM clause.
2. If there is a WHERE clause, apply the search
condition to each row of the product table, retaining
those rows that satisfy the condition.
3. For each remaining row, determine value of each
item in SELECT list to produce a single row in result
table.
CT014-3-2 Database and Web Database Systems
Computing a Join
4. If DISTINCT has been specified, eliminate
any duplicate rows from the result table.
INSERT
dataValueList must match columnList as
follows:
number of items in each list must be same;
must be direct correspondence in position of
items in two lists;
data type of each item in dataValueList must
be compatible with data type of corresponding
column.
Or
INSERT INTO Staff
VALUES (SG44, Anne, Jones, Assistant, NULL,
NULL, 8100, B003);
CT014-3-2 Database and Web Database Systems
INSERT SELECT
Second form of INSERT allows multiple
rows to be copied from one or more tables
to another:
INSERT INTO TableName [ (columnList) ]
SELECT ...
UPDATE
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
UPDATE
WHERE clause is optional:
if omitted, named columns are updated for all
rows in table;
if specified, only those rows that satisfy
searchCondition are updated.
DELETE
DELETE FROM TableName
[WHERE searchCondition]
Summary
Q&A
CT014-3-2 Database and Web Database Systems
Next Session
Database Security