COMP 101 Lecture Notes - Lecture 6: Aggregate Function, Relational Algebra, Operand

100 views18 pages
21 Jun 2018
School
Department
Course
Professor
C
h
a
p
t
e
r
6
SQL: Data Manipulation
Chapter 6 - Objectives
Purpose and importance of SQL.
How to retrieve data from database using SELECT and:
Use compound WHERE conditions.
Sort query results using ORDER BY.
Use aggregate functions.
Group data using GROUP BY and HAVING.
Use subqueries.
Chapter 6 - Objectives
Join tables together.
Perform set operations (UNION, INTERSECT, EXCEPT).
How to update database using INSERT, UPDATE, and DELETE.
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.
Must perform these tasks with minimal user effort and command structure/syntax must be easy to
learn.
It must be portable.
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.
Until SQL:1999, SQL did not contain flow of control commands. These had to be implemented using
a programming or job-control language, or interactively by the decisions of user.
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));
2) INSERT INTO Staff VALUES (‘SG16’, ‘Brown’, 8300);
3) SELECT staffNo, lName, salary
FROM Staff
WHERE salary > 10000;
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 18 pages and 3 million more documents.

Already have an account? Log in
O
b
j
t
i
v
s
o
f
S
Q
L
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, SQL:1999 was released with support for object-oriented data management.
In late 2003, SQL:2003 was released.
Importance of SQL
SQL has become part of application architectures such as IBM’s 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:
ISO’s Information Resource Directory System (IRDS) Standard
Remote Data Access (RDA) Standard.
Writing SQL Commands
SQL statement consists of reserved words and user-defined words.
Reserved words are a fixed part of SQL and must be spelt exactly as required and cannot be split
across lines.
User-defined words are made up by user and represent names of various database objects such as
relations, columns, views.
Writing SQL Commands
Most components of an SQL statement are case insensitive, except for literal character data.
More readable with indentation and lineation:
Each clause should begin on a new line.
Start of a clause should line up with start of other clauses.
If clause has several parts, should each appear on a separate line and be indented under start of
clause.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 18 pages and 3 million more documents.

Already have an account? Log in
W
i
t
i
n
g
S
Q
L
C
o
m
m
a
n
d
s
Use extended form of BNF notation:
- Upper-case letters represent reserved words.
- Lower-case letters represent user-defined words.
- | indicates a choice among alternatives.
- Curly braces indicate a required element.
- Square brackets indicate an optional element.
- … indicates optional repetition (0 or more).
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 BYcolumnList] [HAVING condition]
[ORDER BYcolumnList]
SELECT Statement
SELECT Specifies which columns are to
appear in output.
FROM Specifies table(s) to be used.
WHERE Filters rows.
GROUP BY Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
ORDER BY Specifies the order of the output.
SELECT Statement
Order of the clauses cannot be changed.
Only SELECT and FROM are mandatory.
Example 6.1 All Columns, All Rows
List full details of all staff.
SELECT staffNo, fName, lName, address,
position, sex, DOB, salary, branchNo
FROM Staff;
Can use * as an abbreviation for ‘all columns’:
SELECT *
FROM Staff;
Example 6.1 All Columns, All Rows
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 18 pages and 3 million more documents.

Already have an account? Log in

Document Summary

How to retrieve data from database using select and: Group data using group by and having. How to update database using insert, update, and delete. Ideally, database language should allow user to: Perform insertion, modification, deletion of data from relations; Must perform these tasks with minimal user effort and command structure/syntax must be easy to learn. Sql is a transform-oriented language with 2 major components: A dml for retrieving and updating data. Until sql:1999, sql did not contain flow of control commands. These had to be implemented using a programming or job-control language, or interactively by the decisions of user. It is non-procedural - you specify what information you require, rather than how to get it; Consists of standard english words: create table staff(staffno varchar(5), lname varchar(15), salary decimal(7,2)), insert into staff values ( sg16", brown", 8300), select staffno, lname, salary. Can be used by range of users including dbas, management, application developers, and other types of end users.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents