© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8 (Part b): Advanced SQL...
-
Upload
ilene-briggs -
Category
Documents
-
view
215 -
download
0
Transcript of © 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8 (Part b): Advanced SQL...
© 2009 Pearson Education, Inc. Publishing as Prentice © 2009 Pearson Education, Inc. Publishing as Prentice HallHall 11
Chapter 8 (Part b):Chapter 8 (Part b):Advanced SQLAdvanced SQL
Modern Database Modern Database ManagementManagement
99thth Edition EditionJeffrey A. Hoffer, Mary B. Prescott, Jeffrey A. Hoffer, Mary B. Prescott,
Heikki TopiHeikki Topi
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 22
Conditional Expressions Using Conditional Expressions Using Case SyntaxCase Syntax
This is available with This is available with newer versions of newer versions of SQL, previously not SQL, previously not part of the part of the standardstandard
Figure 8-Figure 8-88
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 33
Tips for Developing Tips for Developing QueriesQueries
Be familiar with the data model (entities Be familiar with the data model (entities and relationships)and relationships)
Understand the desired resultsUnderstand the desired results Know the attributes desired in resultKnow the attributes desired in result Identify the entities that contain desired Identify the entities that contain desired
attributesattributes Review ERDReview ERD Construct a WHERE for each linkConstruct a WHERE for each link Fine tune with GROUP BY and HAING Fine tune with GROUP BY and HAING
clauses if neededclauses if needed Consider the effect on unusual dataConsider the effect on unusual data
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 44
Ensuring Transaction Ensuring Transaction IntegrityIntegrity
Transaction = A discrete unit of work Transaction = A discrete unit of work that must be completely processed or that must be completely processed or not processed at allnot processed at all May involve multiple updatesMay involve multiple updates If any update fails, then all other updates If any update fails, then all other updates
must be cancelledmust be cancelled SQL commands for transactionsSQL commands for transactions
BEGIN TRANSACTION/END TRANSACTIONBEGIN TRANSACTION/END TRANSACTION Marks boundaries of a transactionMarks boundaries of a transaction
COMMITCOMMIT Makes all updates permanentMakes all updates permanent
ROLLBACKROLLBACK Cancels updates since the last COMMITCancels updates since the last COMMIT
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 55
Figure 8-9 An SQL Transaction sequence (in pseudocode)
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 66
Data Dictionary FacilitiesData Dictionary Facilities
System tables that store metadataSystem tables that store metadata Users usually can view some of these tablesUsers usually can view some of these tables Users are restricted from updating themUsers are restricted from updating them Some examples in Oracle 10gSome examples in Oracle 10g
DBA_TABLES–descriptions of tablesDBA_TABLES–descriptions of tables DBA_CONSTRAINTS–description of constraintsDBA_CONSTRAINTS–description of constraints DBA_USERS–information about the users of the systemDBA_USERS–information about the users of the system
Examples in Microsoft SQL Server 2000Examples in Microsoft SQL Server 2000 SYSCOLUMNS–table and column definitionsSYSCOLUMNS–table and column definitions SYSDEPENDS–object dependencies based on foreign SYSDEPENDS–object dependencies based on foreign
keyskeys SYSPERMISSIONS–access permissions granted to usersSYSPERMISSIONS–access permissions granted to users
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 77
SQL:1999 and SQL:200SQL:1999 and SQL:200NN Enhancements/ExtensionsEnhancements/Extensions
User-defined data types (UDT)User-defined data types (UDT) Subclasses of standard types or an object typeSubclasses of standard types or an object type
Analytical functions (for OLAP)Analytical functions (for OLAP) CEILING, FLOOR, SQRT, RANK, DENSE_RANKCEILING, FLOOR, SQRT, RANK, DENSE_RANK WINDOW–improved numerical analysis WINDOW–improved numerical analysis
capabilitiescapabilities New Data TypesNew Data Types
BIGINT, MULTISET (collection), XMLBIGINT, MULTISET (collection), XML CREATE TABLE LIKE–create a new table CREATE TABLE LIKE–create a new table
similar to an existing onesimilar to an existing one MERGEMERGE
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 88
Persistent Stored Modules (SQL/PSM)Persistent Stored Modules (SQL/PSM) Capability to create and drop code Capability to create and drop code
modulesmodules New statements:New statements:
CASE, IF, LOOP, FOR, WHILE, etc.CASE, IF, LOOP, FOR, WHILE, etc. Makes SQL into a procedural languageMakes SQL into a procedural language
Oracle has propriety version called Oracle has propriety version called PL/SQL, and Microsoft SQL Server PL/SQL, and Microsoft SQL Server has Transact/SQLhas Transact/SQL
SQL:1999 and SQL:200SQL:1999 and SQL:200NN Enhancements/Extensions Enhancements/Extensions
(cont.)(cont.)
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 99
Routines and TriggersRoutines and Triggers RoutinesRoutines
Program modules that execute on demandProgram modules that execute on demand FunctionsFunctions–routines that return values and –routines that return values and
take input parameterstake input parameters ProceduresProcedures–routines that do not return –routines that do not return
values and can take input or output values and can take input or output parametersparameters
TriggersTriggers Routines that execute in response to a Routines that execute in response to a
database event (INSERT, UPDATE, or database event (INSERT, UPDATE, or DELETE)DELETE)
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1010
Figure 8-10 Triggers contrasted with stored procedures
Procedures are called explicitly
Triggers are event-drivenSource: adapted from Mullins, 1995.
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1111
Figure 8-11 Simplified trigger syntax, SQL:200n
Figure 8-12 Create routine syntax, SQL:200n
Chapter 8 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1212
Embedded and Dynamic Embedded and Dynamic SQLSQL
Embedded SQLEmbedded SQL Including hard-coded SQL statements in Including hard-coded SQL statements in
a program written in another language a program written in another language such as C or Javasuch as C or Java
Dynamic SQLDynamic SQL Ability for an application program to Ability for an application program to
generate SQL code on the fly, as the generate SQL code on the fly, as the application is runningapplication is running