13.07.2015 Views

David M. Kroenke and David J. Auer Database Processing: SQL for ...

David M. Kroenke and David J. Auer Database Processing: SQL for ...

David M. Kroenke and David J. Auer Database Processing: SQL for ...

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>David</strong> M. <strong>Kroenke</strong> <strong>and</strong> <strong>David</strong> J. <strong>Auer</strong><strong>Database</strong> <strong>Processing</strong>:Fundamentals, Design, <strong>and</strong> ImplementationChapter Seven:<strong>SQL</strong> <strong>for</strong> <strong>Database</strong>Construction<strong>and</strong> Application<strong>Processing</strong>KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-1


Chapter Objectives• To be able to create <strong>and</strong> manage table structures using<strong>SQL</strong> statements• To underst<strong>and</strong> how referential integrity actions areimplemented in <strong>SQL</strong> statements• To be able to create <strong>and</strong> use <strong>SQL</strong> constraints• To underst<strong>and</strong> several uses <strong>for</strong> <strong>SQL</strong> views• To be able to use <strong>SQL</strong> statements to create <strong>and</strong> useviewsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-2


Chapter Objectives• To gain an underst<strong>and</strong>ing of how <strong>SQL</strong> is used in anapplication program• To underst<strong>and</strong> how to create <strong>and</strong> use triggers• To underst<strong>and</strong> how to create <strong>and</strong> use stored proceduresKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-3


View Ridge Gallery• View Ridge Gallery is a small art gallery that hasbeen in business <strong>for</strong> 30 years.• It sells contemporary European <strong>and</strong>North American fine art.• View Ridge has one owner,three salespeople, <strong>and</strong> 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, 11th Edition© 2010 Pearson Prentice Hall7-4


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


View Ridge Gallery <strong>Database</strong> DesignKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-6


<strong>SQL</strong> DDL <strong>and</strong> DMLKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-7


Creating the VRG <strong>Database</strong>KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-8


CREATE TABLE• CREATE TABLE statement is used <strong>for</strong> creatingrelations.• Each column is described with three parts:column name, data type, <strong>and</strong> optionalconstraints.• ExampleKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-9


Data Types:<strong>SQL</strong> Server 2008 Data TypesKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-10


Data Types:Oracle <strong>Database</strong> 11g Data TypesKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-11


Data Types:My<strong>SQL</strong> 5.1Data Types IKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-12


Data Types:My<strong>SQL</strong> 5.1Data Types IIKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-13


Constraints• Constraints can be defined within the CREATETABLE statement, or they can be added to thetable after it is created using the ALTER tablestatement.• Five types of constraints:– PRIMARY KEY may not have null values– UNIQUE may have null values– NULL/NOT NULL– FOREIGN KEY– CHECKKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-14


Creating RelationshipsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-15


Implementing CardinalitiesKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-16


Default Values <strong>and</strong> Data ConstraintsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-17


<strong>SQL</strong> <strong>for</strong> ConstraintsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-18


ALTER Statement• ALTER statement changes table structure,properties, or constraints after it has beencreated.• ExampleALTER TABLE ASSIGNMENTADD CONSTRAINT EmployeeFKFOREIGN KEY (EmployeeNumber)REFERENCES EMPLOYEE (EmployeeNumber)ON UPDATE CASCADEON DELETE NO ACTION;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-19


Adding <strong>and</strong> Dropping Columns• The following statement will add a columnnamed MyColumn to the CUSTOMERtable:ALTER TABLE CUSTOMER ADD MyColumn Char(5) NULL;• You can drop an existing column with thestatement:ALTER TABLE CUSTOMER DROP COLUMN MyColumn;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-20


Adding <strong>and</strong> Dropping Constraints• ALTER can be used to add a constraint asfollows:ALTER TABLE CUSTOMERADD CONSTRAINT MyConstraint CHECK([Name] NOT IN ('Robert No Pay'));• ALTER can be used to drop a constraint:ALTER TABLE CUSTOMERDROP CONSTRAINT MyConstraint;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-21


Removing Tables• <strong>SQL</strong> DROP TABLE:DROP TABLE TRANS;• If there are constraints:ALTER TABLE CUSTOMER_ARTIST_INTDROP CONSTRAINTCustomer_Artist_Int_CustomerFK;ALTER TABLE [TRANSACTION]DROP CONSTRAINT TransactionCustomerFK;DROP TABLE CUSTOMER;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-22


<strong>SQL</strong> DML—INSERT• INSERT comm<strong>and</strong>:INSERT INTO ARTIST ([Name], Nationality,DateOfBirth, DateDeceased)VALUES ('Tamayo', 'Mexican', 1927, 1998);• Bulk INSERT:INSERT INTO ARTIST([Name], Nationality, DateOfBirth)SELECT [Name], Nationality, BirthdateFROM IMPORTED_ARTIST;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-23


<strong>SQL</strong> DML—UPDATE• UPDATE comm<strong>and</strong>:UPDATE CUSTOMERSETCity = 'New York City'WHERE CustomerID = 1000;• Bulk UPDATE:UPDATE CUSTOMERSETWHEREAreaCode = '333'City = 'Denver';KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-24


<strong>SQL</strong> DML—DELETE• DELETE comm<strong>and</strong>:DELETE FROM CUSTOMERWHERE CustomerID = 1000;• If you omit the WHERE clause, you willdelete every row in the table.KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-25


JOIN ON Syntax• JOIN ON syntax:SELECT CUSTOMER.Name, ARTIST.NameFROMCUSTOMER JOIN CUSTOMER_ARTIST_INTON CUSTOMER.CustomerID =CUSTOMER_ARTIST_INT.CustomerIDJOIN ARTISTON CUSTOMER_ARTIST_INT.ArtistID =ARTIST.ArtistID;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-26


Using Aliases• Use of aliases:SELECT C.Name, A.NameFROMONCUSTOMER AS CJOINCUSTOMER_ARTIST_INT AS CIC.CustomerID = CI.CustomerIDJOIN ARTIST AS AON CI.ArtistID = A.ArtistID;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-27


VRG Data—CUSTOMER IKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-28


VRG Data—CUSTOMER IIKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-29


VRG Data—ARTISTKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-30


VRG DataCUSTOMER_ARTIST_INTKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-31


VRG Data—WORK IKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-32


VRG Data—WORK IIKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-33


VRG Data—TRANS IKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-34


VRG Data—TRANS IIKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-35


Outer Joins• Left Outer Join:SELECT C.LastName, C.FirstName,FROMONA.LastName AS ArtistNameCUSTOMER C LEFTJOINCUSTOMER_ARTIST_INT CIC.CustomerID = CI.CustomerIDLEFT JOIN ARTIST AON CI.ArtistID = A.ArtistID;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-36


Result of Outer JoinKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-37


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


<strong>SQL</strong> ViewsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-39


CREATE VIEW Comm<strong>and</strong>• CREATE VIEW comm<strong>and</strong>:CREATE VIEW CustomerNameView ASSELECTFROM• Results:SELECT *FROMORDER BYLastName ASCustomerLastName,FirstName ASCustomerFirstName,CUSTOMER;CustomerNameViewCustomerLastName,CustomerFirstName;KROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-40


Updateable ViewsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-41


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


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


Triggers IIKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-44


Firing Triggers• When a trigger is fired, the DBMS supplies:– Old <strong>and</strong> new values <strong>for</strong> the update– New values <strong>for</strong> inserts– Old values <strong>for</strong> deletions• The way the values are supplied depends on theDBMS product.• Trigger applications include:– Providing default values– En<strong>for</strong>ce data constraints– Updating views– Per<strong>for</strong>ming referential integrity actionsKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-45


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


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


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


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


Stored Procedure Advantages• Greater security as store procedures are alwaysstored on the database server• Decreased network traffic• <strong>SQL</strong> can be optimized by the DBMS compiler• Code sharing resulting in:– Less work– St<strong>and</strong>ardized processing– Specialization among developersKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-50


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


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


Triggers vs. Stored ProceduresKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-53


<strong>David</strong> <strong>Kroenke</strong> <strong>and</strong> <strong>David</strong> <strong>Auer</strong><strong>Database</strong> <strong>Processing</strong>Fundamentals, Design, <strong>and</strong> Implementation(11 th Edition)End of Presentation:Chapter SevenKROENKE AND AUER - DATABASE PROCESSING, 11th Edition© 2010 Pearson Prentice Hall7-54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!