DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12.

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

Fundamentals, Design, and Implementation, 9/e Chapter 8 Database Redesign.
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.
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 10-1 COS 346 Day 21.
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 6-1 COS 346 Day 10.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 COS 346 Day 8.
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.
David M. Kroenke and David J
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 7 Structured Query Language.
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.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 13 Managing Databases with SQL Server 2000.
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 and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Six: Transforming Data Models into Database Designs.
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.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
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.
Oracle 11g: SQL Chapter 4 Constraints.
SQL for Database Construction and Application Processing
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 8/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Fundamentals, Design, and Implementation, 9/e Chapter 8 Database Redesign.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Managing Database With Oracle Replacement for Ch10 COP 4708.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
CREATE TABLE ARTIST ( ArtistID int NOT NULL IDENTITY (1,1), Namechar(25) NOT NULL, TEXT ERROR Nationality char (30) NULL, Birthdate numeric (4,0) NULL,
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
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.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL Triggers, Functions & Stored Procedures Programming Operations.
CH 10 DB Application Design n 10.1 Functions n 10.2 Case Application n 10.3 Creating, Reading, Updating, Deleting View n 10.4 Form Design n 10.5 Report.
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) 교수 홍 기 형.
Database Processing: David M. Kroenke’s Chapter Seven:
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Using SQL in Applications
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 7 Using SQL in Applications
Chapter 8 Database Redesign
Chapter 11 Managing Databases with SQL Server 2000
Presentation transcript:

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-2 Agenda Questions? Assignment 4 Corrected –1 B, 3 C’s, 1 D, 1 F and 1 non-submit Assignment 5 Due Capstone Progress Due Today we will discuss SQL for Database Construction and Application Processing –Chap 7 in DP We will be in the Oracle SQL text for the next 2+ weeks

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-3 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-4 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-5 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-6 View Ridge Gallery Database Design

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-7 SQL DDL and DML

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-8 The Database Design for ARTIST and WORK

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-9 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-10 How to for Oracle 10i CREATE TABLE ARTIST( ArtistID Int NOT NULL, Name Char(25) NOT NULL, Nationality Varchar(30) NULL, Birthdate Number(4,0) NULL, DeceasedDate Number(4,0) NULL, CONSTRAINT ArtistPK PRIMARY KEY (ArtistID), CONSTRAINT ArtistAK1 UNIQUE (Name), CONSTRAINT BirthValuesCheck CHECK (Birthdate < DeceasedDate), CONSTRAINT ValidBirthYear CHECK ((Birthdate > 1000) and (Birthdate < 2100)), CONSTRAINT ValidDeathYear CHECK ((DeceasedDate > 1000) and (DeceasedDate < 2100)));

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 ORACLE-VRG-Create-Tables-and-Sequences.sql

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-17

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-18

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-19 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-20 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-21 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-22 Removing Tables SQL DROP TABLE: DROP TABLE [TRANSACTION]; gets rid of the table and everything in it FOREVER gives error if table is parent in FK constraint 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-23 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-24 SQL DML: UPDATE UPDATE command (one row): UPDATE CUSTOMER SET City = 'New York City' WHERE CustomerID = 1000; Bulk UPDATE (bunch of rows): UPDATECUSTOMER SETAreaCode = '333' WHERE City = 'Denver'; BAD UPDATE (all rows): UPDATECUSTOMER SETAreaCode = '333‘;

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 More complicated updates UDATE PURCHASE_ORDER SET TaxRate = (SELECT Tax from TAX_TABLE WHERE TAX_TABLE.City = PURCHASE_ORDER.City) WHERE PURCHASE_ORDER.Number = 1000;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-27 SQL DELETE Delete a row DELETE FROM CUSTOMER WHERE CustomerID= 1000; Delete All rows (BAD) DELETE FROM CUSTOMER;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-28 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-29 VIEW RIDGE DATA

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-30 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-31 Result of Outer Join

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-32 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-33 SQL Views

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-34 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-35 Updateable Views

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-36 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-37 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-38 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-39

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-40

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-41 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 –In SQL Server 2005, it can be written in VB.NET, C#, and C++ 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-42 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-43

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-44 Triggers vs. Stored Procedures

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-45 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Seven Part Two