44
Introduction to SQL Introduction to SQL

Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Embed Size (px)

Citation preview

Page 1: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Introduction to SQLIntroduction to SQL

Page 2: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SQLSQL

What is SQLSQL ComponentsSyntax & ConventionsSQL Data TypesINNER JOINSELECT Statements

Page 3: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

What Is SQL?What Is SQL?

SQL (Structured Query Language)– Is a standard language to create, modify, manipulate, and

query relational database SQL

– is traditionally a nonprocedural Language. – A procedural language, like FORTRAN or C, describes

instructions to the computer by HOW to proceed.– A nonprocedural language describes WHAT to produce.

SQL3 (1999) – contains procedural features—BEGIN-END block, IF

statement, functions. SQL is an open standard—not owned by a company

Page 4: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Brief History of SQLBrief History of SQL Developed by IBM in the 70’s, along with Relational DB Officially released in 1981 as SQL SQL-86

– ANSI (American National Standards Institute) standard– ISO (International Standards Organization) standard

SQL-89 (SQL1) SQL-92 (SQL2) SQL-99 (SQL3) New Standards

– Support for Internet– Support for XML– Support for Java– Support for OOP

Page 5: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Why Use SQL?Why Use SQL?

SQL can do things Access Design Window cannot– E.g., subquery

SQL can be used from other applications– E.g., from MS Excel, VB– Web applicaitions

SQL is independent of MS Access

Page 6: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SQL ComponentsSQL Components

DDL (Data Definition Language)– e.g., CREATE TABLE, DROP TABLE

DML (Data Manipulation Language)– e.g., SELECT field FROM table

DCL (Data Control Language)– e.g., for internal security

Page 7: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

DDLDDL

CREATE TABLEALTER TABLEDROP TABLECREATE INDEX

Page 8: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

DMLDMLSELECTUNIONUPDATE DELETE -- delete a rowINSERT INTO -- insert a rowSELECT INTO -- select fields into newTablePARAMETER -- allows user to enter

information interactively

Page 9: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SELECT StatementSELECT Statementwith one tablewith one table

SELECT title, price

FROM Books

Given: Books (bkID, title, price)

Page 10: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SELECT StatementSELECT Statementwith conditionwith condition

SELECT title, priceFROM BooksWHERE Books.price >= 25.0

Page 11: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Arranging in Ascending or Arranging in Ascending or Descending OrderDescending Order

SELECT title, priceFROM BooksORDER BY title

SELECT title, priceFROM BooksORDER BY price DESC

Page 12: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Ordering by More Than One Ordering by More Than One FieldField

SELECT *

FROM Books

ORDER BY price, title

Page 13: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Your TurnYour Turn

Given:Persons(lastName, firstName, address, city, ZIP, state)

Write an SQL statement for a view containing1. lastName, firstName, state of persons from the state

of “AZ”2. People (all fields) ordered by their last name3. lastName, firstName, state of persons arranged by

the state and then by last name.

Page 14: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SELECT Statement from two SELECT Statement from two tablestables

SELECT Books.title, Books.price, Publishers.pubName

FROM Publishers INNER JOIN Book

ON Publishers.pubID = Books.pubID;

Books

bkID title price pubID

Publishers

pubID pubName

Page 15: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Inner JoinInner Join

bkID title price pubID

1 b12 b13 2

2 B22 b23 2

3 b32 b33 1

4 b42 b43 1

pubID pubName

1 p11

2 p21

3 p23

4 p24

Books Publishers

bkID Title Price pubID pubID pubName

1 b12 b13 2 2 p21

2 b22 b23 2 2 p21

3 b32 b33 1 1 p11

4 b42 b43 1 1 p11

Books Inner Join PublishersOn Books.pubID = Publishers.pubID

Page 16: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SELECT Statement from Two SELECT Statement from Two TablesTables

SELECT Books.title, Books.price, Publishers.pubNameFROM Publishers INNER JOIN BookON Publishers.pubID = Books.pubIDWHERE Books.price > 25;

Books

bkID title price pubID

Publishers

pubID pubName

Page 17: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Your TurnYour Turn

Given:Books (ISBN, title, price, pubID)Publishers (putID, pubName, pubPhone)

Write an SQL statement to return1. ISBN, title, price, and pubName of all books

2. title, price, pubName of all books published by “Alpha House”

3. Title, price pubName of all books which cost $25 or over and published by “Alpha House”

Page 18: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Syntax and ConventionsSyntax and Conventions

Case insensitive, butUse UPPERCASE for keywords (convention)Statements can be broken up over multiple linesEach SQL statement ends with a semicolonCapitalize table names (our convention)For Access: BooksAuthors or [Books/Authors}

Page 19: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SQL Data TypesSQL Data Types

BOOLEAN, LOGICAL BYTE, INTEGER COUNTER,

AUTOINCREMENT CURRENCY, MONEY DATE, DATETIME SHORT, SMALLINT LONG, INTEGER SINGLE, REAL

Yes/No Number size = byte Autonumber, size = long

Currency Date/Time Number, size = Integer Number, size = Long Integer Number, size = single

SQL Data Type Access Field Type

Page 20: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

SQL Data Types (cont.)SQL Data Types (cont.)

DOUBLE, FLOAT, NUMBER

TEXT, CHAR, STRING LONGTEXT, MEMO

Number, size = Double

TEXT Memo

SQL Data Type Access Field Type

Page 21: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Joining TablesJoining Tables

Books bkID bkTitle pubID

Publishers pubID pubName pubPhone

Authors auID auName auPone

BooksAuthors bkID auID

Page 22: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Inner JoinInner Join

Books INNER JOIN PublishersON Books.pubID = Publishers.pubID

SELECT title, pubNameFROM Books INNER JOIN PublishersON Books.pubID = Publishers.pubID

Page 23: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Nested JoinsNested Joins

SELECT Books.title, Authors.auNameFROM Books INNER JOIN (Authors INNER JOIN BooksAuthors ON Authors.auID = BooksAuthors.auID)

ON Books.ISBN = BooksAuthors.ISBN;

Display Book titles and their Authors.

Books

ISBN title price pubID

BooksAuthors

bkID auID

Authors

auID auName

Page 24: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Relating Many TablesRelating Many Tables

Books

bkID title price pubID

BooksAuthors

ISBN auID

Authors

auID auName

Publishers

pubID pubName

• Display book titles, their authors, and their publishers.

Page 25: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Relating Many TablesRelating Many TablesBooks

bkID title price pubID

BooksAuthors

bkID auID

Authors

auID auName

Publishers

pubID pubName

SELECT Books.title, Authors.auName, Publishers.pubNameFROM Authors INNER JOIN (BooksAuthors INNER JOIN (Books INNER JOIN Publishers ON Books.pubID = Publishers.pubID ) ON BooksAuthors.bkID = Books.bkID )ON Authors.auID = BooksAuthors.auID

Page 26: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Practice with AccessPractice with Access

Write SQL statements to display the following. Then create Access queries to answer the same questions and check their SQL statements.

Display the authors’ names and their phone numbers Display the phone number of author named “Snoopy” Display titles and price of all books which cost $300 or

more

(Download library2.mdb)

Page 27: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Practice with SQLPractice with SQL

Display titles and prices of books and their publishers’ names

Display the book titles and their authors Display the book tittles, their authors, and their

publishers Display titles and prices of books by publisher named

“Big House” Display all books by author named “Sleepy” Display books between $20 and $30 and their authors Display books that are less than $20 or more than $30

and their authors

Page 28: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

ON Clause Or WHERE ON Clause Or WHERE ClauseClause

ON clause is part of INNER JOIN, LEFT JOIN, & RIGHT JOIN

WHERE places conditions on data values to be displayed. (Think of WHERE as a filter to restrict rows--can provide additional restriction.)

Page 29: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

ON or WHERE ON or WHERE

SELECT Books.Title, Publishers.pubNameFROM Books INNER JOIN PublishersON Books.pubID = Publishers.pubID

is equivalent to

SELECT Books.Title, Publishers.pubNameFROM Books, PublishersWHERE Books.pubID = Publishers.pubID;

Page 30: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Can This Be Simplified? Can This Be Simplified?

• SELECT Books.title, Authors.auName, Publishers.pubNameFROM Authors INNER JOIN (BooksAuthors INNER JOIN (Books INNER JOIN Publishers ON Books.pubID = Publishers.pubID ) ON BooksAuthors.bkID = Books.bkID )ON Authors.auID = BooksAuthors.auID

Page 31: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Recall Recall

• if (cond1) { statement1}else { if (cont2) { statement2 } else { if (cond3) { statement3 } }}

if (cond1){ statement1}else if (cond2){ statement2}else if (cond3){ statement3}

Is equivalent

to

Page 32: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Analogously… Analogously… • SELECT Books.title, Authors.auName, Publishers.pubName

FROM Authors INNER JOIN (BooksAuthors INNER JOIN (Books INNER JOIN Publishers ON Books.pubID = Publishers.pubID ) ON BooksAuthors.bkID = Books.bkID )ON Authors.auID = BooksAuthors.auID

is equivalent to

SELECT Books.title, Authors.auName, Publishers.pubNameFROM Authors, BooksAuthors,Books, PublishersWHERE Authors.auID = BooksAuthor.auIDAND BooksAuthor.bkID = Books.bkIDAND Books.pubID = Publishers.pubID

Page 33: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

UPDATE StatementUPDATE Statement

First, make a copy of the Books table--so that you can preserve the original table.

Change the price of “Iliad” to $50.

UPDATE Books2 SET Books2.Price = 50WHERE BOOKS2.Title="iliad";

What will result from the following? UPDATE Books2 SET Books2.Price = 50;

Page 34: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

UPDATE Statement (cont.)UPDATE Statement (cont.)

Raise the price of all books from publisher “Big House” by 10 %– Note: subquery

SELECT pubIDFROM PublishersWHERE pubName = “Big House”

UPDATE BooksSET price = price * 1.1WHERE Books2.pubID = Subquery

Page 35: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Raise the price of all books from publisher “Big House” by 10 %

UPDATE Books2SET Books2.price = Books2.price * 1.1WHERE Books2.pubID = (SELECT Publishers.pubID FROM Publishers WEHRE pubName = “Big House”)

Page 36: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Alternately…

UPDATE Books2 INNER JOIN Publishers ON Books2.pubID = Publishers.pubIDSET Books2.price = price*1.1WHERE Publishers.pubName=”Big House";

Page 37: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

UPDATE with Values from UPDATE with Values from Another Table Another Table

Update Price column in Books2 table, with new prices from table NewPrices (ISBN, price).

UPDATE Books2 INNER JOIN NewPrices ON Books2.ISBN = NewPrices.ISBNSET Books2.price = NewPrices.priceWHERE Books2.prices <> NewPrices.price

Page 38: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Previewing UpdatePreviewing Update

To check which records will be modified by the UPDATE statement…

SELECT Books.* FROM Books INNER JOIN NewPrices ON Books.ISBN = NewPrices.ISBNWHERE BOOKS.Price<>NewPrices.Price;

Page 39: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Your TurnYour Turn

1. Change Author Shakespeare’s telephone number to “999-8888”

2. Reduce the price of all books from publisher “Small House” by 5%

3. Raise the price of all books by Author Shakespeare by 5% (solution)

Page 40: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Raise by 5% the price of all books by Raise by 5% the price of all books by ShakespeareShakespeare

UPDATE Books2 SET bkPrice =1.05 * bkPriceWHERE Books2.bkID IN (SELECT BooksAuthors2.bkID FROM BooksAuthors2, Authors2 WHERE BooksAuthors2.auID = Authors2.auID AND Authors2.auName = "Shakespeare" ) Back

Page 41: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Difference betweenDifference between“=” and “IN”“=” and “IN”

UPDATE Books2 SET bkPrice =1.05 * bkPriceWHERE Books2.bkID = (SELECT BooksAuthors2.bkID . . . ) UPDATE Books2

SET bkPrice =1.05 * bkPriceWHERE Books2.bkID IN (SELECT BooksAuthors2.bkID FROM BooksAuthors2, Authors2 WHERE BooksAuthors2.auID IN Authors2.auID AND Authors2.auName = "Shakespeare"

)

OK if subquery returns only one

value

Necessary when subquery can

return more than one value

Page 42: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

INSERT INTO Statement INSERT INTO Statement

INSERT INTO Books2VALUES (“1-1111-1111-1”, “SQL Is Fun”, 1, 25.00)

INSERT INTO Books2 (ISBN, Title)VALUES (“2-2222-2222-2”, “Born to Code”)

Page 43: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

INSERT INTO Statement (cont.)INSERT INTO Statement (cont.)

Given:Publishers2 (pubID, pubName, pubPhone)where pubID is autonumber type

Insert a new publisher

INSERT INTO Publisher2(pubName, pubPhone)VALUES (“Aloha Press”, “808-738-2222”)

Page 44: Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements

Your TurnYour Turn Add the following record to Books2

• ISBN: 1-2345-6789-0• Title: SQL for Dummies• Publisher ID: 3• Price: $35.50

Add the following record to Authors2• auName: Brando• auPhone: 222-2222

Add the following record to Publihsers2• pubName: Aina Haina Associates• pubPhone: 555-5555