Top Banner
IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.1
26

IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Mar 27, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

IT203 Unit 7: SQL Queries IIIT203 Unit 7: SQL Queries II

SQL

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.1

Page 2: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

SQL Overview

• SQL is the language of relational databases.

• It is used for every aspect of database development and management.

• Anyone who works with relational databases is expected to have knowledge of SQL.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapte7.2

Page 3: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

History• SQL is the programming language used for

accessing and manipulating data and objects in relational databases.

• The first versions of SQL were developed by IBM in the 1970s.

• SQL first became an ANSI standard in 1986 and an ISO standard in 1987.

• There was a major revision to the standard in 1992.

• Additional modifications were made in 1999, 2003, and 2006.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.3

Page 4: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Nature of SQL

• SQL is a declarative language.• Procedural languages like C# or Java

describe how to accomplish a task step by step.

• In a declarative language you say what you want to do, not how.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.4

Page 5: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

SQL Functionality

• SQL is not case sensitive.• In some environments, SQL statements

must end with a semicolon.• SQL is usually divided into two broad

areas of functionality:– DDL (Data Definition Language)– DML (Data Manipulation Language)

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.5

Page 6: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

DDL

• Data Definition language is the set of SQL keywords and commands used to create, alter and remove database objects.

• A example is the CREATE TABLE command:CREATE TABLE TestTable(

TestID INT IDENTITY (1,1),TestDescription NVARCHAR(255)

)

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.6

Page 7: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

DML

• Data Manipulation Language is the set of keywords and commands used to retrieve and modify data.

• SELECT, UPDATE, INSERT, and DELETE are the primary actions of DML.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.7

Page 8: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Starting a New Query Window

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.8

One way to start a new query window in SQL Server is to right-click the database folder in the object explorer window and select New Query from the context menu. It will open up a new query window.

Page 9: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Select Statement

• The SELECT statement is used to retrieve data from the database.

• The basic syntax is:SELECT <columnName>, <columnName>FROM <TableName>

SELECT StudentFirstName, StudentLastName, StudentPhoneFROM Student

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.9

Page 10: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Group By

• When a SELECT clause includes an aggregate function and columns that are not a part of that function, you must use the GROUP BY keywords to group by each the non-included columns.

• This is necessary because you are mixing functions that operate on multiple rows with columns that refer to values in individual rows only.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.10

Page 11: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Group By Example

SELECT TutorKey, COUNT(SessionTimeKey) AS [Total Sessions]FROM SessionGROUP BY TutorKey

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.11

Page 12: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Having

• The HAVING keyword is used when there is an aggregate function in the criteria of a query.SELECT TutorKey, COUNT(SessionTimeKey) AS [Total Sessions]FROM SessionGROUP BY TutorKeyHAVING COUNT(SessionTimeKey)<4

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.12

Page 13: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Joins

• In database design and normalization, the data are broken into several discrete tables.

• Joins are the mechanism for recombining the data into one result set.

• We will look at three kinds of joins:– Inner joins– Equi joins– Outer joins

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.13

Page 14: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Basic Inner Join Syntax

SELECT <column1, column2>FROM <table1>INNER JOIN <table2> ON <table1>.<column>=<table2>.<column

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter_.14

Page 15: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Inner Joins• Inner joins return related records from each

of the tables joined.SELECT TutorLastName,TutorFirstName,SessionDateKey,SessionTimeKey,StudentKeySessionStatusFROM TutorINNER JOIN SessionON Tutor.TutorKey = Session.TutorKey

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.15

Page 16: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Equi Joins• Equi joins present an alternative way to perform

inner joins. Some older RDMSs only support this alternative form. The example below also uses an alias for the table name.

SELECT t.TutorKey,TutorLastName,TutorFirstName,SessionDateKey,SessionTimeKey,StudentKeyFROM Tutor t,Session sWHERE t.TutorKey = s.TutorKeyAND TutorLastName = 'Brown'

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.16

Page 17: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Outer Join Syntax

• Outer joins return records that are not matched. The following query returns tutors that have no sessions scheduled:

SELECT <column1>, <column2>FROM <table1>LEFT OUTER JOIN <table2>

ON <table1>.<column>=<table2>.<column>

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter_.17

Page 18: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Outer Join Example

SELECT t.TutorKey,TutorLastName,SessionDateKeyFROM Tutor tLEFT OUTER JOIN Session sON t.TutorKey = s.TutorKeyWHERE SessionDateKey IS Null

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.18

Page 19: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Inserts

• To insert a record into a table, you use the following syntax:

INSERT INTO <tablename>(<ColumnName>, <columnName>, ...)VALUES(<value1>, <value2>, ...)

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.19

Page 20: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Updates

• Updates allow you to change existing records. The syntax is:

UPDATE <TableName>SET <ColumnName> = <New Value>,<ColumName>=<new value>WHERE <ColumnName> = <criteria>

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.20

Page 21: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Deletes

• Deletes allow you to remove a record from a table:

DELETE FROM <TableName>WHERE <columnName> = <criteria>

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.21

Page 22: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Notes on Deletes and Updates

• Deletes and updates are dangerous. If you do not specify criteria, the update or delete will be applied to all the rows in a table.

• Also, referential integrity may prevent a deletion. You cannot delete a parent that has children in another table.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.22

Page 23: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Creating a Trigger

• Triggers are programs that are triggered by an event, typically INSERT, UPDATE, or DELETE.

• They can be used to enforce business rules that referential integrity and constraints alone cannot enforce.

• The basic syntax for creating a trigger is:CREATE TRIGGER <trigger_name> ON <table_name>[FOR, AFTER, INSTEAD OF] [INSERT, UPDATE, DELETE]AS{SQL Code}

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.23

Page 24: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Documentation: Testing Plans

• When testing the database, you should document all your SQL queries and their results.

• On the next slide is a sample of a test table, showing the test and results.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.24

Page 25: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

Sample Test TableRule to Test Means of testing Expected Result Result

Return all students by Gender

SELECT StudentLastName, StudentFirstName, StudentGenderFROM StudentWHERE StudentGender='M'

Return all male students

Returned all male students

Return unduplicated count of students from tutoring sessions

SELECT Count(StudentID) FROM Session SELECT Count(DISTINCT StudentID) FROMSession

Return unduplicated students from session

Returns duplicated students Returns unduplicated student Count

Return hours for student per month

SELECT Tutorkey, MONTH(SessionDateKey) AS [Month], YEAR(SessionDateKey) AS [Year], ((COUNT (SessionTimeKey))* 30.0)/60.0 AS [Hours]FROM SessionGROUP BY TutorKey, MONTH(SessionDateKey), YEAR(SessionDateKey)ORDER BY YEAR(SessionDateKey),MONTH(SessionDateKey)

Hours grouped by student and month

Returns hours grouped by student and month

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.25

Page 26: IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic,

mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall