




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、高级数据库技术高级数据库技术2020年秋季年秋季Chapter MORE SQL: Assertions, Views, and Programming TechniquesChapter Outline9.1 General Constraints as Assertions9.2 Views in SQL9.3 Database Programming9.4 Embedded SQL9.5 Functions Calls, SQL/CLI9.6 Stored Procedures, SQL/PSM9.7 SummaryChapter ObjectiveslSpecification of
2、more general constraints via assertionslSQL facilities for defining views (virtual tables)lVarious techniques for accessing and manipulating a database via programs in general-purpose languages (e.g., Java)Constraints as Assertionsl General constraints: constraints that do not fit in the basic SQL c
3、ategories (presented in chapter 8)l Mechanism: CREAT ASSERTIONcomponents include: a constraint name, followed by CHECK, followed by a conditionAssertions: An Examplel“The salary of an employee must not be greater than the salary of the manager of the department that the employee works forCREAT ASSER
4、TION SALARY_CONSTRAINTCHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.SALARY M.SALARY AND E.DNO=D.NUMBER AND D.MGRSSN=M.SSN)Using General AssertionslSpecify a query that violates the condition; include inside a NOT EXISTS clauselQuery result must be empty if the query r
5、esult is not empty, the assertion has been violatedSQL TriggerslObjective: to monitor a database and take action when a condition occurslTriggers are expressed in a syntax similar to assertions and include the following: event (e.g., an update operation) condition action (to be taken when the condit
6、ion is satisfied)SQL Triggers: An Examplel A trigger to compare an employees salary to his/her supervisor during insert or update operations:CREATE TRIGGER INFORM_SUPERVISORBEFORE INSERT OR UPDATE OFSALARY, SUPERVISOR_SSN ON EMPLOYEEFOR EACH ROWWHEN(NEW.SALARY (SELECT SALARY FROM EMPLOYEE WHERE SSN=
7、NEW.SUPERVISOR_SSN)INFORM_SUPERVISOR (NEW.SUPERVISOR_SSN,NEW.SSN;Views in SQLlA view is a “virtual” table that is derived from other tableslAllows for limited update operations (since the table may not physically be stored)lAllows full query operationslA convenience for expressing certain operations
8、Specification of Viewsl SQL command: CREATE VIEW a table (view) name a possible list of attribute names (for example, when arithmetic operations are specified or when we want the names to be different from the attributes in the base relations) a query to specify the table contentsSQL Views: An Examp
9、lelSpecify a different WORKS_ON tableCREATE TABLE WORKS_ON_NEW ASSELECT FNAME, LNAME, PNAME, HOURSFROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN=ESSN AND PNO=PNUMBERGROUP BY PNAME;Using a Virtual TablelWe can specify SQL queries on a newly create table (view):SELECT FNAME, LNAME FROM WORKS_ON_NEWWHERE P
10、NAME=Seena;lWhen no longer needed, a view can be dropped:DROP WORKS_ON_NEW;Efficient View ImplementationlQuery modification: present the view query in terms of a query on the underlying base tables disadvantage: inefficient for views defined via complex queries (especially if additional queries are
11、to be applied to the view within a short time period)Efficient View ImplementationlView materialization: involves physically creating and keeping a temporary table assumption: other queries on the view will follow concerns: maintaining correspondence between the base table and the view when the base
12、 table is updated strategy: incremental updateView UpdatelUpdate on a single view without aggregate operations: update may map to an update on the underlying base tablelViews involving joins: an update may map to an update on the underlying base relations not always possibleUn-updatable ViewslViews
13、defined using groups and aggregate functions are not updateablelViews defined on multiple tables using joins are generally not updateablel WITH CHECK OPTION: must be added to the definition of a view if the view is to be updated to allow check for updatability and to plan for an execution strategyDa
14、tabase ProgramminglObjective: to access a database from an application program (as opposed to interactive interfaces)lWhy? An interactive interface is convenient but not sufficient; a majority of database operations are made thru application programs (nowadays thru web applications)Database Programm
15、ing ApproacheslEmbedded commands: database commands are embedded in a general-purpose programming languagelLibrary of database functions: available to the host language for database calls; known as an APIlA brand new, full-fledged language (minimizes impedance mismatch)Impedance MismatchlIncompatibi
16、lities between a host programming language and the database model, e.g., type mismatch and incompatibilities; requires a new binding for each language set vs. record-at-a-time processinglneed special iterators to loop over query results and manipulate individual valuesSteps in Database Programming1.
17、 Client program opens a connection to the database server2. Client program submits queries to and/or updates the database3. When database access is no longer needed, client program terminates the connectionEmbedded SQLlMost SQL statements can be embedded in a general-purpose host programming languag
18、e such as COBOL, C, JavalAn embedded SQL statement is distinguished from the host language statements by EXEC SQL and a matching END-EXEC (or semicolon) shared variables (used in both languages) usually prefixed with a colon (:) in SQLExample: Variable Declarationin Language Cl Variables inside DECL
19、ARE are shared and can appear (while prefixed by a colon) in SQL statementsl SQLCODE is used to communicate errors/exceptions between the database and the programint loop;EXEC SQL BEGIN DECLARE SECTION;varchar dname16, fname16, ;char ssn10, bdate11, ;int dno, dnumber, SQLCODE, ;EXEC SQL END DECLARE
20、SECTION;SQL Commands forConnecting to a DatabaselConnection (multiple connections are possible but only one is active)CONNECT TO server-name AS connection-nameAUTHORIZATION user-account-info;lChange from an active connection to another oneSET CONNECTION connection-name;lDisconnectionDISCONNECT conne
21、ction-name; Embedded SQL in CProgramming Examplesloop = 1;while (loop) prompt (“Enter SSN: “, ssn);EXEC SQLselect FNAME, LNAME, ADDRESS, SALARYinto :fname, :lname, :address, :salaryfrom EMPLOYEE where SSN = :ssn;if (SQLCODE = 0) printf(fname, );else printf(“SSN does not exist: “, ssn);prompt(“More S
22、SN? (1=yes, 0=no): “, loop);END-EXEC Embedded SQL in CProgramming ExampleslA cursor (iterator) is needed to process multiple tupleslFETCH commands move the cursor to the next tuplelCLOSE CURSOR indicates that the processing of query results has been completedDynamic SQLl Objective: executing new (no
23、t previously compiled) SQL statements at run-time a program accepts SQL statements from the keyboard at run-time a point-and-click operation translates to certain SQL queryl Dynamic update is relatively simple; dynamic query can be complex because the type and number of retrieved attributes are unkn
24、own at compile timeDynamic SQL: An ExampleEXEC SQL BEGIN DECLARE SECTION;varchar sqlupdatestring256;EXEC SQL END DECLARE SECTION;prompt (“Enter update command:“, sqlupdatestring);EXEC SQL PREPARE sqlcommand FROM :sqlupdatestring;EXEC SQL EXECUTE sqlcommand;Embedded SQL in JavalSQLJ: a standard for e
25、mbedding SQL in JavalAn SQLJ translator converts SQL statements into Java (to be executed thru the JDBC interface)lCertain classes, e.g., java.sql have to be importedJava Database ConnectivitylJDBC: SQL connection function calls for Java programminglA Java program with JDBC functions can access any
26、relational DBMS that has a JDBC driverlJDBC allows a program to connect to several databases (known as data sources)Steps in JDBC Database Access1. Import JDBC library (java.sql.*)2. Load JDBC driver: Class.forname(“oracle.jdbc.driver.OracleDriver”)3. Define appropriate variables4. Create a connect object (via getConnection)5. Create a statement object from the Statement class:1.PreparedStatment2.CallableStatementSteps in JDBC Database Access(continued)6. Identify statement parameters (to be designated by question marks)7. Bound parameters to pr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 便利店店长合同协议书
- 租铺面餐饮合同协议书
- 合同协议书完整版
- 施工合同解除部分协议书
- 美容美体项目计划书
- 废船转让合同协议书模板
- 中间人合同协议书范本
- 创业计划书炸鸭爪
- 彩票店合伙合同协议书
- 奶粉销毁合同协议书范本
- 2025年物联网工程师考试试题及答案
- 肥胖症诊疗指南(2024年版)解读
- DBJ50-T-078-2016重庆市城市道路工程施工质量验收规范
- 中国历史地理智慧树知到期末考试答案章节答案2024年北京大学
- MOOC 跨文化交际通识通论-扬州大学 中国大学慕课答案
- C-TPAT反恐程序文件(完整版)
- 艾默生PEX系列精密空调技术手册
- 发改委招标代理服务收费管理暂行办法
- 10kV备自投调试报告
- 名著导读《简爱》ppt课件(58页)
- 人教部编版初中英语中考100个长难句实例分析
评论
0/150
提交评论