Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application."— Presentation transcript:

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

2 Chapter Objectives To create and manage table structures using SQL statements To understand how referential integrity actions are implemented in SQL statements To create and use SQL constraints To understand several uses for SQL views To use SQL statements to create and use views To gain an understanding of how SQL is used in an application program To understand SQL/Persistent Stored Modules (SQL/PSM) To understand how to create and use user-defined functions To understand how to create and use triggers To understand how to create and use stored procedures KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-2

3 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. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-3

4 VRG Application Requirements KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-4

5 View Ridge Gallery Database Design KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-5

6 SQL Catagories SQL statements can be divided into five categories: –Data definition language (DDL) –Data manipulation language (DML) statements –SQL/Persistent Stored Modules (SQL/PSM) statements –Transaction control language (TCL) statements –Data control language (DCL) statements KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-6

7 SQL DDL and DML Data definition language (DDL) statements –Used for creating tables, relationships, and other structures –Covered in this chapter Chapter 7 Data manipulation language (DML) statements –Used for queries and data modification –Covered in this chapter and Chapter 2 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-7

8 SQL SQL/PSM SQL/Persistent Stored Modules (SQL/PSM) statements –Add procedural programming capabilities Variables Control-of-flow statements –Covered in Chapters: This chapter (general introduction) 10A (SQL Server 2012) 10B (Oracle Database 11g Release 2) 10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-8

9 SQL TCL Transaction control language (TCL) statements –Used to mark transaction boundaries and control transaction behavior –Covered in Chapters: 9 (general introduction) 10A (SQL Server 2012) 10B (Oracle Database 11g Release 2) 10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-9

10 SQL DCL Data control language (DCL) statements –Used to grant (or revoke) database permissions to (from) users and groups –Covered in Chapters: 9 (general introduction) 10A (SQL Server 2012) 10B (Oracle Database 11g Release 2) 10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-10

11 Chapter 7 SQL Elements KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-11

12 Creating the VRG Database KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-12

13 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 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-13

14 Specify Column Properties: SQL Server 2012 Data Types I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-14

15 Specify Column Properties: SQL Server 2012 Data Types II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-15

16 Specify Column Properties: SQL Server 2012 Data Types III KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-16

17 Specify Column Properties: Oracle Database 11g Release 2 Data Types I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-17

18 Specify Column Properties: Oracle Database 11g Release 2 Data Types II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-18

19 Specify Column Properties: Oracle Database 11g Release 2 Data Types III KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-19

20 Specify Column Properties: MySQL 5.6 Data Types I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-20

21 Specify Column Properties: MySQL 5.6 Data Types II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 6-21

22 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 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-22

23 Creating Relationships I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-23

24 Creating Relationships II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-24

25 Creating Relationships III KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-25

26 Implementing Cardinalities KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-26

27 Default Values and Data Constraints KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-27

28 SQL for Constraints KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-28

29 ALTER TABLE Statement ALTER TABLE 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; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-29

30 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; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-30

31 Adding and Dropping Constraints ALTER TABLE can be used to add a constraint as follows: ALTER TABLE CUSTOMER ADD CONSTRAINT MyConstraint CHECK (LastName NOT IN ('Robert-No-Pay')); ALTER TABLE can be used to drop a constraint: ALTER TABLE CUSTOMER DROP CONSTRAINT MyConstraint; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-31

32 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 TRANS DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-32

33 Removing Data Only SQL TRUNCATE TABLE: TRUNCATE TABLE TRANS; Cannot be used with a table that is referenced by a foreign key constraint. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-33

34 SQL DML—INSERT SQL INSERT statement: INSERT INTO ARTIST (LastName, FirstName, Nationality, DateOfBirth, DateDeceased) VALUES ('Tamayo', ‘Rufino', 'Mexican', 1899, 1991); Bulk INSERT: INSERT INTO ARTIST (LastName, FirstName, Nationality, DateOfBirth) SELECT LastName, FirstName, Nationality, DateOfBirth FROM IMPORTED_ARTIST; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-34

35 SQL DML—UPDATE SQL UPDATE statement: UPDATE CUSTOMER SET City = 'New York City' WHERE CustomerID = 1000; Bulk UPDATE: UPDATECUSTOMER SETAreaCode = '333' WHERE City = 'Denver'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-35

36 SQL DML—MERGE SQL MERGE command: MERGE INTO ARTIST AS A USING ARTIST_DATA AS ADR ON (A.LastName = ADR.LastName AND A.FirstName = ADR.FirstName WHEN MATCHED THEN UPDATE SET A.Nationality = ADR.Nationality WHEN NOT MATCHED THEN INSERT (LastName, FirstName, Nationality); KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-36

37 SQL DML—DELETE SQL DELETE statement: DELETE FROM CUSTOMER WHERECustomerID = 1000; If you omit the WHERE clause, you will delete every row in the table. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-37

38 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 ; DBMS products differ CUSTOMER AS C versus CUSTOMER C KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-38

39 VRG Data—CUSTOMER I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-39

40 VRG Data—CUSTOMER II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-40

41 VRG Data—ARTIST KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-41

42 VRG Data CUSTOMER_ARTIST_INT KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-42

43 VRG Data—WORK I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-43

44 VRG Data—WORK II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-44

45 VRG Data—TRANS I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-45

46 VRG Data—TRANS II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-46

47 SQL Views An 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, 13th Edition © 2014 Pearson Education, Inc. 7-47

48 SQL Views KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-48

49 CREATE VIEW Command CREATE VIEW command: CREATE VIEW CustomerNameView AS SELECTLastName AS CustomerLastName, FirstName AS CustomerFirstName, FROM CUSTOMER; Results: SELECT * FROMCustomerNameView ORDER BYCustomerLastName, CustomerFirstName; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-49

50 Updateable Views KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-50

51 Embedding SQL in Program Code SQL cursors are used to select one row at a time from pseudo-files. 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, 13th Edition © 2014 Pearson Education, Inc. 7-51

52 SQL Cursors in Program Code SQL can be embedded in triggers, stored procedures, and program code. A typical cursor code pattern is: DECLARE SQLCursor CURSOR FOR (SELECT * FROM CUSTOMER); OPEN SQLCursor: MOVE SQLCursorer to first row WHILE (SQLCursor not past last row) LOOP {SQL action statements go here} REPERT LOOP UNTIL DONE; CLOSE SQLCursor; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-52

53 SQL/Persistent Stored Modules (SSL/PSM) SQL/Persistent Stored Modules (SQL/PSM) is an ANSI/ISO standard for embedding procedural programming functionality into SQL Each DBMS product implements SQL/PSM in a different way, with some closer to the standard than others. –Microsoft SQL Server 2008 R2 calls its version Transact-SQL (T-SQL). –Oracle Database 11g calls its variant Procedural Language/SQL (PL/SQL). –MySQL implements SQL/PSM, but has no special name for its variant of SQL. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-53

54 User-Defined Functions I A user-defined function (stored function) is a stored set of SQL statements that: –is called by name from another SQL statement –may have input parameters passed to it by the calling SQL statement, and –returns an output value to the SQL statement hat called the function. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-54

55 User-Defined Functions II The NameConcatenation Function KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-55

56 User-Defined Functions III The NameConcatenation Function Using the NameConcatenation function: SELECT dbo.NameConcatenation(FirstName, LastName) AS CustomerName, AreaCode, PhoneNumber, Email FROM CUSTOMER ORDER BY CustomerName; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-56

57 User-Defined Functions IV The NameConcatenation Function NameConcatenation function results: KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-57

58 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 (INSTEAD OF and AFTER). MySQL supports six trigger types (BEFORE and AFTER). KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-58

59 Triggers II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-59

60 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 –Enforcing data constraints –Updating views –Performing referential integrity actions KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-60

61 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-61

62 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-62

63 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-63

64 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, 13th Edition © 2014 Pearson Education, Inc. 7-64

65 Stored Procedure Advantages Greater security as stored 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 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-65

66 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-66

67 KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-67

68 Triggers vs. Stored Procedures KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-68

69 David Kroenke and David Auer Database Processing Fundamentals, Design, and Implementation (13 th Edition) End of Presentation: Chapter Seven KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-69

70 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. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 7-70


Download ppt "David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application."

Similar presentations


Ads by Google