Database Administration ISQA 436 <ul><li>SQL Review, </li></ul><ul><li>Oracle Objects </li></ul><ul><li>and Data Types, </...
SQL <ul><li>Structured Query Language </li></ul><ul><li>Declaritive language vs. procedural </li></ul><ul><li>Three types ...
Data Manipulation Commands (DML) <ul><li>SELECT </li></ul><ul><ul><li>Retrieve data from the database </li></ul></ul><ul><...
SELECT <ul><li>SELECT,  columns , FROM and  table names  are mandatory.  The rest are optional. </li></ul><ul><li>SELECT *...
Joins <ul><li>Joins are queries that select from two or more tables </li></ul><ul><li>A table may be joined with itself in...
Join - Example <ul><li>SELECT first_name, last_name, department_name </li></ul><ul><li>FROM employees, departments </li></...
Join Example <ul><li>select a.first_name, a.last_name, b.last_name as manager </li></ul><ul><li>from employees a, employee...
Subqueries <ul><li>Subquery is a query within a query </li></ul><ul><li>Subqueries are necessary when a condition can’t be...
Correlated Subquery <ul><li>A  correlated  subquery joins the subquery ( inner  query) with the  outer  query. </li></ul><...
Subquery Exists Example <ul><li>Combine subquery with EXISTS to determine conditions of existence or non-existence </li></...
Inline View <ul><li>Oracle provides an optimization to the subquery </li></ul><ul><li>The query can be included in the FRO...
<ul><li>INSERT </li></ul><ul><li>INSERT INTO  table (col1, col2,…)  VALUES ( val1, val2, … ) </li></ul><ul><li>UPDATE </li...
INSERT <ul><li>Two styles: with or without column names: </li></ul><ul><li>Style 1 – insert values in order of columns </l...
UPDATE <ul><li>Modify data in a table </li></ul><ul><li>UPDATE locks rows during update </li></ul><ul><li>Without a WHERE ...
DELETE <ul><li>Remove entire rows from table </li></ul><ul><li>Again, without WHERE clause, deletes ALL rows! </li></ul><u...
Three options for deleting a table <ul><li>DELETE FROM emp; </li></ul><ul><ul><li>Safest:  Logs the delete transactions in...
Data Definition Language (DDL) <ul><li>CREATE </li></ul><ul><ul><li>TABLE </li></ul></ul><ul><ul><li>VIEW </li></ul></ul><...
CREATE TABLE <ul><li>Creates a  base table </li></ul><ul><li>CREATE TABLE  table_name </li></ul><ul><li>( col1_name col1_t...
Constraints <ul><li>Primary key </li></ul><ul><li>CONSTRAINT pk_stock PRIMARY KEY(stkcode) </li></ul><ul><li>Foreign key <...
Index <ul><li>An index is a sorted list of rows from a table </li></ul><ul><li>Only a subset of one or more columns is sto...
Views - virtual tables <ul><li>An imaginary table constructed by the DBMS when required – not a  base table. </li></ul><ul...
Create View example <ul><li>CREATE VIEW emp_sal AS </li></ul><ul><li>(SELECT first_name, last_name, salary FROM employees)...
ORACLE DATABASE OBJECTS <ul><li>Table </li></ul><ul><li>Index </li></ul><ul><li>View </li></ul><ul><li>Materialized View <...
Tables <ul><li>Tables contain  rows  of data with a  column  for each datum. </li></ul><ul><li>Each column is of a specifi...
DUAL Table <ul><li>ORACLE provides a special  dummy  table for using select on data not contained in a table, such as SYSD...
DUAL Table <ul><li>The DUAL table has just one row with one column, who’s value is ‘X’; </li></ul><ul><ul><li>SELECT * FRO...
SYSDATE Function <ul><li>The SYSDATE function returns the current date and time: </li></ul><ul><ul><li>SELECT SYSDATE FROM...
ROWNUM Column <ul><li>Every Oracle query has a  pseudo -column named “rownum” </li></ul><ul><li>rownum can be used to limi...
Materialized Views <ul><li>Oracle has an object called a  materialized view .  It is a view that actually contains data. <...
SYNONYMS <ul><li>Synonyms are aliases for objects, a name that can be referred to without the schema name. </li></ul><ul><...
Sequence <ul><li>A sequence generates a sequential integer value </li></ul><ul><li>Note – sequence numbers may be  skipped...
SCHEMA <ul><li>Generally: A data model diagram </li></ul><ul><li>Oracle: a collection of database objects belonging to a s...
Miscellaneous SQL notes <ul><li>In Oracle, SQL statements are terminated with a semi-colon ; </li></ul><ul><li>By conventi...
ORACLE DATATYPES Internal row address ROWID Pointer to an external file BFILE Large binary object, up to 4GB BLOB Large ch...
ORACLE DATATYPES - NUMBER <ul><li>NUMBER(p,s) </li></ul><ul><ul><li>precision is number of digits to store </li></ul></ul>...
NUMBER EXAMPLES Exceeds precision NUMBER(4,6) .001235 NUMBER(6,6) .00123456 NUMBER(6,8) .00123456 1234600 NUMBER(9, -2) Ex...
ORACLE DATE <ul><li>Dates and times are stored internally as a number in Oracle </li></ul><ul><li>To insert or select a da...
ORACLE DATE <ul><li>You can insert a date as a literal string </li></ul><ul><ul><li>Use date format DD-MON-YYYY (day-month...
ORACLE DATE <ul><li>DATE type includes date and time information. </li></ul><ul><li>To insert a date, use the TO_DATE func...
ORACLE DATE <ul><li>Likewise, when selecting a date from a table, you can display the date in different formats using the ...
DATE FORMAT ELEMENTS See Table 8-6 on page 224 of textbook for complete listing Seconds SS Military hour, i.e. 14:00 = 2pm...
DATE FORMAT ELEMENTS <ul><li>Examples </li></ul><ul><ul><li>SELECT TO_CHAR(SYSDATE, 'DAY MONTH DD, YYYY HH:MI:SS PM') AS &...
Oracle Date Functions <ul><li>TO_CHAR converts date to a character string </li></ul><ul><li>TO_DATE converts a character s...
<ul><li>insert into emp  </li></ul><ul><li>(first_name, last_name, email, job_id, hire_date) </li></ul><ul><li>values </li...
<ul><li>SELECT SYSDATE FROM DUAL; </li></ul><ul><li>SYSDATE </li></ul><ul><li>--------- </li></ul><ul><li>06-OCT-04 </li><...
PL/SQL Procedures <ul><li>PL/SQL stands for  procedural language . </li></ul><ul><li>A  procedure s is a group of SQL stat...
SQL*Plus <ul><li>Oracle’s command-line SQL interpreter </li></ul><ul><li>Two kinds of commands </li></ul><ul><ul><li>SQL c...
SQL*Plus Transactions <ul><li>By default SQL*Plus does not run in  autocommit  mode. </li></ul><ul><li>DELETE’s, UPDATE’s ...
SQL*Plus Commands - DESCRIBE <ul><li>DESCRIBE </li></ul><ul><ul><li>Lists the description of an Oracle object </li></ul></...
SQL*Plus Commands - DESCRIBE <ul><li>DESCRIBE employees </li></ul><ul><li>Name  Null?  Type </li></ul><ul><li>------------...
SQL*Plus Commands - spool <ul><li>spool opens a file and sends all output to the file </li></ul><ul><li>Filename cannot co...
SQL*Plus Commands - SET <ul><li>The SET command sets certain parameters affecting the output display </li></ul><ul><li>SET...
SQL*Plus Commands - SHOW <ul><li>The SHOW command displays parameter settings </li></ul><ul><li>SHOW SGA </li></ul><ul><ul...
SQL*Plus Commands - HELP <ul><li>HELP  topic  displays information about other commands </li></ul><ul><ul><li>help spool <...
Upcoming SlideShare
Loading in...5
×

web.pdx.edu

429

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
429
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

web.pdx.edu

  1. 1. Database Administration ISQA 436 <ul><li>SQL Review, </li></ul><ul><li>Oracle Objects </li></ul><ul><li>and Data Types, </li></ul><ul><li>SQL*Plus </li></ul>
  2. 2. SQL <ul><li>Structured Query Language </li></ul><ul><li>Declaritive language vs. procedural </li></ul><ul><li>Three types of SQL statements </li></ul><ul><ul><li>Data Manipulation Language (DML) </li></ul></ul><ul><ul><ul><li>SELECT, INSERT, UPDATE, DELETE </li></ul></ul></ul><ul><ul><li>Data Definition Language (DDL) </li></ul></ul><ul><ul><ul><li>CREATE/ALTER/DROP TABLE, VIEW, INDEX </li></ul></ul></ul><ul><ul><li>Data Control Language (DCL) </li></ul></ul><ul><ul><ul><li>GRANT, REVOKE </li></ul></ul></ul>
  3. 3. Data Manipulation Commands (DML) <ul><li>SELECT </li></ul><ul><ul><li>Retrieve data from the database </li></ul></ul><ul><li>INSERT </li></ul><ul><ul><li>Add new rows to the database </li></ul></ul><ul><li>UPDATE </li></ul><ul><ul><li>Modify data in the database </li></ul></ul><ul><li>DELETE </li></ul><ul><ul><li>Remove rows from the database </li></ul></ul>
  4. 4. SELECT <ul><li>SELECT, columns , FROM and table names are mandatory. The rest are optional. </li></ul><ul><li>SELECT * will select all columns. </li></ul><ul><li>SELECT col1, col2, … FROM table1, table2, … [ WHERE search_condition </li></ul><ul><li>AND search_condition </li></ul><ul><li>OR search_condition ] [ GROUP BY group_by_expression ] [ HAVING search_condition ] [ ORDER BY order_expression [ ASC | DESC ]] </li></ul>
  5. 5. Joins <ul><li>Joins are queries that select from two or more tables </li></ul><ul><li>A table may be joined with itself in a self-join </li></ul><ul><li>Tables are joined on one or more columns to combine similar rows from each </li></ul><ul><li>The join is done in the WHERE clause of the SELECT statement </li></ul>
  6. 6. Join - Example <ul><li>SELECT first_name, last_name, department_name </li></ul><ul><li>FROM employees, departments </li></ul><ul><li>WHERE employees.department_id = departments.department_id; </li></ul><ul><li>FIRST_NAME LAST_NAME DEPARTMENT_NAME </li></ul><ul><li>------------ ------------ --------------- </li></ul><ul><li>Nancy Greenberg Finance </li></ul><ul><li>Daniel Faviet Finance </li></ul><ul><li>John Chen Finance </li></ul><ul><li>Ismael Sciarra Finance </li></ul><ul><li>Jose Manuel Urman Finance </li></ul><ul><li>Luis Popp Finance </li></ul><ul><li>Shelley Higgins Accounting </li></ul><ul><li>William Gietz Accounting </li></ul>
  7. 7. Join Example <ul><li>select a.first_name, a.last_name, b.last_name as manager </li></ul><ul><li>from employees a, employees b </li></ul><ul><li>where a.manager_id = b.employee_id </li></ul><ul><li>order by b.last_name; </li></ul><ul><li>FIRST_NAME LAST_NAME MANAGER </li></ul><ul><li>------------ ------------ ------------------------- </li></ul><ul><li>William Smith Cambrault </li></ul><ul><li>Elizabeth Bates Cambrault </li></ul><ul><li>Sundita Kumar Cambrault </li></ul><ul><li>Alexander Hunold De Haan </li></ul><ul><li>Clara Vishney Errazuriz </li></ul><ul><li>Danielle Greene Errazuriz </li></ul>
  8. 8. Subqueries <ul><li>Subquery is a query within a query </li></ul><ul><li>Subqueries are necessary when a condition can’t be adequately defined in the WHERE clause </li></ul><ul><li>Example, find employee with the highest salary: </li></ul><ul><li>select first_name, last_name, salary </li></ul><ul><li>from employees </li></ul><ul><li>where salary = </li></ul><ul><li>(select max(salary) from employees); </li></ul>
  9. 9. Correlated Subquery <ul><li>A correlated subquery joins the subquery ( inner query) with the outer query. </li></ul><ul><li>Example, find employees making more than their department average: </li></ul><ul><li>SELECT first_name, last_name, salary </li></ul><ul><li>FROM employees a </li></ul><ul><li>WHERE a.salary > (select avg(salary) </li></ul><ul><li>FROM employees e, departments d </li></ul><ul><li>WHERE e.department_id = d.department_id </li></ul><ul><li>AND e.department_id = a.department_id ); </li></ul>
  10. 10. Subquery Exists Example <ul><li>Combine subquery with EXISTS to determine conditions of existence or non-existence </li></ul><ul><li>Find all products that have not sold: </li></ul><ul><li>SELECT product_id, product_name </li></ul><ul><li>FROM product_information p </li></ul><ul><li>WHERE NOT EXISTS (SELECT order_id </li></ul><ul><li>FROM order_items o </li></ul><ul><li>WHERE o.product_id = p.product_id); </li></ul>
  11. 11. Inline View <ul><li>Oracle provides an optimization to the subquery </li></ul><ul><li>The query can be included in the FROM clause as an inline view. </li></ul><ul><li>SELECT first_name, last_name, salary </li></ul><ul><li>FROM employees e, </li></ul><ul><li>(SELECT avg(salary) avg_sal, d.department_id </li></ul><ul><li>FROM departments d, employees e </li></ul><ul><li>where d.department_id = e.department_id </li></ul><ul><li>GROUP BY d.department_id) dept_avg </li></ul><ul><li>WHERE e.salary > dept_avg.avg_sal </li></ul><ul><li>AND e.department_id = dept_avg.department_id; </li></ul>
  12. 12. <ul><li>INSERT </li></ul><ul><li>INSERT INTO table (col1, col2,…) VALUES ( val1, val2, … ) </li></ul><ul><li>UPDATE </li></ul><ul><li>UPDATE table SET col1 = val1, col2 = val2, … </li></ul><ul><li>WHERE [ condition ] </li></ul><ul><li>DELETE </li></ul><ul><li>DELETE FROM table WHERE [ condition ] </li></ul>
  13. 13. INSERT <ul><li>Two styles: with or without column names: </li></ul><ul><li>Style 1 – insert values in order of columns </li></ul><ul><ul><li>as defined by CREATE TABLE – see object browser. </li></ul></ul><ul><ul><li>All columns must be included or NULL </li></ul></ul><ul><li>INSERT INTO item VALUES (21, 'Camoflage Pants', 'C', 'Khaki'); </li></ul><ul><li>Style 2 – indicate columns explicitly. </li></ul><ul><li>(Not all columns need appear, but must match up with values) </li></ul><ul><li>INSERT INTO item (itemno, itemname, itemtype) </li></ul><ul><li>VALUES (22, 'Hunting Vest', 'C'); </li></ul>
  14. 14. UPDATE <ul><li>Modify data in a table </li></ul><ul><li>UPDATE locks rows during update </li></ul><ul><li>Without a WHERE clause, updates ALL rows! </li></ul><ul><ul><li>Give all employees a 10% raise: </li></ul></ul><ul><li>UPDATE emp SET empsalary = 1.10*(empsalary); </li></ul><ul><ul><li>Move all employees in Personnel to Marketing: </li></ul></ul><ul><li>UPDATE emp SET deptname = 'Marketing' </li></ul><ul><li>WHERE deptname = 'Personnel'; </li></ul>
  15. 15. DELETE <ul><li>Remove entire rows from table </li></ul><ul><li>Again, without WHERE clause, deletes ALL rows! </li></ul><ul><li>DELETE FROM employees; </li></ul><ul><li>DELETE FROM employees </li></ul><ul><li>WHERE employee_id = 195; </li></ul>
  16. 16. Three options for deleting a table <ul><li>DELETE FROM emp; </li></ul><ul><ul><li>Safest: Logs the delete transactions in a log file for undo </li></ul></ul><ul><ul><li>Does not delete the table </li></ul></ul><ul><ul><li>Does not reclaim space </li></ul></ul><ul><li>TRUNCATE TABLE emp; </li></ul><ul><ul><li>Faster than delete, does not log transactions </li></ul></ul><ul><ul><li>Does not delete the table </li></ul></ul><ul><ul><li>Reclaims space </li></ul></ul><ul><li>DROP TABLE emp; </li></ul><ul><ul><li>Fast, does not log deletions </li></ul></ul><ul><ul><li>Deletes the table as well as the rows </li></ul></ul><ul><ul><li>Reclaims all space </li></ul></ul>
  17. 17. Data Definition Language (DDL) <ul><li>CREATE </li></ul><ul><ul><li>TABLE </li></ul></ul><ul><ul><li>VIEW </li></ul></ul><ul><ul><li>INDEX </li></ul></ul><ul><li>ALTER </li></ul><ul><ul><li>TABLE </li></ul></ul><ul><ul><li>VIEW </li></ul></ul><ul><ul><li>INDEX </li></ul></ul><ul><li>DROP </li></ul><ul><ul><li>TABLE </li></ul></ul><ul><ul><li>VIEW </li></ul></ul><ul><ul><li>INDEX </li></ul></ul>
  18. 18. CREATE TABLE <ul><li>Creates a base table </li></ul><ul><li>CREATE TABLE table_name </li></ul><ul><li>( col1_name col1_type DEFAULT constraints, </li></ul><ul><li>col2_name col2_type DEFAULT constraints, </li></ul><ul><li>… , </li></ul><ul><li>PRIMARY KEY ( pk_col1, pk_col2, … ), </li></ul><ul><li>FOREIGN KEY ( fk_col1, fk_col2, … ) </li></ul><ul><li>REFERENCES ref_table ( ref_col1, ref_col2,… ), </li></ul><ul><li>CHECK ( check conditions )); </li></ul>
  19. 19. Constraints <ul><li>Primary key </li></ul><ul><li>CONSTRAINT pk_stock PRIMARY KEY(stkcode) </li></ul><ul><li>Foreign key </li></ul><ul><li>CONSTRAINT fk_stock_nation </li></ul><ul><li>FOREIGN KEY(natcode) REFERENCES nation </li></ul><ul><li>Unique </li></ul><ul><li>CONSTRAINT unq_stock_stkname UNIQUE(stkname) </li></ul><ul><li>Check </li></ul><ul><li>CONSTRAINT item_color_cons </li></ul><ul><li>CHECK (itemcolor IN ('White', 'Brown', 'Khaki')) </li></ul>
  20. 20. Index <ul><li>An index is a sorted list of rows from a table </li></ul><ul><li>Only a subset of one or more columns is stored, along with the address of each row. </li></ul><ul><li>Data retrieval is much faster with an index. </li></ul><ul><li>Types of index </li></ul><ul><ul><li>B-tree (most common) </li></ul></ul><ul><ul><li>bitmap </li></ul></ul><ul><ul><li>reverse </li></ul></ul><ul><ul><li>hash </li></ul></ul><ul><ul><li>cluster </li></ul></ul>
  21. 21. Views - virtual tables <ul><li>An imaginary table constructed by the DBMS when required – not a base table. </li></ul><ul><li>Only the definition of the view is stored, not the result </li></ul><ul><li>Usually can’t be updated except in special circumstances </li></ul><ul><li>CREATE VIEW view_name </li></ul><ul><li>( col1, col2, … ) </li></ul><ul><li>AS select statement; </li></ul>
  22. 22. Create View example <ul><li>CREATE VIEW emp_sal AS </li></ul><ul><li>(SELECT first_name, last_name, salary FROM employees); </li></ul><ul><li>SELECT * FROM emp_sal; </li></ul><ul><li>FIRST_NAME LAST_NAME SALARY </li></ul><ul><li>-------------------- ------------------------- ---------- </li></ul><ul><li>Donald OConnell 2600 </li></ul><ul><li>Douglas Grant 2600 </li></ul><ul><li>Jennifer Whalen 4400 </li></ul><ul><li>Michael Hartstein 13000 </li></ul><ul><li>Pat Fay 6000 </li></ul><ul><li>Susan Mavris 6500 </li></ul><ul><li>Hermann Baer 10000 </li></ul>
  23. 23. ORACLE DATABASE OBJECTS <ul><li>Table </li></ul><ul><li>Index </li></ul><ul><li>View </li></ul><ul><li>Materialized View </li></ul><ul><li>Synonym </li></ul><ul><li>Sequence </li></ul><ul><li>Procedure </li></ul><ul><li>Function </li></ul><ul><li>Package </li></ul><ul><li>Trigger </li></ul>
  24. 24. Tables <ul><li>Tables contain rows of data with a column for each datum. </li></ul><ul><li>Each column is of a specific data type </li></ul><ul><li>Columns without data are NULL </li></ul><ul><li>Each table has a primary key – a column that has unique values that identify rows </li></ul><ul><li>Columns may have foreign key references to a column in another table. The value in the foreign table must exist and be identical. </li></ul>
  25. 25. DUAL Table <ul><li>ORACLE provides a special dummy table for using select on data not contained in a table, such as SYSDATE </li></ul><ul><ul><li>SELECT SYSDATE FROM DUAL; </li></ul></ul><ul><ul><li>SYSDATE </li></ul></ul><ul><ul><li>--------- </li></ul></ul><ul><ul><li>08-FEB-04 </li></ul></ul><ul><ul><li>SELECT 'This is a test' AS message FROM DUAL; </li></ul></ul><ul><ul><li>MESSAGE </li></ul></ul><ul><ul><li>-------------- </li></ul></ul><ul><ul><li>This is a test </li></ul></ul>
  26. 26. DUAL Table <ul><li>The DUAL table has just one row with one column, who’s value is ‘X’; </li></ul><ul><ul><li>SELECT * FROM DUAL; </li></ul></ul><ul><ul><li>D </li></ul></ul><ul><ul><li>- </li></ul></ul><ul><ul><li>X </li></ul></ul>
  27. 27. SYSDATE Function <ul><li>The SYSDATE function returns the current date and time: </li></ul><ul><ul><li>SELECT SYSDATE FROM DUAL; </li></ul></ul><ul><ul><li>SYSDATE </li></ul></ul><ul><ul><li>--------- </li></ul></ul><ul><ul><li>08-FEB-04 </li></ul></ul>
  28. 28. ROWNUM Column <ul><li>Every Oracle query has a pseudo -column named “rownum” </li></ul><ul><li>rownum can be used to limit output to a certain number of rows </li></ul><ul><li>SELECT * FROM customers </li></ul><ul><li>WHERE rownum < 5; </li></ul><ul><li>CUST_ID CUST_FIRST_NAME CUST_LAST_NAME </li></ul><ul><li>---------- -------------------- -------------------- </li></ul><ul><li>10 Abigail Kessel </li></ul><ul><li>20 Abner Everett </li></ul><ul><li>30 Abraham Odenwalld </li></ul><ul><li>40 Absolom Sampson </li></ul><ul><li>4 rows selected. </li></ul>
  29. 29. Materialized Views <ul><li>Oracle has an object called a materialized view . It is a view that actually contains data. </li></ul><ul><li>Data is updated periodically </li></ul><ul><li>Useful for derived data , such as statistical data </li></ul>
  30. 30. SYNONYMS <ul><li>Synonyms are aliases for objects, a name that can be referred to without the schema name. </li></ul><ul><li>Synonyms are public or private </li></ul><ul><ul><li>public = all users can use it </li></ul></ul><ul><ul><li>private = only the owner can use it </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>CREATE PUBLIC SYNONYM employees FOR employees; </li></ul></ul><ul><ul><li>Now anyone can select from the employees table </li></ul></ul><ul><ul><li>SELECT * FROM employees; </li></ul></ul>
  31. 31. Sequence <ul><li>A sequence generates a sequential integer value </li></ul><ul><li>Note – sequence numbers may be skipped. </li></ul><ul><li>Useful for serial numbers </li></ul><ul><ul><li>CREATE SEQUENCE customers_seq START WITH 1000 INCREMENT BY 1 ; </li></ul></ul><ul><li>Use nextval to get the next incremental number </li></ul><ul><ul><li>INSERT INTO employees VALUES (employees_seq.nextval, 'John', 'Doe', 'jdoe', '555-1212', TO_DATE(SYSDATE), 'PU_CLERK', 2500, null, null, 30); </li></ul></ul>
  32. 32. SCHEMA <ul><li>Generally: A data model diagram </li></ul><ul><li>Oracle: a collection of database objects belonging to a single user </li></ul><ul><li>The schema has the name of the owner </li></ul><ul><li>Normally, only the owner has access to the schema </li></ul><ul><li>Other users can access using the user name prefix if they have been granted privileges: </li></ul><ul><ul><li>select * from hr.employees; </li></ul></ul>
  33. 33. Miscellaneous SQL notes <ul><li>In Oracle, SQL statements are terminated with a semi-colon ; </li></ul><ul><li>By convention, uppercase SQL reserved words and lowercase data-specific words ( not mandatory ): </li></ul><ul><ul><li>SELECT last_name FROM employees </li></ul></ul><ul><ul><li>WHERE employee_id = 197; </li></ul></ul><ul><li>Object names are not case-sensitive, but string literals are: </li></ul><ul><ul><li>SELECT employee_id FROM EMPLOYEES </li></ul></ul><ul><ul><li>WHERE last_name = ‘Smith’; </li></ul></ul>
  34. 34. ORACLE DATATYPES Internal row address ROWID Pointer to an external file BFILE Large binary object, up to 4GB BLOB Large character string, up to 4GB CLOB A date and time DATE Floating point number with precision = p and scale = s NUMBER(p,s) Variable-length character string VARCHAR2(size) Fixed-length character string CHAR(size)
  35. 35. ORACLE DATATYPES - NUMBER <ul><li>NUMBER(p,s) </li></ul><ul><ul><li>precision is number of digits to store </li></ul></ul><ul><ul><li>scale is number of digits to the right of the decimal place </li></ul></ul><ul><ul><li>p is an integer between 1 and 38 (inclusive) </li></ul></ul><ul><ul><li>s is an integer between -84 and 127 (inclusive) </li></ul></ul><ul><ul><li>s = 0 is a decimal integer </li></ul></ul><ul><ul><li>negative s pads zeroes to the left of the decimal point </li></ul></ul><ul><ul><li>specifying neither p nor s assumes maximum values </li></ul></ul>
  36. 36. NUMBER EXAMPLES Exceeds precision NUMBER(4,6) .001235 NUMBER(6,6) .00123456 NUMBER(6,8) .00123456 1234600 NUMBER(9, -2) Exceeds precision NUMBER(6,2) 1234567.9 NUMBER(9,1) 1234567.89 NUMBER(9,2) 1234568 NUMBER(9) 1234567.89 NUMBER 1234567.89 Stored as Specified as Actual Data
  37. 37. ORACLE DATE <ul><li>Dates and times are stored internally as a number in Oracle </li></ul><ul><li>To insert or select a date or time in a way that is readable, it has to be converted to or from a string using a date format . </li></ul><ul><ul><li>Example: 09-FEB-2004 is in date format DD-MON-YYYY </li></ul></ul>
  38. 38. ORACLE DATE <ul><li>You can insert a date as a literal string </li></ul><ul><ul><li>Use date format DD-MON-YYYY (day-month-year) </li></ul></ul><ul><ul><li>Can’t insert the time this way </li></ul></ul><ul><ul><li>Time will be 12:00:00 am </li></ul></ul><ul><ul><li>INSERT INTO employees (employee_id, first_name, last_name, email, hire_date, job_id) </li></ul></ul><ul><ul><li>VALUES (208, 'Brown', 'John', 'jbrown@yahoo.com', '28-JAN-2004', 'SA_REP'); </li></ul></ul>
  39. 39. ORACLE DATE <ul><li>DATE type includes date and time information. </li></ul><ul><li>To insert a date, use the TO_DATE function and specify the date, time and date format as strings: </li></ul><ul><ul><li>TO_DATE(’08-FEB-2004’, ‘DD-MON-YYYY’) </li></ul></ul><ul><ul><li>TO_DATE(’08-FEB-2004 13:44:00’, ‘DD-MON-YYYY HH24:MI:SS’) </li></ul></ul><ul><ul><li>insert into employees (employee_id, first_name, last_name, email, hire_date, job_id) </li></ul></ul><ul><ul><li>values (207, 'Brown', 'John', 'jb@yahoo.com', TO_DATE('27-JAN-2004', 'DD-MON-YYYY'), 'SA_REP'); </li></ul></ul>
  40. 40. ORACLE DATE <ul><li>Likewise, when selecting a date from a table, you can display the date in different formats using the TO_CHAR function. </li></ul><ul><ul><li>select TO_CHAR(SYSDATE, 'DD/MM/YY HH:MI:SS PM') as &quot;current time&quot; FROM DUAL </li></ul></ul><ul><ul><li>current time </li></ul></ul><ul><ul><li>-------------------- </li></ul></ul><ul><ul><li>08/02/04 02:37:05 PM </li></ul></ul>
  41. 41. DATE FORMAT ELEMENTS See Table 8-6 on page 224 of textbook for complete listing Seconds SS Military hour, i.e. 14:00 = 2pm HH24 Hour, 11 am and 11 pm are the same HH Minutes MI Show PM or AM for HH PM Two-digit year in 20 th century, i.e. 98 = 1998 RR Four-digit year – safest! YYYY Two-digit year – beware: 98 = 2098 YY Month spelled out, i.e. January MONTH Three-character month, i.e. JAN MON Two-digit month MM Day of week spelled out DAY Two-digit day DD Meaning Element
  42. 42. DATE FORMAT ELEMENTS <ul><li>Examples </li></ul><ul><ul><li>SELECT TO_CHAR(SYSDATE, 'DAY MONTH DD, YYYY HH:MI:SS PM') AS &quot;current time&quot; FROM DUAL; </li></ul></ul><ul><ul><li>current time </li></ul></ul><ul><ul><li>---------------------------------------- </li></ul></ul><ul><ul><li>SUNDAY FEBRUARY 08, 2004 04:38:05 PM </li></ul></ul><ul><ul><li>SELECT TO_CHAR(SYSDATE, 'MM+DD+RR HH24:MI:SS') AS &quot;current time&quot; FROM DUAL; </li></ul></ul><ul><ul><li>current time </li></ul></ul><ul><ul><li>----------------- </li></ul></ul><ul><ul><li>02+08+04 16:40:16 </li></ul></ul>
  43. 43. Oracle Date Functions <ul><li>TO_CHAR converts date to a character string </li></ul><ul><li>TO_DATE converts a character string to a date </li></ul><ul><li>TRUNC returns date with no time (time is set to 00:00:00. Use this when you want to compare the date and not the time </li></ul><ul><li>ADD_MONTHS, returns date incremented by a specified number of monts </li></ul><ul><li>Addition: adding integers to a date adds that many days to the date </li></ul><ul><li>See Table 8-5 on page 220 for complete list </li></ul>
  44. 44. <ul><li>insert into emp </li></ul><ul><li>(first_name, last_name, email, job_id, hire_date) </li></ul><ul><li>values </li></ul><ul><li>('Mark', 'Freeman', 'markf@pdx.edu', 50, '06-OCT-2004'); </li></ul><ul><li>1 row created. </li></ul><ul><li>SELECT * FROM emp WHERE hire_date = SYSDATE; </li></ul><ul><li>no rows selected </li></ul><ul><li>SELECT * FROM emp WHERE hire_date = trunc(SYSDATE); </li></ul><ul><li>FIRST_NAME LAST_NAME… </li></ul><ul><li>-------------------- ------------------------- </li></ul><ul><li>Mark Freeman… </li></ul><ul><li>1 row selected. </li></ul>TRUNC example
  45. 45. <ul><li>SELECT SYSDATE FROM DUAL; </li></ul><ul><li>SYSDATE </li></ul><ul><li>--------- </li></ul><ul><li>06-OCT-04 </li></ul><ul><li>SELECT ADD_MONTHS(SYSDATE, 3) FROM DUAL; </li></ul><ul><li>ADD_MONTH </li></ul><ul><li>--------- </li></ul><ul><li>06-JAN-05 </li></ul><ul><li>SELECT SYSDATE + 7 AS &quot;Next Week&quot; FROM DUAL; </li></ul><ul><li>Next Week </li></ul><ul><li>--------- </li></ul><ul><li>13-OCT-04 </li></ul>Date Arithmetic Examples
  46. 46. PL/SQL Procedures <ul><li>PL/SQL stands for procedural language . </li></ul><ul><li>A procedure s is a group of SQL statements with programming logic </li></ul><ul><li>A function is a procedure that returns a value </li></ul><ul><li>A trigger is a procedure that fires when a database event occurs, such as an insert or update </li></ul><ul><li>A package is a named group of procedures and functions </li></ul>
  47. 47. SQL*Plus <ul><li>Oracle’s command-line SQL interpreter </li></ul><ul><li>Two kinds of commands </li></ul><ul><ul><li>SQL commands </li></ul></ul><ul><ul><li>SQL*Plus commands </li></ul></ul><ul><li>SQL*Plus commands are special formatting and control commands that are not part of SQL </li></ul><ul><li>SQL*Plus commands are not case-sensitive and can be abbreviated </li></ul>
  48. 48. SQL*Plus Transactions <ul><li>By default SQL*Plus does not run in autocommit mode. </li></ul><ul><li>DELETE’s, UPDATE’s and INSERT’s do not become permanent until a COMMIT statement is explicitely executed </li></ul><ul><li>This means you can use ROLLBACK to undo changes </li></ul><ul><li>If you end a session with out committing, Oracle will automatically rollback your changes </li></ul><ul><li>Autocommit can be enaabled with the SET AUTOCOMMIT ON comand </li></ul>
  49. 49. SQL*Plus Commands - DESCRIBE <ul><li>DESCRIBE </li></ul><ul><ul><li>Lists the description of an Oracle object </li></ul></ul><ul><ul><li>Can be abbreviated as ‘DESC’ </li></ul></ul><ul><li>desc move_ccuser </li></ul><ul><li>PROCEDURE move_ccuser </li></ul><ul><li>Argument Name Type In/Out Default? </li></ul><ul><li>-------------- ----------------------- ------ -------- </li></ul><ul><li>CCUSER VARCHAR2 IN </li></ul><ul><li>TSPACE VARCHAR2 IN </li></ul>
  50. 50. SQL*Plus Commands - DESCRIBE <ul><li>DESCRIBE employees </li></ul><ul><li>Name Null? Type </li></ul><ul><li>---------------- -------- ------------- </li></ul><ul><li>EMPLOYEE_ID NOT NULL NUMBER(6) </li></ul><ul><li>FIRST_NAME VARCHAR2(20) </li></ul><ul><li>LAST_NAME NOT NULL VARCHAR2(25) </li></ul><ul><li>EMAIL NOT NULL VARCHAR2(25) </li></ul><ul><li>PHONE_NUMBER VARCHAR2(20) </li></ul><ul><li>HIRE_DATE NOT NULL DATE </li></ul><ul><li>JOB_ID NOT NULL VARCHAR2(10) </li></ul><ul><li>SALARY NUMBER(8,2) </li></ul><ul><li>COMMISSION_PCT NUMBER(2,2) </li></ul><ul><li>MANAGER_ID NUMBER(6) </li></ul><ul><li>DEPARTMENT_ID NUMBER(4) </li></ul>
  51. 51. SQL*Plus Commands - spool <ul><li>spool opens a file and sends all output to the file </li></ul><ul><li>Filename cannot contain spaces </li></ul><ul><ul><li>spool h: est.out </li></ul></ul><ul><li>Turn off with the spool off command </li></ul>
  52. 52. SQL*Plus Commands - SET <ul><li>The SET command sets certain parameters affecting the output display </li></ul><ul><li>SET ECHO [ON/OFF] </li></ul><ul><ul><li>Controls display of SQL commands in reports </li></ul></ul><ul><li>SET HEADING [ON/OFF] </li></ul><ul><ul><li>Control display of column headings </li></ul></ul><ul><li>SET LINESIZE int </li></ul><ul><ul><li>Width of a line before wrapping </li></ul></ul><ul><li>SET PAGESIZE int </li></ul><ul><ul><li>Number of lines to print between headers </li></ul></ul><ul><ul><li>Set to 0 turns off all heading and page breaks </li></ul></ul><ul><li>SET UNDERLINE char </li></ul><ul><ul><li>Sets the underline character at column headings to char </li></ul></ul>
  53. 53. SQL*Plus Commands - SHOW <ul><li>The SHOW command displays parameter settings </li></ul><ul><li>SHOW SGA </li></ul><ul><ul><li>Shows memory configuration </li></ul></ul><ul><li>SHOW PARAMETER parameter name </li></ul><ul><ul><li>Shows setting of an init.ora parameter </li></ul></ul><ul><li>SHOW ERROR </li></ul><ul><ul><li>Displays PL/SQL error messages </li></ul></ul><ul><li>SHOW USER </li></ul><ul><li>SHOW SPOOL </li></ul><ul><li>SHOW ALL </li></ul>
  54. 54. SQL*Plus Commands - HELP <ul><li>HELP topic displays information about other commands </li></ul><ul><ul><li>help spool </li></ul></ul><ul><ul><li>SPOOL </li></ul></ul><ul><ul><li>----- </li></ul></ul><ul><ul><li>Stores query results in an operating system file, or sends the </li></ul></ul><ul><ul><li>file to a printer. </li></ul></ul><ul><ul><li>In iSQL*Plus, output can be directed to a file. </li></ul></ul><ul><ul><li>SPO[OL] [file_name[.ext] | OFF | OUT] </li></ul></ul><ul><ul><li>Not available in iSQL*Plus </li></ul></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×