+ All Categories
Home > Documents > Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

Date post: 27-Mar-2015
Category:
Upload: lucas-west
View: 221 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005
Transcript
Page 1: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

Structured Query Language(SQL)

DB Chapter 3

J.G. ZhengJune 27th 2005

Page 2: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

2

Overview

SQL Language (SQL 92) Create Table Select

All examples can be used with the “AmazonBook” database This database can be downloaded

from the “Course Material” section in WebCT

Page 3: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

3

Introduction/Review

What is SQL Review: Database and DBMS (chapter 1) SQL is a standard language accepted by

relational DBMS to perform database operations

Some facts about SQL SQL 92 is the most commonly supported version English-like (not programming) Case insensitive Venders have different implementations

Page 4: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

4

Concepts Review

Relational model concepts reviewRelation : TableRow : RecordColumn : Field (Attribute)

Page 5: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

5

Create Table Statement

CREATE TABLE TableName(

Column 1 Definition,Column 2 Definition,…,PRIMARY KEY (Column, [Column])

)

Page 6: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

6

Column Definition

Column Definition Format ColumnName DataType

Example BookTitle Text(200) NumberofPages Integer ListPrice Currency

Page 7: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

7

Common Data Types

Numeric INTEGER, SMALLINT DECIMAL(i,j), NUMBER(i,j)

Character/String CHAR(n), VARCHAR(n)

Date DATE, DATETIME

See textbook page 42-43 for data types implemented in Access

Page 8: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

8

Create Table Example

CREATE TABLE Books(

Title VARCHAR(100),Publisher VARCHAR(50),ISBN CHAR(13),Price DECIMAL(6,2),Year INTEGER,PRIMARY KEY (ISBN)

)

Page 9: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

9

SELECT Statement

SELECT statement retrieves data from database (query) The result is usually another table

We will learn Selection and projection Sorting Calculation

Page 10: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

10

SELECT Statement Syntax

SELECT Column(s) or other expressionsFROM Table(s)[WHERE …][ORDER BY ColumnName]

Page 11: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

11

Simple SELECT Statement

Syntax SELECT * (or a list of columns)

FROM TableName Wild card: *

Example SELECT * FROM Books SELECT BookTitle, ListPrice FROM

Books

Page 12: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

12

Use WHERE Clause

Use WHERE clause to specify selection criteria

Example SELECT * FROM Books

WHERE ListPrice = 29.99 SELECT BookTitle, ListPrice FROM Books

WHERE ListPrice < 20

Comparison Operators “=“, “>”, “<“, “>=“, “<=“, “<>”

Page 13: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

13

More Comparison Operators

IN (value list) SELECT * FROM Books

WHERE ListPrice IN (19.99, 29.99, 39.99)

BETWEEN min AND max SELECT * FROM Books

WHERE ListPrice BETWEEN 9.99 AND 19.99

Page 14: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

14

String Pattern Match

Fuzzy query using LIKE _ (underscore): single character wildcard

? in Access % (percentage): multiple character wildcard

* in Access

Example SELECT * FROM Books

WHERE BookTitle LIKE '*information systems*'

Page 15: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

15

Compound Conditions

Use logical operators to connect multiple conditions

AND: an intersection of the data sets OR: a union of the data sets

Examples SELECT * FROM Books

WHERE ListPrice <= 19.99 AND ListPrice >= 9.99 SELECT * FROM Books

WHERE PubDate=#10/1/2003# OR PubDate=#10/1/2004#

SELECT * from Books WHERE Publisher = 'Que' AND Binding = 'Paperback'

Page 16: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

16

Sorting

Syntax ORDER BY Column(s) [ASC/DESC]

Examples SELECT * FROM Books

ORDER BY PubDate SELECT * FROM Books

ORDER BY Publisher DESC, PubDate

Page 17: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

17

Calculation

Calculating columns Calculated columns are not designed directly

into the table Using +, -, *, / with columns and numbers

Example SELECT BookTitle, ListPrice, ListPrice * 0.15

AS DiscountFROM BooksWHERE ListPrice * 0.15 > 20

Page 18: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

18

Built-in Functions

Using these functions to do statistics MIN MAX COUNT AVG SUM

Example SELECT COUNT(*) FROM Books SELECT AVG(ListPrice) FROM Books

WHERE Publisher = 'Prentice Hall'

Page 19: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

19

[Grouping]

GROUP BY: doing math with groups SELECT COUNT(*) FROM Books

WHERE Publisher = 'Prentice Hall';SELECT COUNT(*) FROM Books WHERE Publisher = 'The MIT Press';…

Or:SELECT Publisher, COUNT(*) FROM Books GROUP BY Publisher

Page 20: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

20

A Complete Query

SELECT ISBN, BookTitle, ListPrice, Publisher

FROM BooksWHERE

BookTitle like '*Information Systems*'AND PubDate > #1/1/2002#AND ListPrice < 100

ORDER BY ListPrice

Page 21: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

21

ExerciseUsing the “AmazonBook” database, use SQL or QBE to answer the following questions.

1. Which book is the most expensive? 2. How many books are under $100 of list price?3. I am looking for a “database” book, no more

than 50 dollars, and published after 1/1/2003; do you have any recommendations? I need the book title and its price.

4. What is the price per page for the books published by Que? List the book title, price, number of pages and price for page, and sort by the price per page, descending

Page 22: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

22

Updating Tables

INSERT INTO To add a new record

UPDATE To change data

DELETE FROM To delete records

Read this part yourself

Page 23: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

23

Summary

SQL is a standard Relational DBMS languageSQL statements CREATE TABLE SELECT

WHERE clause DELETE FROM INSERT INTO UPDATE

Page 24: Structured Query Language (SQL) DB Chapter 3 J.G. Zheng June 27 th 2005.

24

Good Resources

SQL Online Tutorial http://sqlcourse.com/ http://sqlcourse2.com/


Recommended