Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "IT203 Unit 7: SQL Queries II SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter7.1."— Presentation transcript:

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

2 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

3 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 There was a major revision to the standard in Additional modifications were made in 1999, 2003, and Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.3

4 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

5 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

6 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

7 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

8 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.

9 Select Statement The SELECT statement is used to retrieve data from the database. The basic syntax is: SELECT, FROM SELECT StudentFirstName, StudentLastName, StudentPhone FROM Student Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.9

10 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

11 Group By Example SELECT TutorKey, COUNT(SessionTimeKey) AS [Total Sessions] FROM Session GROUP BY TutorKey Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.11

12 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 Session GROUP BY TutorKey HAVING COUNT(SessionTimeKey)<4 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.12

13 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

14 Basic Inner Join Syntax SELECT FROM INNER JOIN ON. =.

15 Inner Joins Inner joins return related records from each of the tables joined. SELECT TutorLastName, TutorFirstName, SessionDateKey, SessionTimeKey, StudentKey SessionStatus FROM Tutor INNER JOIN Session ON Tutor.TutorKey = Session.TutorKey Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.15

16 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, StudentKey FROM Tutor t, Session s WHERE t.TutorKey = s.TutorKey AND TutorLastName = 'Brown' Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.16

17 Outer Join Syntax Outer joins return records that are not matched. The following query returns tutors that have no sessions scheduled: SELECT, FROM LEFT OUTER JOIN ON. =. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter_.17

18 Outer Join Example SELECT t.TutorKey, TutorLastName, SessionDateKey FROM Tutor t LEFT OUTER JOIN Session s ON t.TutorKey = s.TutorKey WHERE SessionDateKey IS Null Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.18

19 Inserts To insert a record into a table, you use the following syntax: INSERT INTO (,,...) VALUES(,,...) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.19

20 Updates Updates allow you to change existing records. The syntax is: UPDATE SET =, = WHERE = Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.20

21 Deletes Deletes allow you to remove a record from a table: DELETE FROM WHERE = Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.21

22 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

23 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 ON [FOR, AFTER, INSTEAD OF] [INSERT, UPDATE, DELETE] AS {SQL Code} Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Chapter7.23

24 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

25 Sample Test Table Rule to TestMeans of testingExpected ResultResult Return all students by Gender SELECT StudentLastName, StudentFirstName, StudentGender FROM Student WHERE 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) FROM Session 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 Session GROUP 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

26 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


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

Similar presentations


Ads by Google