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

Slides:



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

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Basic SQL Introduction Presented by: Madhuri Bhogadi.
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Copyright © 2014 Pearson Education, Inc. Chapter 7 SQL Chapter7.1.
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. by Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson,
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
The Relational Model Chapter Two DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Getting Started with Microsoft SQL Server 2012 Express Edition Appendix A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Getting Started Chapter One DATABASE CONCEPTS, 7th Edition
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 10 Using Macros and SQL in Access 1.
Copyright ©2014 Pearson Education, Inc. Chapter 3 Requirements and Business Rules Chapter3.1.
Structured Query Language
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Getting Started with Oracle Database 11g Release 2 Express Edition Appendix B DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 1 Workshop 1 The Four Main Database Objects Series Editor Amy Kinser.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Access 2010 by Robert Grauer, Keith Mast, Mary Anne Poatsy Chapter.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 1 Workshop 2 Tables, Keys, and Relationships Series Editor Amy Kinser.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 2 Workshop 3 Queries and Data Access Series Editor Amy Kinser by.
CHAPTER 6: INTRODUCTION TO SQL Copyright © 2014 Pearson Education, Inc. 1 Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 6 Action and Specialized Queries 1.
© 2012 Pearson Education, Inc. publishing Prentice Hall. Note 9 The Product Life Cycle.
Chapter 3 Requirements and Business Rules Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter3.1.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
David M. Kroenke and David J
Structured Query Language (SQL) William Klingelsmith
PT2520 Unit 9: Database Security II
Database Processing: David M. Kroenke’s Chapter Seven:
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
SQL Queries Chapter No 3.
PT2520 Unit 5: Physical Design
Introduction To Structured Query Language (SQL)
Copyright © 2018, 2015, 20 Pearson Education, Inc. All Rights Reserved Database Concepts Eighth Edition Chapter # 2 The Relational Model.
PT2520 Unit 8: Database Security I
Getting Started Chapter One DATABASE CONCEPTS, 5th Edition
Getting Started Chapter One DATABASE CONCEPTS, 4th Edition
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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