1 IT420: Database Management and Organization SQL Views, Triggers and Stored Procedures 17 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 9-1 COS 346 Day 20.
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Getting Started (Excerpts) Chapter One DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
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.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
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.
Transforming Data Models into Database Designs
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database Technical Session By: Prof. Adarsh Patel.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
SQL Server 7.0 Maintaining Referential Integrity.
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.
CSE 3330 Database Concepts Stored Procedures. How to create a user CREATE USER.. GRANT PRIVILEGE.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
1 IT420: Database Management and Organization Database Security 5 April 2006 Adina Crăiniceanu
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Dimu' Rumpak © 2009 by Prentice Hall 1 Getting Started Didimus Rumpak, M.Si. Database Concepts Chapter 1 1.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Data Driven Designs 99% of enterprise applications operate on database data or at least interface databases. Most common DBMS are Microsoft SQL Server,
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Oracle 11g: SQL Chapter 4 Constraints.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Visual Programing SQL Overview Section 1.
1 IT420: Database Management and Organization SQL Views 15 February 2006 Adina Crăiniceanu
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
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.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
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 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.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
7.5 Using Stored-Procedure and Triggers NAME MATRIC NUM GROUP Muhammad Azwan Bin Khairul Anwar CS2305A Muhammad Faiz Bin Badrol Shah CS2305B.
Chapter Seven: SQL for Database Construction and Application Processing.
A Guide to SQL, Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Using SQL in Applications
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 11 Managing Databases with SQL Server 2000
Triggers.
Triggers 7/11/2019 See scm-intranet.
Presentation transcript:

1 IT420: Database Management and Organization SQL Views, Triggers and Stored Procedures 17 February 2006 Adina Crăiniceanu

Kroenke, Database Processing2 Last time  SQL Views

Kroenke, Database Processing3 Today  Updates on views  Triggers  Stored procedures

Kroenke, Database Processing4 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  It only has a definition

Kroenke, Database Processing5 CREATE VIEW Command  CREATE VIEW command: CREATE VIEW view_name AS select_statement  Use the view:  In SELECT statements  Sometimes in INSERT statements  Sometimes in UPDATE statements  Sometimes in DELETE statements

Kroenke, Database Processing6 Uses for SQL Views  Security: hide columns and rows  Display results of computations  Hide complicated SQL syntax  Provide a level of isolation between actual data and the user’s view of data  three-tier architecture  Assign different processing permissions to different views on same table  Assign different triggers to different views on same table

Kroenke, Database Processing7 Using Views  Customer(CustID, CustName, Address, Phone)  CREATE VIEW CustomerV AS SELECT * FROM Customers  SELECT * FROM CustomerV CustIDCustNameAddressPhone 01Amy GaleAnnapolis, MD Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 01Amy GaleAnnapolis, MD Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD Customers SELECT * FROM CustomerV query result:

Kroenke, Database Processing8 Using Views  CREATE VIEW CustomerV AS SELECT * FROM Customers  SELECT * FROM CustomerV WHERE Address LIKE ‘%Annapolis%’ CustIDCustNameAddressPhone 01Amy GaleAnnapolis, MD Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 01Amy GaleAnnapolis, MD Chris O’NealAnnapolis, MD Customers SELECT query result:

Kroenke, Database Processing9 UPDATE on Views  CREATE VIEW CustomerV AS SELECT * FROM Customers  UPDATE CustomerV SET Phone = ‘ ’ WHERE CustID = 01 CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD Customers table before update: Customers table after update: UPDATE impacts the Customers table

Kroenke, Database Processing10 INSERT on Views  CREATE VIEW CustomerV AS SELECT * FROM Customers  INSERT INTO CustomerV VALUES(‘08’,’Scott White’,’DC’,’ ’) CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD Scott WhiteDC Customers table Customers table after insert: INSERT impacts the Customers table

Kroenke, Database Processing11 DELETE on Views  CREATE VIEW CustomerV AS SELECT * FROM Customers  DELETE FROM CustomerV WHERE Address LIKE ‘%Annapolis%’ CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 02Tom SmithBaltimore, MD Customers table Customers table after delete: DELETE impacts the Customers table

Kroenke, Database Processing12 Using Views – Case 2  Customer(CustID, CustName, Address, Phone)  CREATE VIEW CustomerV2 AS SELECT CustID, CustName, Phone FROM Customers  SELECT * FROM CustomerV2 CustIDCustNameAddressPhone 01Amy GaleAnnapolis, MD Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNamePhone 01Amy Gale Tom Smith Chris O’Neal Customers SELECT * FROM CustomerV2 query result:

Kroenke, Database Processing13 INSERT on Views - Case 2  CREATE VIEW CustomerV2 AS SELECT CustID, CustName, Phone FROM Customers  INSERT INTO CustomerV2 VALUES(‘08’,’Scott White’,’ ’) CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD CustIDCustNameAddressPhone 01Amy GaleAnnapolis Tom SmithBaltimore, MD Chris O’NealAnnapolis, MD Scott WhiteNULL Customers table Customers table after insert: Address NOT NULL, INSERT fails

Kroenke, Database Processing14 Views – Case 3  Rental(RentalID, CustID, PlaneID, NbHours, HRate)  CREATE VIEW RentalView AS SELECT RentalID, CustID, PlaneID, NbHours*HRate AS Charge FROM Rental  SELECT * FROM RentalView RentalIDCustIDPlaneIDNbHoursHRate $ $ $420 RentalIDCustIDPlaneIDCharge $ $ $420 Rental SELECT * FROM RentalView query result:

Kroenke, Database Processing15 INSERT on Views – Case 3  CREATE VIEW RentalView AS SELECT RentalID, CustID, PlaneID, NbHours*HRate AS Charge FROM Rental  INSERT INTO RentalView VALUES (03,113,01,1250) INSERT fails! UPDATE Charge fails! RentalIDCustIDPlaneIDNbHoursHRate $ $ $420 RentalIDCustIDPlaneIDCharge $ $ $420 Rental SELECT * FROM RentalView query result:

Kroenke, Database Processing16 Updateable Views  Views based on a single table  No computed columns  All non-null columns present in view  Views with INSTEAD OF triggers defined on them  Views based on a single table, primary key in view, some non-null columns missing from view  Updates for non-computed columns ok  Deletes ok  Inserts not ok

Kroenke, Database Processing17 Triggers  Trigger: stored program that is executed by the DBMS whenever a specified event occurs  Associated with a 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

Kroenke, Database Processing18 Programming Languages for Triggers  Depends on DBMS  Java or PL/SQL for Oracle  T-SQL for SQL Server  C++, C#, Visual Basic.NET for SQL Server 2005

Kroenke, Database Processing19 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

Kroenke, Database Processing20 Create trigger  CREATE TRIGGER trigger_name ON table_or_view_name AFTER | BEFORE | INSTEAD OF INSERT | UPDATE | DELETE AS trigger_code

Kroenke, Database Processing21 Trigger for Complex Default Value  Trans(TransactionID, WorkID, AcquisitionPrice, AskingPrice)  ArtistWorkNet(SaleID, WorkID, NetPrice)  AskingPrice = max of  2*AcquisitionPrice  AcquisitionPrice+AVG(Past_NetPrice), if WorkID already in table

Kroenke, Database Processing22 Declare variables Create trigger Built-in function

Kroenke, Database Processing23

Kroenke, Database Processing24 Trigger for Referential Integrity Actions – generic code

Kroenke, Database Processing25 Class Exercise  Students(Alpha, LName, FName, GPA)  Enroll(Alpha, CourseID, Semester, Grade)  GradeValues(LetterGrade, PointValue)  Define a trigger to update the GPA every time the student gets a new grade, or a grade changes

Kroenke, Database Processing26 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, Database Processing27 Stored Procedure Advantages  Greater security as store procedures are always stored on the database server  SQL can be optimized by the DBMS compiler  Code sharing resulting in:  Less work  Standardized processing  Specialization among developers

Kroenke, Database Processing28

Kroenke, Database Processing29 Triggers vs. Stored Procedures

Kroenke, Database Processing30 Project 1 – Due March 3, 2006  National College Learning Center Organization (NCLCA)  Membership  Conferences organized  Have: Partial user requirements (forms)  Tasks:  Design the ER model  Transform ER model to tables  Verify tables are normalized  Write SQL to create the tables in SQL Server  Write SQL to answer typical user queries  Write-up explaining your work