DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-2 Agenda Questions? Assignment 3 redo corrected –2 A, 4 B’s, 1 C & 1 F Assignment 4 corrected –1 A, 1 B, 3 C’s, 4 D’s & 1 Mia Assignment 5 posted –Due march 16 (after break) Capstone Progress Report due Quiz 1 Corrected –Problems with blackboard scoring –3 A’s, 3 B’s & 2 C’s Erwin tutorial – SQL for Database Construction and Application Processing
Course schedule Course Scheduling –I will be out from April 30 ??? –One (or two) weeks behind schedule –Possible options Drop Chap 15 ( + 2 Days) Eliminate one exam 7.55 10% ) (+1 day) Accelerate the SQL coding section ( + 1 day) ??? End course on April 27 ??? DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-3
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-4 David M. Kroenke’s Chapter Seven: SQL for Database Construction and Application Processing Part One Database Processing: Fundamentals, Design, and Implementation
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-5 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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-6 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 Web page
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-7 View Ridge Gallery Database Design
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-8 SQL DDL and DML
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-9 The Database Design for ARTIST and WORK
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-10 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:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-11 Data Types
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-12 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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-13 Creating Relationships
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-14 Implementing Cardinalities
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-15 Default Values and Data Constraints
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-16 SQL for Constraints
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-17 ALTER Statement ALTER statement changes table structure, properties, or constraints after it has been created Example: ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNum) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON DELETE NO ACTION;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-18 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 ;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-19 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;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-20 Removing Tables SQL DROP TABLE: DROP TABLE [TRANSACTION]; If there are constraints : ALTER TABLE CUSTOMER_ARTIST_INT DROP CONSTRAINT Customer_Artist_Int_CustomerFK; ALTER TABLE [TRANSACTION] DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-21 SQL DML - INSERT INSERT command: INSERT INTO ARTIST ([Name], Nationality, Birthdate, DeceasedDate) VALUES ('Tamayo', 'Mexican', 1927, 1998); Bulk INSERT: INSERT INTO ARTIST ([Name], Nationality, Birthdate) SELECT [Name], Nationality, Birthdate FROM IMPORTED_ARTIST;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-22 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Seven Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-23 David M. Kroenke’s Chapter Seven: SQL for Database Construction and Application Processing Part Two Database Processing: Fundamentals, Design, and Implementation
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-24 SQL DML: UPDATE UPDATE command: UPDATE CUSTOMER SET City = 'New York City' WHERE CustomerID = 1000; Bulk UPDATE: UPDATECUSTOMER SETAreaCode = '333' WHERE City = 'Denver';
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-25 SQL DML: DELETE DELETE command: DELETE FROM CUSTOMER WHERE CustomerID = 1000; If you omit the WHERE clause, you will delete every row in the table!
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-26 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; 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;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-27
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-28 Outer Joins Left Outer Join: SELECTC.[Name] Customer, A.[Name] Artist FROMCUSTOMER C LEFT JOIN CUSTOMER_ARTIST_INT CI ONC.CustomerID = CI.CustomerID LEFT JOIN ARTIST A ON CI.ArtistID = A.ArtistID;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-29 Result of Outer Join
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-30 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 multi- valued path through the schema
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-31 SQL Views
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-32 CREATE VIEW Command CREATE VIEW command: CREATE VIEW CustomerNameView AS SELECT[Name] AS CustomerName FROM CUSTOMER; To see the view use: SELECT * FROMCustomerNameView ORDER BYCustomerName; Results:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-33 Updateable Views
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-34 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 applications work on one row at a time Solution: process the SQL results as pseudofiles
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-35 Triggers 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)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-36 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: –Provide default values –Enforce data constraints –Update views –Perform referential integrity actions
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-37
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-38
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-39 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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-40 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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-41
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-42 Triggers vs. Stored Procedures
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-43 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Seven Part Two
Creating a databse in Sql 2005 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-44
Use a database DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-45
Create ViewRidge tables DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-46 dbp10_im_ch07_MSSQL_VRG