Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.

Slides:



Advertisements
Similar presentations
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Advertisements

Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Structured Query Language Part II Chapter Three CIS 218.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
Structured Query Language
Introduction to SQL J.-S. Chou Assistant Professor.
Concepts of Database Management, Fifth Edition
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Database Basics CPSC 4670/ Purpose of a Database The purpose of a database is to keep track of things Unlike a list or spreadsheet, a database.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL (DDL & DML Commands)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Structured Query Language
+ Midterm Review. + Notes from the Midterm When updating, deleting records Make sure you have a WHERE statement that only will give you the row(s) you.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Structured Query Language
SQL Query Getting to the data ……..
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Structured Query Language
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-2 Chapter Objectives Learn basic SQL statements for creating database structures Learn basic SQL SELECT statements and options for processing a single table Learn basic SQL SELECT statements for processing multiple tables with subqueries Learn basic SQL SELECT statements for processing multiple tables with joins Learn SQL statements to add, modify, and delete data

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-3 Structured Query Language –Acronym: SQL –Pronounced as “S-Q-L” –Also pronounced as “Sequel” –Originally developed by IBM as the SEQUEL language in the 1970s –SQL-92 is an ANSI national standard adopted in 1992

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-4 SQL Defined SQL is not a programming language, but rather a data sub-language SQL is comprised of: –A data definition language (DDL) Used to define database structures –A data manipulation language (DML) Data definition and updating Data retrieval (Queries) –There are other SQL functions not covered in this chapter Concurrency control [See Chapter 6] Transaction control [See Chapter 6]

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-5 SQL for Data Definition The SQL data definition statements include –CREATE To create database objects –ALTER To modify the structure and/or characteristics of database objects –DROP To delete database objects

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-6 SQL for Data Definition: CREATE Creating database tables –The SQL CREATE TABLE statement CREATE TABLE Employee( EmpID Integer Primary Key, EmpName Char(25) Not Null );

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-7 SQL for Data Definition: CREATE with CONSTRAINT Creating database tables with PRIMARY KEY constraints –The SQL CREATE TABLE statement –The SQL CONSTRAINT keyword CREATE TABLE Employee( EmpID Integer Not Null, EmpName Char(25) Not Null CONSTRAINT EmpPK PRIMARY KEY (EmpID) );

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-8 SQL for Data Definition: CREATE with CONSTRAINT Creating database tables with composite primary keys using PRIMARY KEY constraints –The SQL CREATE TABLE statement –The SQL CONSTRAINT keyword CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID) );

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-9 SQL for Data Definition: CREATE with CONSTRAINT Creating database tables using PRIMARY KEY and FOREIGN KEY constraints –The SQL CREATE TABLE statement –The SQL CONSTRAINT keyword CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID), CONSTRAINT EmpFKFOREIGN KEY EmpIDREFERENCES Employee (EmpID), CONSTRAINT SkillFKFOREIGN KEY SkillIDREFERENCES Skill (SkillID) );

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-10 SQL for Data Definition: CREATE with CONSTRAINT Creating database tables using PRIMARY KEY and FOREIGN KEY constraints –The SQL CREATE TABLE statement –The SQL CONSTRAINT keyword –ON UPDATE CASCADE and ON DELETE CASCADE CREATE TABLE Emp_Skill ( EmpID Integer Not Null, SkillID Integer Not Null, SkillLevel Integer, CONSTRAINT EmpSkillPK PRIMARY KEY (EmpID, SkillID), CONSTRAINT EmpFKFOREIGN KEY (EmpID) REFERENCES Employee (EmpID) ON DELETE CASCADE, CONSTRAINT SkillFKFOREIGN KEY (SkillID) REFERENCES Skill (SkillID) ON UPDATE CASCADE );

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-11 Deleting Database Objects: DROP To remove unwanted database objects from the database, use the SQL DROP statement Warning… The DROP statement will permanently remove the object and all data DROP TABLE Employee;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-12 Removing a Constraint: ALTER & DROP To change the constraints on existing tables, you may need to remove the existing constraints before adding new constraints ALTER TABLE Employee DROP CONSTRAINT EmpFK;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-13 SQL for Data Retrieval (Queries) SELECT is the best known SQL statement SELECT will retrieve information from the database that matches the specified criteria SELECT EmpName FROM Emp;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-14 The Results of a Query is a Relation A query pulls information from one or more relations and creates (temporarily) a new relation This allows for a query to: –Create a new relation –Feed information to another query (as a “sub-query”)

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-15 Displaying All Columns: * To show all of the column values for the rows that match the specified criteria, use an * SELECT * FROM Emp;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-16 Showing a Row Only Once: DISTINCT A qualifier may be added to the SELECT statement to inhibit duplicate rows from displaying SELECT DISTINCT DeptID FROM Emp;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-17 Specifying Search Criteria: WHERE The WHERE clause stipulates the matching criteria for the record that are to be displayed SELECT EmpName FROM Emp WHERE DeptID = 15;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-18 Match Criteria The WHERE clause match criteria may include –Equals “=“ –Not Equals “<>” –Greater than “>” –Less than “<“ –Greater than or Equal to “>=“ –Less than or Equal to “<=“

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-19 Match Operators Multiple matching criteria may be specified using –AND Representing an intersection of the data sets –OR Representing a union of the data sets

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-20 Operator Examples SELECT EmpName FROM Emp WHERE DeptID < 7 OR DeptID > 12;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-21 Operator Examples SELECT EmpName FROM Emp WHERE DeptID = 9 AND SalaryCode <= 23;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-22 A List of Values The WHERE clause may specify that a particular column value must be included in a list of values SELECT EmpName FROM Emp WHERE DeptID IN (4, 8, 9);

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-23 The Logical NOT Operator Any criteria statement may be preceded by a NOT operator which is to say that all information will be shown except that information matching the specified criteria SELECT EmpName FROM Emp WHERE DeptID NOT IN (4, 8, 9);

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-24 Finding Data Matching a Range of Values: BETWEEN SQL provides a BETWEEN statement that allows a user to specify a minimum and maximum value on one line SELECT EmpName FROM Emp WHERE SalaryCode BETWEEN 10 AND 45;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-25 Allowing for Wildcard Searches: LIKE Sometimes it may be advantageous to find rows matching a string value using wildcards –Single character wildcard character is an underscore (_) –Multiple character wildcard character is a percent sign (%)

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-26 Wildcard Search Examples SELECT EmpID FROM Emp WHERE EmpName LIKE ‘Kr%’; SELECT EmpID FROM Emp WHERE Phone LIKE ‘616-___-____’;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-27 Sorting the Results: ORDER BY The results may be sorted using the ORDER BY clause SELECT * FROM Emp ORDER BY EmpName;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-28 Built-in SQL Functions SQL provides several built-in functions –COUNT Counts the number of rows that match the specified criteria –MIN Finds the minimum value for a specific column for those rows matching the criteria –MAX Finds the maximum value for a specific column for those rows matching the criteria

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-29 Built-in SQL Functions (continued) SUM –Calculates the sum for a specific column for those rows matching the criteria AVG –Calculates the numerical average of a specific column for those rows matching the criteria

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-30 Built-in Function Examples SELECT COUNT(DISTINCT DeptID) FROM Emp; SELECT MIN(Hours), MAX(Hours), AVG(Hours) FROM Project WHERE ProjID > 7;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-31 Providing Subtotals: GROUP BY Subtotals may be calculated by using the GROUP BY clause SELECT DeptID, COUNT(*) FROM Emp GROUP BY DeptID HAVING Count(*) > 3;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-32 Retrieving Information from Multiple Tables SubQueries –As stated earlier, the result of a query is a relation. As a result, a query may feed another query. This is called a subquery Joins –Another way of combining data is by using a Join Join [also called an Inner Join] Left Outer Join Right Outer Join

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-33 Subquery SELECT EmpName FROM Emp WHERE DeptID in (SELECT DeptID FROM Department WHERE DeptName LIKE ‘Accounts%’);

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-34 Join SELECT EmpName FROM Emp, Department WHERE Emp.DeptID = Department.DeptID AND Department.DeptName LIKE ‘Account%’;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-35 Modifying Data using SQL Insert –Will add a new row in a table Update –Will update the data in a table that matches the specified criteria Delete –Will delete the data in a table that matches the specified criteria

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-36 Adding Data: INSERT To add a row to an existing table, use the INSERT statement INSERT INTO Emp VALUES (91, ‘Smither’, 12); INSERT INTO Emp (EmpID, SalaryCode) VALUES (62, 11);

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-37 Changing Data Values: UPDATE To change the data values in an existing row (or set of rows) use the Update statement UPDATE Emp SET Phone ‘ ’ WHERE EmpID = 29; UPDATE Emp SET DeptID = 44 WHERE EmpName LIKE ‘Kr%’;

DAVID M. KROENKE’S DATABASE CONCEPTS, 2nd Edition © 2005 Pearson Prentice Hall 3-38 Deleting Data: DELETE To delete a row or set of rows from a table using the DELETE statement DELETE FROM Emp WHERE EmpID = 29; DELETE FROM Emp WHERE EmpName LIKE ‘Kr%’;

Structured Query Language End of Presentation on Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition