Download presentation - University of Reading

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Database Design
SQL (1)
John Wordsworth
Department of Computer Science
The University of Reading
[email protected]
Room 129, Ext 6544
April 2002
Information Systems Design
John Ogden & John Wordsworth
1
Lecture objectives
Outline the history of SQL.
Distinguish data definition, data manipulation, and data control
languages.
Use SQL to perform projections and selections on tables.
Use the COUNT, GROUP, and ORDER BY options of SQL
SELECT.
Use SQL for Cartesian product and equijoin.
List the column aggregate functions and their uses.
April 2002
Information Systems Design
John Ogden & John Wordsworth
2
Overview of SQL
Language invented in IBM in the 1970s to support relational
databases, and now an international standard.
Three aspects:
A Data Definition Language
A Data Manipulation Language
SELECT
UPDATE
INSERT
DELETE
A Data Control Language
April 2002
Information Systems Design
John Ogden & John Wordsworth
3
Tables used throughout the SQL slides (1)
Book
ID
31753
63181
Title
Fundamentals of
Database Systems
Modern Database
Management
Modern Database
Management
Conceptual Database
Design
Software Inspection
Author
Elmasri
Navathe
MCFadden
Hoffer
MCFadden
Hoffer
Batini Ceri
Navathe
Gilb Graham
56529
Software Engineering
Sommerville
55805
Computer Related
Risks
The Trouble With
Computers
36047
36048
30244
12186
April 2002
Subject
Databases
Bind
H
Databases
H
Databases
P
Databases
P
P
Neumann
Software
Engineering
Software
Engineering
Ethics
Landauer
Ethics
P
Information Systems Design
John Ogden & John Wordsworth
H
H
4
Tables used throughout the SQL slides (2)
Qty
Publisher
ID
31753
36047
36048
30244
63181
56529
55805
12186
April 2002
Pub_Name
Addison Wesley
Benjamin Cummings
Benjamin Cummings
Benjamin Cummings
Addison Wesley
Addison Wesley
Addison Wesley
MIT Press
Information Systems Design
John Ogden & John Wordsworth
ID
31753
36047
36048
30244
63181
56529
55805
12186
No
5
3
4
1
1
2
0
1
5
SQL : DDL
Most important commands:
• CREATE TABLE
•
•
•
•
CREATE INDEX
ALTER TABLE
DROP TABLE
DROP INDEX
CREATE TABLE Book (
ID
INTEGER
Title
CHARACTER (35)
Author
CHARACTER (25)
Subject
CHARACTER (25)
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL ) ;
ALTER TABLE Book
ADD Bind CHARACTER (1) NOT NULL;
April 2002
Information Systems Design
John Ogden & John Wordsworth
6
SELECT syntax
SELECT [ DISTINCT | ALL ]
{ * | column_expression [ AS newname ] [ , ... ] }
FROM table_name [ alias ] [ , ... ]
[ WHERE condition ]
[ GROUP BY column_list ]
[ HAVING condition ]
[ ORDER BY column_list ]
;
April 2002
Information Systems Design
John Ogden & John Wordsworth
7
Projection
SELECT * FROM Book ;
denotes the whole table Book.
SELECT DISTINCT Title FROM Book ;
denotes the following table:
Title
Fundamentals of Database Systems
Modern Database Management
Conceptual Database Design
Software Inspection
Software Engineering
Computer Related Risks
The Trouble With Computers
April 2002
Information Systems Design
John Ogden & John Wordsworth
8
Selection
SELECT DISTINCT Title, Author
FROM Book
WHERE Subject = ‘Databases’;
Title
Fundamentals of Database
Systems
Modern Database
Management
Conceptual Database Design
April 2002
Author
Elmasri Navathe
MCFadden Hoffer
Batini Ceri Navathe
Information Systems Design
John Ogden & John Wordsworth
9
Using COUNT and GROUP BY
SELECT Subject, COUNT(*) AS SCount
FROM Book GROUP BY Subject ;
April 2002
Subject
Databases
Software Engineering
SCount
4
2
Ethics
2
Information Systems Design
John Ogden & John Wordsworth
10
Using ORDER BY
SELECT ID, Title, Author, Bind FROM Book
WHERE Subject = ‘Databases’
ORDER BY ID ;
ID
30244
31753
36047
36048
April 2002
Title
Conceptual Database
Design
Fundamentals of
Database Systems
Modern Database
Management
Modern Database
Management
Author
Batini Ceri
Navathe
Elmasri
Navathe
MCFadden
Hoffer
MCFadden
Hoffer
Information Systems Design
John Ogden & John Wordsworth
Bind
P
H
H
P
11
Cartesian product
SELECT Book.*, Qty.*
FROM Book, Qty ;
denotes the Cartesian product of Book and Qty, 7
columns and 64 rows.
April 2002
Information Systems Design
John Ogden & John Wordsworth
12
Equijoin with projection
SELECT Title, Bind, No
FROM Book, Qty
WHERE Book.ID = Qty.ID ;
Title
Fundamentals of Database Systems
Modern Database Management
Modern Database Management
Conceptual Database Design
Software Inspection
Software Engineering
Computer Related Risks
The Trouble With Computers
April 2002
Information Systems Design
John Ogden & John Wordsworth
Bind
H
H
P
P
P
H
H
P
No
5
3
4
1
1
2
0
1
13
Equijoin with selection
SELECT ID, Title, Author
FROM Book, Publisher
WHERE Book.ID = Publisher.ID
AND Publisher.Pub_Name = ‘Addison Wesley’
ORDER BY Author;
ID
31753
63181
55805
56529
April 2002
Title
Fundamentals of Database Systems
Software Inspection
Computer Related Risks
Software Engineering
Information Systems Design
John Ogden & John Wordsworth
Author
Elmasri Navathe
Gilb Graham
Neumann
Sommerville
14
Equijoin with other conditions
SELECT Pub_Name, SUM(Qty.No)
FROM Publisher, Qty
WHERE Publisher.ID = Qty.ID
GROUP BY Pub_Name
HAVING SUM (Qty.No) > 1
Pub_Name
Addison Wesley
Benjamin Cummings
April 2002
Information Systems Design
John Ogden & John Wordsworth
SUM(Qty.No)
8
8
15
Column aggregate functions
COUNT: the number of elements in each group
SUM: the sum of the numeric values of an attribute in a group
AVG: the average of the numeric valued of an attribute in a group
MAX: the greatest numeric value of an attribute in a group
MIN: the least numeric value of an attribute in a group
April 2002
Information Systems Design
John Ogden & John Wordsworth
16
Key points
SQL has been developed over many years, and is now an
international standard.
SQL provides commands for defining databases and tables, for
making queries and updates, and for creating views and setting
security options.
The SELECT statement is used for projection, selection,
Cartesian product, and equijoin.
The column aggregate functions are COUNT, SUM, AVG, MIN,
MAX; they need a GROUP BY clause to fix their scope.
April 2002
Information Systems Design
John Ogden & John Wordsworth
17
Related documents