David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.

Slides:



Advertisements
Similar presentations
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Advertisements

Database Design Chapter Five DATABASE CONCEPTS, 6th Edition
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 9-1 COS 346 Day 20.
The Relational Model Chapter Two DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Ten: Managing Databases with SQL Server 2008.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Prentice Hall © COS 346 Day Agenda Questions? Assignment 7 Corrected –4 A’s and 4 B’s Assignment 8 posted –Due April 6 Quiz 2 next class.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 COS 346 Day 17.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
David M. Kroenke and David J
Database Design Chapter Five DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 7 th 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.
David M. Kroenke and David J. Auer Database Processing—12 th Edition Fundamentals, Design, and Implementation Chapter One: Introduction KROENKE AND AUER.
Getting Started Chapter One DATABASE CONCEPTS, 7th Edition
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Getting Started with Microsoft Access The Access Workbench: Section One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Structured Query Language
Getting Started with Oracle Database 11g Release 2 Express Edition Appendix B DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Six: Transforming Data Models into Database Designs.
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
1 IT420: Database Management and Organization SQL Views, Triggers and Stored Procedures 17 February 2006 Adina Crăiniceanu
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
SQL for Database Construction and Application Processing
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 8/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Ten A: Managing Databases with Oracle Database.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Eight: Database Redesign.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Ten A: Managing Databases with SQL Server 2012.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Six: Transforming Data Models into Database Designs.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix A: Getting Started with Microsoft Access 2013.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Three: The Relational Model and Normalization.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Ten: Managing Databases with SQL Server 2012,
Chapter Six: Transforming Data Models into Database Designs 6-1.
Chapter Seven: SQL for Database Construction and Application Processing.
Transforming Data Models
David M. Kroenke and David J
SQL for Database Construction and Application Processing
Ch7. SQL for DB construction and Application Processing 데이터베이스시스템 (ND352) 교수 홍 기 형.
David M. Kroenke and David J
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database Processing: David M. Kroenke’s Chapter Seven:
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Using SQL in Applications
Chapter 7 Using SQL in Applications
Getting Started Chapter One DATABASE CONCEPTS, 5th Edition
Getting Started Chapter One DATABASE CONCEPTS, 4th Edition
Presentation transcript:

David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application Processing 7-1 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Chapter Objectives To be able to create and manage table structures using SQL statements To understand how referential integrity actions are implemented in SQL statements To be able to create and use SQL constraints To understand several uses for SQL views To be able to use SQL statements to create and use views 7-2 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Chapter Objectives To gain an understanding of how SQL is used in an application program To understand how to create and use triggers To understand how to create and use stored procedures 7-3 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

View Ridge Gallery View Ridge Gallery is a small art gallery that has been in business for 30 years. It sells contemporary European and North American fine art. View Ridge has one owner, three salespeople, and two workers. View Ridge owns all of the art that it sells; it holds no items on a consignment basis. 7-4 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Application Requirements View Ridge application requirements: –Track customers and their artist interests –Record gallery’s purchases –Record customers’ art purchases –List the artists and works that have appeared in the gallery –Report how fast an artist’s works have sold and at what margin –Show current inventory in a Webpage 7-5 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

View Ridge Gallery Database Design 7-6 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL DDL and DML 7-7 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Creating the VRG Database 7-8 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

CREATE TABLE CREATE TABLE statement is used for creating relations. Each column is described with three parts: column name, data type, and optional constraints. Example 7-9 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Data Types: SQL Server 2008 Data Types 7-10 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Data Types: Oracle Database 11g Data Types 7-11 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Data Types: MySQL 5.1Data Types I 7-12 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Data Types: MySQL 5.1Data Types II 7-13 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Constraints Constraints can be defined within the CREATE TABLE statement, or they can be added to the table after it is created using the ALTER table statement. Five types of constraints: –PRIMARY KEY may not have null values –UNIQUE may have null values –NULL/NOT NULL –FOREIGN KEY –CHECK 7-14 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Creating Relationships 7-15 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Implementing Cardinalities 7-16 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Default Values and Data Constraints 7-17 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL for Constraints 7-18 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

ALTER Statement ALTER statement changes table structure, properties, or constraints after it has been created. Example ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON DELETE NO ACTION; 7-19 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Adding and Dropping Columns The following statement will add a column named MyColumn to the CUSTOMER table: ALTER TABLE CUSTOMER ADD MyColumn Char(5) NULL; You can drop an existing column with the statement: ALTER TABLE CUSTOMER DROP COLUMN MyColumn; 7-20 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Adding and Dropping Constraints ALTER can be used to add a constraint as follows: ALTER TABLE CUSTOMER ADD CONSTRAINT MyConstraint CHECK ([Name] NOT IN ('Robert No Pay')); ALTER can be used to drop a constraint: ALTER TABLE CUSTOMER DROP CONSTRAINT MyConstraint; 7-21 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Removing Tables SQL DROP TABLE: DROP TABLE TRANS; If there are constraints : ALTER TABLE CUSTOMER_ARTIST_INT DROP CONSTRAINT Customer_Artist_Int_CustomerFK; ALTER TABLE [TRANSACTION] DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER; 7-22 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL DML—INSERT INSERT command: INSERT INTO ARTIST ([Name], Nationality, DateOfBirth, DateDeceased) VALUES ('Tamayo', 'Mexican', 1927, 1998); Bulk INSERT: INSERT INTO ARTIST ([Name], Nationality, DateOfBirth) SELECT [Name], Nationality, Birthdate FROM IMPORTED_ARTIST; 7-23 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL DML—UPDATE UPDATE command: UPDATE CUSTOMER SET City = 'New York City' WHERE CustomerID = 1000; Bulk UPDATE: UPDATECUSTOMER SETAreaCode = '333' WHERE City = 'Denver'; 7-24 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL DML—DELETE DELETE command: DELETE FROM CUSTOMER WHERECustomerID = 1000; If you omit the WHERE clause, you will delete every row in the table KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

JOIN ON Syntax JOIN ON syntax: SELECTCUSTOMER.Name, ARTIST.Name FROMCUSTOMER JOIN CUSTOMER_ARTIST_INT ONCUSTOMER.CustomerID = CUSTOMER_ARTIST_INT.CustomerID JOIN ARTIST ON CUSTOMER_ARTIST_INT.ArtistID = ARTIST.ArtistID; 7-26 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Using Aliases Use of aliases: SELECTC.Name, A.Name FROMCUSTOMER AS C JOIN CUSTOMER_ARTIST_INT AS CI ONC.CustomerID = CI.CustomerID JOIN ARTIST AS A ON CI.ArtistID = A.ArtistID ; 7-27 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—CUSTOMER I 7-28 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—CUSTOMER II 7-29 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—ARTIST 7-30 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data CUSTOMER_ARTIST_INT 7-31 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—WORK I 7-32 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—WORK II 7-33 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—TRANS I 7-34 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

VRG Data—TRANS II 7-35 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Outer Joins Left Outer Join: SELECTC.LastName, C.FirstName, A.LastName AS ArtistName FROMCUSTOMER C LEFT JOIN CUSTOMER_ARTIST_INT CI ONC.CustomerID = CI.CustomerID LEFT JOIN ARTIST A ON CI.ArtistID = A.ArtistID; 7-36 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Result of Outer Join 7-37 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL Views SQL view is a virtual table that is constructed from other tables or views. It has no data of its own, but obtains data from tables or other views. SELECT statements are used to define views: –A view definition may not include an ORDER BY clause. SQL views are a subset of the external views: –They can be used only for external views that involve one multivalued path through the schema KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

SQL Views 7-39 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

CREATE VIEW Command CREATE VIEW command: CREATE VIEW CustomerNameView AS SELECTLastName AS CustomerLastName, FirstName AS CustomerFirstName, FROM CUSTOMER; Results: SELECT * FROMCustomerNameView ORDER BYCustomerLastName, CustomerFirstName; 7-40 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Updateable Views 7-41 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Embedding SQL in Program Code SQL can be embedded in triggers, stored procedures, and program code. Problem: assigning SQL table columns with program variables. Solution: object-oriented programming, PL/SQL. Problem: paradigm mismatch between SQL and application programming language: –SQL statements return sets of rows; an application works on one row at a time. Solution: process the SQL results as pseudo-files KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Triggers I A trigger is a stored program that is executed by the DBMS whenever a specified event occurs on a specified table or view. Three trigger types: BEFORE, INSTEAD OF, and AFTER: –Each type can be declared for Insert, Update, and Delete. –Resulting in a total of nine trigger types. Oracle supports all nine trigger types. SQL Server supports six trigger types (only for INSTEAD OF and AFTER triggers) KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Triggers II 7-44 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Firing Triggers When a trigger is fired, the DBMS supplies: –Old and new values for the update –New values for inserts –Old values for deletions The way the values are supplied depends on the DBMS product. Trigger applications include: –Providing default values –Enforce data constraints –Updating views –Performing referential integrity actions 7-45 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

7-46 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

7-47 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

7-48 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Stored Procedures A stored procedure is a program that is stored within the database and is compiled when used: –In Oracle, it can be written in PL/SQL or Java. –In SQL Server, it can be written in TRANSACT-SQL. Stored procedures can receive input parameters and they can return results. Stored procedures can be called from: –Programs written in standard languages, e.g., Java, C#. –Scripting languages, e.g., JavaScript, VBScript. –SQL command prompt, e.g., SQL Plus, Query Analyzer KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Stored Procedure Advantages Greater security as store procedures are always stored on the database server Decreased network traffic SQL can be optimized by the DBMS compiler Code sharing resulting in: –Less work –Standardized processing –Specialization among developers 7-50 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

7-51 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

7-52 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

Triggers vs. Stored Procedures 7-53 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

David Kroenke and David Auer Database Processing Fundamentals, Design, and Implementation (11 th Edition) End of Presentation: Chapter Seven 7-54 KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall

KROENKE AND AUER: DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 7-55 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 © 2010 Pearson Education, Inc. Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall