SQL Programming.pdf

  1. 1、本文档共5页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
SQL Programming

CS145 Lecture Notes #10 SQL Programming Example schema: CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10), PRIMARY KEY(SID, CID)); CREATE TABLE Course (CID CHAR(10) PRIMARY KEY, title VARCHAR(100) UNIQUE); Motivation Pros and cons of SQL: Very high-level, possible to optimize Not tuned to support general-purpose computation Oracle as a calculator? SELECT 142857*3 FROM DUAL; Strictly less expressive than general-purpose languages SQL2 has no recursion and cannot even compute factorial! Solutions: Augment SQL: Oracle’s PL/SQL Use SQL together with a general-purpose programming language: embedded SQL, dynamic SQL Oracle PL/SQL Basics Rough form of a PL/SQL program: DECLARE BEGIN END; . RUN; DECLARE section is optional . and RUN end the program and execute it Jun Yang 1 CS145 Spring 1999 Example: go through students 142–857 and set all GPA’s under 4.0 to 4.0 DECLARE thisSID Student.SID%TYPE; thisGPA Student.GPA%TYPE; BEGIN thisSID := 142; LOOP EXIT WHEN (thisSID 857); SELECT GPA INTO thisGPA FROM Student WHERE SID = thisSID; IF (thisGPA 4.0) THEN UPDATE Student SET GPA = 4.0 WHERE SID = thisSID; END IF; thisSID := thisSID + 1; END LOOP; END; . RUN; Basic features: Local variable: Use %TYPE to match its type to a column in the schema Use := for assignment; = for comparison Branch: IF (...) THEN ... ELSE ... END IF; Loop: LOOP ... EXIT WHEN (...); ... END LOOP; The usual data modification statements: INSERT, DELETE, UPDATE Single-row SELECT: SELECT ... INTO ... FROM ...; Oracle raises an exception if SELECT returns no rows or more than one row Cursors Inside a PL/SQL program, the result of a SELECT must go somewhere: If SELECT returns one row, it can go INTO a variable What if SELECT returns multiple rows? Cursor: a variable that runs through the result of a SELECT, row by row Declare by: CURSOR IS ; Use inside a cursor loop: Fetch one result row at a time: FETCH INTO ; Break the loop when

文档评论(0)

l215322 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档