Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
C20.0046: Database Management Systems Lecture #19 M.P. Johnson Stern School of Business, NYU Spring, 2005 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 1 Homework Project part 3 due today Project part 4 up soon… Topic: populating your tables with data Using MySQL’s bulk loader Can get errors parsing Start early! M.P. Johnson, DBMS, Stern/NYU, Spring 2005 2 NYU Infrastructure News The Oracle client should be working as usual again Can run sqlplus and Pro*C programs without touching TWO_TASK M.P. Johnson, DBMS, Stern/NYU, Spring 2005 3 Agenda: Programming for SQL Embedded SQL Pro*C, SQLJ CLI SQL/CLI in C JDBC in Java DBI/DBDs in Perl, PHP Stored Procedures: PL/SQL, Triggers MySQL Bulk Loader M.P. Johnson, DBMS, Stern/NYU, Spring 2005 4 Goals Have more confidence in writing scripts/functions/procedures 1. Be able to define a simple trigger 2. 3. Be able to respond to ftn/proc errors So code executes at the right time Understand how to load a text file of records into MySQL M.P. Johnson, DBMS, Stern/NYU, Spring 2005 5 PL/SQL “Procedural Language for SQL” Oracle’s language for stored procedures Simple, interpreted, procedural language But Pascal-like: BEGIN END, not { } AND OR, not && || vars defined at top of procedre how return works M.P. Johnson, DBMS, Stern/NYU, Spring 2005 6 Hello, World Try again… SET SERVEROUTPUT ON BEGIN -- print out message DBMS_OUTPUT.PUT_LINE('Hello World, from PL/SQL'); END; / M.P. Johnson, DBMS, Stern/NYU, Spring 2005 7 Example procedure Define the procedure: CREATE PROCEDURE testProcedure AS BEGIN INSERT INTO Student VALUES (5, ‘Godel'); END; Now we can call it: EXEC testProcedure M.P. Johnson, DBMS, Stern/NYU, Spring 2005 8 Procedure I/O example A procedure to take a beer and price and add it to Joe's menu: Sells(bar, beer, price) CREATE PROCEDURE izzyMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE) AS BEGIN INSERT INTO Sells VALUES(‘Izzy’’s', b, p); END; / M.P. Johnson, DBMS, Stern/NYU, Spring 2005 9 Function example CREATE OR REPLACE FUNCTION maxval(a IN int, b IN int) RETURN int AS BEGIN IF a > b THEN RETURN a; ELSE RETURN b; END IF; END maxval; INSERT INTO R VALUES(“abc”, maxval(5,10)); http://pages.stern.nyu.edu/~mjohnson/dbms/eg/lec19/maxval.sql M.P. Johnson, DBMS, Stern/NYU, Spring 2005 10 http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/wordcount.sql CREATE OR REPLACE FUNCTION wordcount (str IN VARCHAR2) RETURN PLS_INTEGER AS /* words PLS_INTEGER := 0; ***Commented out for intentional error*** */ len PLS_INTEGER := NVL(LENGTH(str),0); inside_a_word BOOLEAN; BEGIN FOR i IN 1..len + 1 LOOP IF ASCII(SUBSTR(str, i, 1)) < 33 OR i > len THEN IF inside_a_word THEN words := words + 1; inside_a_word := FALSE; END IF; ELSE inside_a_word := TRUE; END IF; END LOOP; RETURN words; END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 Word count program 11 Getting errors Simple says: Warning: Function created with compilation errors. To get actual errors, say SHOW ERR(ORS) Can also get errors per object: SQL> show errors function wordcount Warning: must get object type right! Can also look at user_errors tbl directly M.P. Johnson, DBMS, Stern/NYU, Spring 2005 12 Stored ftns & procs persist Once a function or procedure is created, it persists until it’s dropped CREATE OR REPLACE FUNCTION … Stored procs are stored in the DB itself In user_procedures in Oracle SELECT object_name from user_procedures; Also, can describe ftns and procs: SQL> describe wordcount M.P. Johnson, DBMS, Stern/NYU, Spring 2005 13 Calling functions and procedures Procedures can simple executed, ftns can’t How to just call a ftn? Can use dbms_output, as seen Can also select the ftn value from dual 1. 2. SQL> select(wordcount(‘hi there’) from dual; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 14 Programs and rights By default, only the creator of a program may run it (apart from the admin) If others should run, must GRANT them permission: SQL> GRANT EXECUTE ON wordcount TO george; Permissions can be revoked: SQL> REVOKE EXECUTE FROM wordcount TO george; Can also grant to particular roles or everyone: SQL> GRANT EXECUTE ON wordcount TO everyone; Wider/narrower grant ops are independent… M.P. Johnson, DBMS, Stern/NYU, Spring 2005 15 Branching IF–THEN statements use THEN Must end with END IF Use ELSIF in place of ELSE IF Example: IF <condition> THEN <statement(s)> ELSIF <statement(s)> END IF; http://pages.stern.nyu.edu/~mjohnson/dbms/eg/lec19/maxval.sql M.P. Johnson, DBMS, Stern/NYU, Spring 2005 16 More ifs IF <condition> ELSE END IF; IF <expression> ELSEIF <expression> ELSE END IF; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 17 Multiple elsifs An if statement can have multiple elseifs: IF salary >= 10000 AND salary <= 20000 THEN give_bonus(employee_id, 1500); ELSIF salary > 20000 AND salary <= 40000 THEN give_bonus(employee_id, 1000); ELSIF salary > 40000 THEN give_bonus(employee_id, 400); END IF; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 18 Loop example DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END; / M.P. Johnson, DBMS, Stern/NYU, Spring 2005 19 More loops Infinite loop: while loop: LOOP executable_statements; END LOOP; WHILE condition LOOP executable_statements; END LOOP; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 20 For loops Integer for loop: FOR for_index IN low_value .. high_value LOOP executable_statements; END LOOP; Cursor for loop: FOR record_index IN my_cursor LOOP executable_statements; END LOOP; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 21 For loop example FOR my-rec IN my-cursor LOOP … END LOOP; Example: http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/for.sql M.P. Johnson, DBMS, Stern/NYU, Spring 2005 22 PL/SQL v. SQL There are some things SQL can’t do (e.g., factorial), but some problems can be solved in both DECLARE CURSOR checked_out_cur IS SELECT pet_id, name, checkout_date FROM occupancy WHERE checkout_date IS NOT NULL; BEGIN FOR checked_out_rec IN checked_out_cur LOOP INSERT INTO occupancy_history (pet_id, name, checkout_date) VALUES (checked_out_rec.pet_id, checked_out_rec.name, checked_out_rec.checkout_date); DELETE FROM occupancy WHERE pet_id = checked_out_rec.pet_id; END LOOP; 23 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 END; PL/SQL v. SQL The same thing can be done w/o a cursor: BEGIN INSERT INTO occupancy_history (pet_id, NAME, checkout_date) SELECT pet_id, NAME, checkout_date FROM occupancy WHERE checkout_date IS NOT NULL; DELETE FROM occupancy WHERE checkout_date IS NOT NULL; END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 24 Dynamic PL/SQL Saw “dynamic SQL” in the cases of Pro*C and JDBC Ability to run ad-hoc (non-hard-coded) SQL in programs/scripts Can also do this in PL/SQL EXECUTE IMMEDIATE <string>; The string can be passed in, created from concatenation, etc. M.P. Johnson, DBMS, Stern/NYU, Spring 2005 25 Dynamic PL/SQL E.g.: write function to return number rows in an arbitrary table CREATE OR REPLACE FUNCTION rowCount ( tabname IN VARCHAR2) return integer as retval integer; begin execute immediate 'select count(*) from ' || tabname into retval; return retval; end; / M.P. Johnson, DBMS, Stern/NYU, Spring 2005 26 Dynamic PL/SQL for DDL Ordinarily can’t do DDL in PL/SQL But you can in dynamic PL/SQL Here’s an e.g.: CREATE OR REPLACE procedure dropproc(procname in varchar2) as begin execute immediate 'drop procedure ' || procname; end; / M.P. Johnson, DBMS, Stern/NYU, Spring 2005 27 More on PL/SQL O’Reilly’s Oracle PL/SQL Programming: PL/SQL Reference & Tutorial: http://www.ilook.fsnet.co.uk/ora_sql/sqlmain2.htm Introduction to PL/SQL: http://www.unix.org.ua/orelly/oracle/prog2/ This lecture somewhat follows 3rd edition of this book http://www.geocities.com/cliktoprogram/plsql/introduction.html Oracle FAQ's Script and Code Exchange: http://www.orafaq.com/scripts/ M.P. Johnson, DBMS, Stern/NYU, Spring 2005 28 Live examples Factorial function: Converting between bases: http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/ fact.sql http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/ numsys.sql Directory of examples: http://pages.stern.nyu.edu/~mjohnson/dbms/plsql/ M.P. Johnson, DBMS, Stern/NYU, Spring 2005 29 New topic: Triggers PL/SQL programs that run automatically (are “triggered”) when a certain event occurs E.g.: on insert to some table On system start-up On delete from table Big benefit: need not be called explicitly However row in table x is deleted, the trigger gets called M.P. Johnson, DBMS, Stern/NYU, Spring 2005 30 Trigger events Trigger code may be “triggered” by many kinds of events: Oracle start-up/shut-down Data updates: Triggers may replace initialization scripts Delete: maybe delete related rows Inserts Updates: maybe make other rows consistent Delete: maybe prevent DDL statements Log creation of all objects, e.g. M.P. Johnson, DBMS, Stern/NYU, Spring 2005 31 Triggers Constraints state what must remain true DBMS decides when to check Triggers are instructions to perform at explicitly specified times Three aspects: An event (e.g., update to an attribute) A condition (e.g., a test of that update value) An action (the trigger’s effect) (deletion, update, insertion) When the event occurs, DBMS checks the constraint, and if it is satisfied, performs the action M.P. Johnson, DBMS, Stern/NYU, Spring 2005 32 DML trigger options The trigger may be: The trigger may run Statement-level (e.g., a DELETE WHERE statement) or Row-level (e.g., for each row deleted) BEFORE AFTER or INSTEAD OF the statement (in Oracle, not in others) It may be triggered by INSERTs UPDATEs DELETEs M.P. Johnson, DBMS, Stern/NYU, Spring 2005 33 Trigger form CREATE [OR REPLACE] TRIGGER trigger name {BEFORE | AFTER | INSTEAD OF} {INSERT | DELETE | UPDATE | UPDATE OF column list} ON table name [FOR EACH ROW] [WHEN (...)] [DECLARE ... ] BEGIN ... executable statements ... [EXCEPTION ... ] END [trigger name]; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 34 Skip to: Extended auditing example Trigger type examples First run copy_tables.sql 1. statement_vs_row.sql INSERT INTO to_table SELECT * FROM from_table; before_vs_after.sql 2. INSERT INTO to_table SELECT * FROM from_table; one_trigger_per_type.sql 3. INSERT INTO to_table VALUES (1); UPDATE to_table SET col1 = 10; DELETE to_table; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 35 DML Trigger e.g. CREATE OR REPLACE TRIGGER validate_employee_changes BEFORE INSERT OR UPDATE ON employee FOR EACH ROW BEGIN check_age (:NEW.date_of_birth); check_resume (:NEW.resume); END; Q: Why is this (maybe) better than client-side validation? M.P. Johnson, DBMS, Stern/NYU, Spring 2005 36 Triggers with WHEN CREATE OR REPLACE TRIGGER check_raise AFTER UPDATE OF salary, commission ON employee FOR EACH ROW WHEN ((OLD.salary != NEW.salary OR (OLD.salary IS NULL AND NEW.salary IS NULL)) OR (OLD.commission != NEW.commission OR (OLD.commission IS NULL AND NEW.commission IS NULL))) BEGIN ... END; NB: WHEN applies only to row-level triggers M.P. Johnson, DBMS, Stern/NYU, Spring 2005 37 Triggers with WHEN Remember: WHEN applies only to row-levels Parentheses are required Can only call built-in functions in when Packages like DBMS_OUTPUT are not allowed CREATE OR REPLACE TRIGGER valid_when_clause BEFORE INSERT ON frame FOR EACH ROW WHEN ( TO_CHAR(SYSDATE,'HH24') BETWEEN 9 AND 17 ) ... M.P. Johnson, DBMS, Stern/NYU, Spring 2005 38 Multiple DML actions DML actions may be ORed together To find actual action, check: INSERTING DELETING UPDATING CREATE OR REPLACE TRIGGER three_for_the_price_of_one BEFORE DELETE OR INSERT OR UPDATE ON account_transaction FOR EACH ROW BEGIN IF INSERTING THEN :NEW.created_by := USER; :NEW.created_date := SYSDATE; ELSIF DELETING THEN audit_deletion(USER,SYSDATE); 39 M.P. Johnson, DBMS, Stern/NYU, Spring 2005 END; More on UPDATING UPDATING may be called for partic. columns CREATE OR REPLACE TRIGGER validate_update BEFORE UPDATE ON account_transaction FOR EACH ROW BEGIN IF UPDATING ('ACCOUNT_NO') THEN errpkg.raise('Account number cannot be updated'); END IF; END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 40 Extended auditing example Tables: grades, grades_audit Run: grades_tables.sql, grades_audit.sql Cases: hacker changes grades, deletes others UPDATE grades SET grade = 'A+' WHERE student_id = 1 AND class_id = 101; DELETE grades WHERE student_id = 2 AND class_id = 101; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 41 Extended auditing example Run: grades_tables.sql, grades_audit2.sql Cases: hacker changes student or class ids UPDATE grades SET student_id = 3 WHERE student_id = 1 AND class_id = 101; UPDATE grades SET student_id = 1 WHERE student_id = 2 AND class_id = 101; UPDATE grades SET student_id = 2 WHERE student_id = 3 AND class_id = 101; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 42 DDL Triggers Respond to DDL events Creating/dropping tables, indices, etc. ALTER TABLE etc. General form: CREATE [OR REPLACE] TRIGGER trigger name {BEFORE | AFTER| {DDL event} ON {DATABASE | SCHEMA} DECLARE Variable declarations BEGIN ... some code... END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 43 DDL trigger e.g. Town crier examples triggered by creates: uninformed_town_crier.sql informed_town_crier.sql M.P. Johnson, DBMS, Stern/NYU, Spring 2005 44 Skip to: the bulk loader Available DDL events CREATE, ALTER, DROP, GRANT, RENAME, REVOKE, TRUNCATE DDL: any DDL event CREATE OR REPLACE TRIGGER no_create AFTER CREATE ON SCHEMA BEGIN RAISE_APPLICATION_ERROR (-20000, 'ERROR : Objects cannot be created in the production database.'); END; Q: Does this work?? M.P. Johnson, DBMS, Stern/NYU, Spring 2005 45 DB Event triggers Form similar to DDL triggers: CREATE [OR REPLACE] TRIGGER trigger name {BEFORE | AFTER} {database event} ON {DATABASE | SCHEMA} DECLARE Variable declarations BEGIN ... some code... END; Triggering events: STARTUP, SHUTDOWN, SERVERERROR, LOGON, LOGOFF M.P. Johnson, DBMS, Stern/NYU, Spring 2005 46 DB event restrictions Have BEFORE and AFTER as above, but they don’t always apply: No BEFORE STARTUP/LOGON/SERVERERROR No AFTER SHUTDOWN/LOGOFF M.P. Johnson, DBMS, Stern/NYU, Spring 2005 47 DB Trigger e.g. Gather stat before shutdown: CREATE OR REPLACE TRIGGER on_shutdown BEFORE SHUTDOWN ON DATABASE BEGIN gather_system_stats; END; Log error messages: error_log.sql M.P. Johnson, DBMS, Stern/NYU, Spring 2005 48 Trigger maintenance Enabling & disabling: Deleting: DROP TRIGGER emp_after_insert; Viewing: ALTER TRIGGER emp_after_insert DISABLE; ALTER TRIGGER emp_after_insert ENABLE; select trigger_name from user_triggers; select text from user_source where name='TOWN_CRIER'; Check validity: select object_name, status from user_objects where object_type='TRIGGER'; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 49 Triggers – important points Can replace old row (result of event) with new row Action may be performed before or after event Can refer to old row and new row WHEN clauses tests whether to continue Action may be performed either For each row involved in event Once per event Oracle does triggers as PL/SQL programs A trigger runs in the same transaction as the event triggering it M.P. Johnson, DBMS, Stern/NYU, Spring 2005 50 Elements of Triggers Timing of action execution: before, after or instead of triggering event The action can refer to both the old and new state of the database Update events may specify a particular column or set of columns A condition is specified with an optional WHEN clause The action can be performed either for once for every tuple or once for all the tuples that are changed by the database operation M.P. Johnson, DBMS, Stern/NYU, Spring 2005 51 Simple trigger example R(id, data, last-modified) Goal: whenever data is modified, update lastmodified date Could modify all scripts/programs that touch this table CREATE TRIGGER UpdateDateTrigger data is a large string Last-modified is a newly added date field Bad idea Better: user a trigger BEFORE UPDATE OF data ON R REFERENCING NEW ROW AS NewTuple FOR EACH STATEMENT BEGIN NewTuple.last-modified = sysdate; END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 52 Triggers: Row-level example MovieExec(name, address, cert#, netWorth) “If someone decreases a movie executive’s net worth, I want the database to reset itself to the previous net worth.” CREATE TRIGGER NetWorthTrigger AFTER UPDATE OF netWorth ON MovieExec REFERENCING NEW ROW AS NewTuple OLD ROW AS OldTuple FOR EACH ROW WHEN (OldTuple.netWorth>NewTuple.netWorth) UPDATE MovieExec SET netWorth = oldTuple.netWorth WHERE cert# = newTuple.cert#) M.P. Johnson, DBMS, Stern/NYU, Spring 2005 53 Triggers: Table-level example MovieExec(name, address, cert#, netWorth) “If someone updates the net worth of one movie exec so that the average net worth of all movie execs becomes less than $50,000, I want the database to reset itself.” CREATE TRIGGER AvgNetWorthTrigger AFTER UPDATE OF netWorth ON MovieExec REFERENCING OLD TABLE AS OldStuff, NEW TABLE AS NewStuff FOR EACH STATEMENT WHEN (50000 > (SELECT AVG(netWorth) FROM MovieExec)) BEGIN DELETE FROM MovieExec WHERE (Name, address, cert#, netWorth) IN NewStuff; INSERT INTO MovieExec (SELECT * FROM OldStuff); END; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 54 Mutating Table Errors It’s generally impossible to a row-level trigger to modify or query the table that triggered it trigger 2 slides back is not allowed! Does not apply to statement-level triggers trigger 1 slide back is Can do the equiv by creating a complicated statement-level trigger Won’t discuss… M.P. Johnson, DBMS, Stern/NYU, Spring 2005 55 New topic: the bulk loader To insert data, can insert rows one at a time with INSERT INTO <table> VALUES(<>) If data is in/can be computed from other tables, can use INSERT INTO <table> SELECT … Often, have text file of data MySQL’s bulk loader will parse file and insert all into the database M.P. Johnson, DBMS, Stern/NYU, Spring 2005 56 Using the bulk loader Use mysqlimport command Takes a text file as input Each line ~ 1 row Tab-delimited 100 101 Max Sydow Count Dracula M.P. Johnson, DBMS, Stern/NYU, Spring 2005 57 Running the bulk loader The bulk loader is a command-line program mysqlimport, separate from SQL*Plus: sales% mysqlimport -hmysql2 -umyNetID -p --local my-db imptest.txt At cmd line, specify: user/pass (pass is optional here) Host http://pages.stern.nyu.edu/~mjohnson/dbms/proj4.html Database Input file / table name M.P. Johnson, DBMS, Stern/NYU, Spring 2005 58 IMPORT DATA command Can also load data while inside mysql: mysql> LOAD DATA LOCAL INFILE 'imptest.txt' INTO TABLE imptest; Does not work without LOCAL Means: reading from client (your directory), not server M.P. Johnson, DBMS, Stern/NYU, Spring 2005 59 More info If you want, you can Use a different field separator Reorder the fields Import date values For more info, see the webpages: http://dev.mysql.com/doc/mysql/en/mysqlimport.ht ml http://dev.mysql.com/doc/mysql/en/load-data.html M.P. Johnson, DBMS, Stern/NYU, Spring 2005 60 New-old topic: Transactions So far, have simply issued commands Recall, though: an xact is an operation/set of ops executed atomically Ignored xacts In one instant ACID test: Xacts are atomic Each xact (not each statement) must leave the DB consistent M.P. Johnson, DBMS, Stern/NYU, Spring 2005 61 Default xact behavior An xact begins upon login By default, xact lasts until logoff Except for DDL statements They automatically commit Examples with two views of emp… M.P. Johnson, DBMS, Stern/NYU, Spring 2005 62 Direct xact instructions At any point, may explicitly COMMIT: Conversely, can ROLLBACK SQL> COMMIT; Saves all statements entered up to now Begins new xact SQL> ROLLBACK; Cancels all statements entered since start of xact Example: delete from emp; or delete junk; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 63 Direct xact instructions Remember, DDL statements are autocommitted They cannot be rollbacked Examples: drop table junk; rollback; truncate table junk; rollback; Q: Why doesn’t rollback “work”? M.P. Johnson, DBMS, Stern/NYU, Spring 2005 64 Savepoints Xacts are atomic Can rollback to beginning of current xact But might want to rollback only part way Make 10 changes, make one bad change Want to: roll back to before last change Don’t have Word-like multiple undo But do have savepoints M.P. Johnson, DBMS, Stern/NYU, Spring 2005 65 Savepoints Create a savepoint: SAVEPOINT savept_name; --changes SAVEPOINT sp1; emp example: --changes Can skip savepoints SAVEPOINT sp2; But can ROLLBACK --changes SAVEPOINT sp3 only backwards --changes Can ROLLBACK ROLLBACK TO sp2; only to last COMMIT ROLLBACK TO sp1; M.P. Johnson, DBMS, Stern/NYU, Spring 2005 66 AUTOCOMMIT Finally, can turn AUTOCOMMIT on: SQL> SET AUTOCOMMIT ON; Then each statement is auto-committed as its own xact Not just DDL statements M.P. Johnson, DBMS, Stern/NYU, Spring 2005 67