New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 3 notes


Week 3 notes 95-736


Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

intro to advanced SQL and PL/SQL PL/SQL blocks. attributes.
Advanced Relational Database Management
Randy Trzeciak
Class Notes
25 ?




Popular in Advanced Relational Database Management

Popular in Heinz

This 4 page Class Notes was uploaded by ZAID AL-MOMEN on Tuesday September 20, 2016. The Class Notes belongs to 95-736 at Carnegie Mellon University taught by Randy Trzeciak in Fall 2016. Since its upload, it has received 19 views. For similar materials see Advanced Relational Database Management in Heinz at Carnegie Mellon University.


Reviews for Week 3 notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/20/16
Lecture 3 PL/SQL is a third generation language. difference between 3GL and 4GL. 3GL tells the db what to do AND how to do it! select data, process it. example: PL/SQL 4GL only tells the db what to do. example: SELECT Why use PL/SQL? - complex business rules. example: a staff cannot manage three properties at a time. conditional logic. ** Could you write a query in pure SQL that allows for a condition IF .. THEN .. ELSE... YES! using CASE STATEMENTS. Advantages of PL/SQL. anonymous block. open up a worksheet and start to type PL/SQL code. named block. Basic structure. DECLARE SECTION (optional) BEGIN SECTION EXCEPTION HANDLING (optional) END; no case sensitivity in PL/SQL. DECLARE section takes local variables. give it a datatype. embedded SQL like SELECT statements in the BEGIN block. database locks the selected data to avoid interference from other users. the FOR UPDATE OF quantity statement locks the value and row to make sure no one can change it. if an update/insert fails within the BEGIN/END block, automatic rollback. use forward slash ( / ) or type EXECUTE to execute statemnt. program will throw error if the SELECT statement returns more than one row (INTO a local variable) CURSORS can get a record set and update it row by row. REM stands for SQL REMARKS. usually a comment. DBMS_OUTPUT is a package used to print out data from the SERVER. make sure to use it with SET SERVEROUTPUT ON important types in pl/sql: 1. scalar types. number/integer/date/etc... 2. composite types. (Table/Record) 3. Reference Type. (CURSOR) using constant variable vs. local variable. constants cannot change once initialized. Un-initialized local variables are default to null. general practice to use v_ for the local variable to avoid column conflict. v_acct_id INTEGER(5) NOT NULL; DOES NOT WORK! once created as integer, cannot force not null without initializing; Two ways to assign values to variables: 1. assign (:=) operator 2. SELECT .. INTO v_variable Anchoring a datatype %TYPE useful when we don't know the column type that we are selecting from. ex: v_radius AREAS.radius%TYPE; v_area areas.area%TYPE; - eliminates the need to change the local var in future. - more dynamic that way. Boolean v_done is a boolean. If v_done if (v_done = TRUE) BOTH CORRECT! if the local variable is not initialized, it is not a valid comparison. PL/SQL provides over 75 functions that can be used for error reporting, number-, date- char manipulation. PL/SQL can support DML ( SELECT INSERT UPDATE DELETE) PL/SQL does NOT support DDL (CREATE, DROP, ALTER, GRANT, REVOKE) DDL does an auto commit, therefore cannot be used in pl/sql, because pl/sql has a BEGIN .. END which is a ransaction. either all commits or all rolls back. EXCEPTION! PL/SQL DDL. use EXECUTE IMMEDIATE BEGIN example: execute immediate 'truncate table viewing'; END PREFERABLY not do DDL within PL/SQL. assume CREATE TABLE AREAS (radius NUMBER, area NUMBER); DECLARE v_radius areas.radius%TYPE; v_area areas.area%TYPE; BEGIN SELECT radius, area INTO v_radius, v_area FROM areas WHERE radius = 5; END; careful with how many rows the SELECT statement returns. has to return only one! alternatively use CURSOR. INSERTS in PL/SQL. One and only one row. same as SELECT. UPDATES in PL/SQL. not dependent on values of local var. rows updated could 0 or 1 or many rows. DELETES in PL/SQL. same as updates, can impact 0 or 1 or however many rows based on condition. CONTROL STRUCTURES IN PLSQL: IF THEN ELSE IF THEN ELSIF (NOTICE not ELSEIF) Looping Structures: regular loop (LOOP .. END LOOP) - pro: executes the code at least once. - con: will loop infinitely unless there is a condition inside the loop. WHILE cond. LOOP .. END LOOP; FOR counter IN lower bound .. higher bound .. LOOP for i in 1..25 loop sum:= sum + 1; GOTO statement. unconditional jump in the pl/sql program. Not recommended, because spaghetti code, hard to follow. NULL; this statement does nothing. for coding consistency. ----- RECORDS. get entire row of a table DECLARE TYPE t_deptRecTyp IS RECORD (deptno NUMBER(2):=-1; dname dept.dname%TYPE, loc dept.loc%TYPE); v_dept_rec t_DeptRecType; /*variable is v_dept_rec*/ manually choose the placeholders in a RECORD. if we dont know the length of record DECLARE v_deptRecType department%ROWTYPE; this will enable the use of v_deptRecType.dname local variable reference. CURSOR: SQL queries sometimes return more than one row. Basic use: OPEN .. FETCH .. CLOSE give the cursor a name c1 IS select name from mechanics where salary > 50000. if there are 10 mechanics .. cursor will fetch each row. BEGIN open loop fetch exit when c1%notFOUND; output.put_line(v_name)l End loop; CLOSE c1; END; / CURSOR attributes. - EXIT WHEN c1%NOTFOUND -- breaks out of Style guidelines loop. Use capitalization,indentation,and nesting to make your code easy to follow - %ISOPEN: returns true if cursor is open, else • Followaconventionfornaminguser-defined FALSE. lexical units, e.g.: – v_name for variables which row is the cursor on. – e_name for exceptions use – c_name for cursors - %ROWCOUNT – t_name for user defined types


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Anthony Lee UC Santa Barbara

"I bought an awesome study guide, which helped me get an A in my Math 34B class this quarter!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.