1 / 96

DB Programming

DB Programming. Database Systems, 2008-2009 Presented by Rubi Boim. Project Details Basic Oracle Usage Little More Complex Oracle stuff.. JDBC Coding Tips. Agenda. Database project – TV/Movies DB. Examples: IMDb !. Database project II.

yana
Download Presentation

DB Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DB Programming Database Systems, 2008-2009 Presented by Rubi Boim

  2. Project Details Basic Oracle Usage Little More Complex Oracle stuff.. JDBC Coding Tips Agenda

  3. Database project – TV/Movies DB Examples: IMDb!

  4. Database project II • Project goal: to tackle and resolve real-life DB related development issues • So what do we need to do: • Design database • Load data • Think of an application • Build application • Test

  5. Database project III • What to focus on: • Database • Data Populating • Usability • Ideas that will give you an edge over the competition

  6. Database project - Features • Think your self! Any idea is acceptable • Some trivial ideas: • Search for movies / actors (daaaaa..) • Add / Edit / Remove data manually (not just massive import) • Security (who can view / edit / remove) • Recommendations (how?)..(btw, netfilx prize is $1,000,000 ) • Facebook?

  7. Database project IV • Hard work, but real. • Work in groups of 4 • One stage • Submission database is Oracle in TAU • Thinking out of the box will be rewarded

  8. Database project V • So where to get the data? • IMDb! • http://www.imdb.com/interfaces#plain • Textfile to Oracle… Not trivial • You can find other sources (min 1M records)

  9. Database project – Note on IMDbtextfiles • It is not trivial to deal with large text files… • Understand first what each file represents • You don’t have to use all of them..(do you even know what laserdisc is??) • You will need to generate IDs for everything!

  10. Database project – Working from TAU • Quota issues.. • local copy is available from unix by:cd /users/courses/databases/imdb • Also available by the websitehttp://www.cs.tau.ac.il/courses/databases/imdb/

  11. Database project – Windows Tip • Don’t try to view large text files with Notepad.. • Use TextPad (google it..) or similar..

  12. Project Details Basic Oracle Usage Little More Complex Oracle stuff.. JDBC Coding Tips Agenda

  13. There are 3 main groups of types: • Character • Numeric • Date • http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/datatype.htm Oracle Data Types

  14. Character set is established when you create the database (UTF-8,ANSI..). So ignore..  Char: Fixed length! Short value is padded Varchar2: variable-length Both types needs to define max length(4000 max. for more use BLOB) Oracle Data Types – Character

  15. Implemented by the “Number” data type A Number has two properties:- precision: Total number of digits- scale: Number of digits after the point(up to 38 digits) For floating point (if you need..): BINARY_FLOAT, BINARY_DOUBLE Oracle Data Types – Numeric

  16. Number Example Oracle Data Types – Numeric

  17. Implemented by the “Date” data type Stores “dates” and “times” Default format is DD-MON-YY Use the TO_DATE function for any other formatTO_DATE('13-AUG-66 12:56 A.M.','DD-MON-YY HH:MI A.M.') Oracle Data Types – Date

  18. Don’t forget to define the primary key on the other table.. What happens when you delete the “key record” from the “primary table”? - Restrict - Cascade - Set null Define Foreign keys

  19. Define Foreign keys

  20. Demo.. - create table (data types) - define primary key - define foreign keys (insert / delete data) Basic oracle usage - Demo

  21. Project Details Basic Oracle Usage Little More Complex Oracle stuff.. JDBC Coding Tips Agenda

  22. Index improves the speed of operations on a table Can be created using one or more fields You will learn more with Tova.. But don’t forget, its important Index

  23. Index - HowTo

  24. How do you know how to assign an ID?? “AutoNumber”

  25. Lock table new_id = 1 + select max id from table insert into table values(new_id, ”Rubi”); Unlock table “AutoNumber” – Algorithm?

  26. Sequence - an object from which multiple users may generate unique integers NEXTVAL() - incrementsthe sequence and returnsthe new value. Sequence

  27. If we defined the sequence as TEST_SEQ INSERT INTO test values(TEST_SEQ.NEXTVAL, 'rubi') Usually, sequence is defined astable_name +”_SEQ” Sequence – Insert example

  28. Why are we complicating things?? Do all DBMS support sequences? If we change a sequence name, we need to update all our queries Can we separate it from the query?INSERT INTO test(name) values('rubi') Sequence – Can we do better?

  29. A database trigger is procedural code that is automatically executed in response to certain events on a particular table Events: BEFORE INSERT AFTER INSERT BEFORE UPDATE AFTER UPDATE BEFORE DELETE AFTER DELETE Triggers

  30. Occurs only once per Insert/Update/Delete CREATE OR REPLACE TRIGGER <trigger_name> <BEFORE | AFTER> <ACTION> ON <table_name> BEGIN  <trigger_code> END; Triggers – Statement Level

  31. Occurs for each row CREATE OR REPLACE TRIGGER <trigger_name> <BEFORE | AFTER> <ACTION> ON <table_name> FOR EACH ROW BEGIN  <trigger_code> END; Triggers – Row Level

  32. You can not “just use the GUI” - you need to “code” the trigger” • After you press “ok” you can edit the code Triggers – Row Level – Example

  33. Use “NEW” to refer to the row • dual – simply a scratch-padselect max(12,54,2,75,142) from dual CREATE bi_test BEFORE INSERT ON test FOR EACH ROW BEGIN SELECT TEST_SEQ.NEXTVAL INTO :NEW.id FROM dual; END; Triggers – Row Level – Example

  34. Demo.. - Create index - Create “Autonumber”: - Create Sequence - Create Trigger “Complex” Oracle Stuff

  35. What if your query returns 1,000,000 results? How to return the TOP n results How to return the results from n to m Limit the Results

  36. Works only on Oracle..(mysql has “Limit”, sql-server has “Top”) ROWNUM is a pseudocolumn (not “real”) Each row is assigned with a number, starting with 1 We can select just the ones we want.. Oracle’s Rownum

  37. Its assigned BEFORE sorting or aggregation ROWNUM value is incremented only after it is assigned Read the previous lines 5 more times! Oracle’s Rownum – NOT THAT SIMPLE!

  38. SELECT * FROM students WHERE ROWNUM > 1 What NOT to do…  Oracle’s Rownum – Example 1

  39. SELECT * FROM students WHERE ROWNUM < 10 ORDER BY students.name What NOT to do…  Oracle’s Rownum – Example 2

  40. SELECT * FROM ( SELECT * FROM students ORDER BY students.name ) WHERE ROWNUM < 10 This will work… Oracle’s Rownum – Example 3

  41. SELECT * FROM ( SELECT * FROM students ORDER BY students.name ) WHERE ROWNUM >= 10 AND ROWNUM < 20 What NOT to do…  Oracle’s Rownum – Example 4

  42. SELECT * FROM ( SELECT a.*, ROWNUM rnum FROM ( SELECT * FROM students ORDER BY students.name ) a ) WHERE rnum >= 10 AND rnum < 20 Will work but we can do better (y)…  Oracle’s Rownum – Example 5

  43. SELECT * FROM ( SELECT a.*, ROWNUM rnum FROM ( SELECT * FROM students ORDER BY students.name ) a WHERE ROWNUM < 20 ) WHERE rnum >= 10 That’s the way…  Oracle’s Rownum – Example 6

  44. There is a big difference between > and < If you are using “example 6”, be sure the order by is unique (y?) btw, in MySQL its simply:select * from students order by name limit 10,20 Oracle’s Rownum – Final slide 

  45. Demo.. - create Sequence - create Trigger (for autonumber) - limiting the results Little More Complex Oracle Stuff - Demo

  46. Project Details Basic Oracle Usage Little More Complex Oracle stuff.. JDBC Coding Tips Agenda

  47. During the last episode… Application DB infrastructure DB driver transport DB engine Storage

  48. Concepts vs APIs Concepts APIs/Language Connection Connection pooling Error Handling Fetching results Rowset Prepared statements Batch processing ODBC JDBC OCI/OCCI ADO.NET X

  49. ODBC – Open Database Connectivity API • Pros: • Cross platform and cross databases • Easy to use • Cons: • Too low level • We wont use it.. But its very very common

  50. JDBC is a standard interface for connecting to relational databases from Java JDBC

More Related