Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall"— Presentation transcript:

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

2 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

3 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

4 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

5 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

6 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

7 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

8 Starting a New Query Window
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. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

9 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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, StudentPhone FROM Student Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

10 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

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

12 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

13 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

14 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

15 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

16 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

17 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

18 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

19 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

20 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Updates Updates allow you to change existing records. The syntax is: UPDATE <TableName> SET <ColumnName> = <New Value>, <ColumNmae>=<new value> WHERE <ColumnName> = <criteria> Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

21 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

22 Notes on Deletes and Updates
Deletes and updates are dangerous. If you do not specify a criterion, 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

23 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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

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

25 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Sample Test Table Rule to Test Means of testing Expected Result Result 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

26 Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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 "Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall"

Similar presentations


Ads by Google