Oracle SQL introduction. ½ç”¨è²...

download Oracle SQL introduction. ½ç”¨è² è«‹°é‡™…§è²ç”¢¬ œ¬è³‡–™ƒ…é™é«é›„‡‰ç”¨ç§‘€¤§­¸è³‡ç®ç³»•™­¸½ç”¨

If you can't read please download the document

  • date post

    15-Dec-2015
  • Category

    Documents

  • view

    235
  • download

    4

Embed Size (px)

Transcript of Oracle SQL introduction. ½ç”¨è²...

  • Slide 1

Oracle SQL introduction Slide 2 Slide 3 Agenda Database Structure Overview Oracle SQL* Net (Networking) SQL Data Retrieval Data Manipulation Language (DML) Data Definition Language (DDL) Transaction Control Database Objects Table, Constraint, Sequence, View, Index Slide 4 DAY 1 Slide 5 Terminology Instance (SGA + background process) Database (control file + data file + redo files) Tablespace Schema Object Table,Index,Sequence,View,Package,Procedure,Function,C lusteretc Slide 6 Database Structure Overview Control File Redo log Files Data Files Parameter File File Archived Log File Instance Database SGA SharedPool LibraryCache Data Dict. Cache DatabaseBufferCache Redo log Buffer SMON DBW0 PMON CKPT LGWR ARC0 UserProcess ServerProcess Slide 7 Stages in Startup and ShutdownOPEN MOUNT NOMOUNT SHUTDOWN STARTUP SHUTDOWN Slide 8 How to determine Database up or down ? (Windows) Slide 9 How to determine Database up or down ? (UNIX) Slide 10 Database & Tablespace Slide 11 Tablespace & Database object Slide 12 Oracle E-Business Suite Tablespace Design SYSTEM TEMP RBS FNDD FNDX GLD GLX OED OEX Slide 13 Establishing Connections to Oracle Servers Slide 14 How to determine Listener up or down?(Windows) Slide 15 How to determine Listener up or down?(Unix) Slide 16 Starting and using the Net Configuration Assistant Slide 17 tnsnames.ora SEMPROD = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.64.32.53 )(PORT = 1520))) (CONNECT_DATA = (SERVICE_NAME = PROD)) ) SEMUAT2 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.64.32.54 )(PORT = 1529))) (CONNECT_DATA = (SERVICE_NAME = UAT2)) ) Slide 18 SQL*Plus Slide 19 Logging into Database Oracle 9i Application Development SQL Plus Slide 20 Logging into Database Slide 21 SQL*Plus Command Summary (I) @ & get edit execute host list / Slide 22 SQL*Plus Command Summary (II) spool connect describe column set show all exit help Slide 23 3rd Party Tools TOAD (Quest Software) SQL Navigator (Quest Software) Slide 24 Exercise 1 Installation Oracle 9i Database Create Database Connect to Database by SQL* Net (using SQL* Plus) show user Slide 25 Sample Schema Diagrams Slide 26 SQL Statement Data Retrieval Select Data Manipulation Language (DML) Insert,Update,Delete Data Definition Language (DDL) Create,alter,drop,rename,truncate Transaction Control Commit,rollback,savepoint Data Control Language (DCL) Grant,revoke Slide 27 SQL Slide 28 Displaying Table Structure Slide 29 Basic SELECT Statement SELECT column [alias] FROM table [table alias] WHERE column = xxx SELECT column [alias] FROM table [table alias] WHERE column = xxx Not Case Sensitive Can be one or more lines Keywords can not be abbreviated or split across lines Slide 30 Selecting All Columns, All Rows Slide 31 Selecting Specific Columns Slide 32 Arithmetic Expressions Slide 33 Concatenation Operator Slide 34 Managing Null Values NULL is a value that is unavailable,unassigned,unknown,or inapplicable NULL is not the same as zero or space Arithmetic expressions containing a null value evaluate to NULL Slide 35 Conditions containing NULL Slide 36 Conditions containing NULL (example) Slide 37 NVL function Converts null to an actual value Datatype that can be used are date,character,and number Datatype must match NVL(comm,0) NVL(hiredate,01-JAN-97) NVL(job,Not Job Yet) Slide 38 NVL Function (example) Slide 39 DECODE Function Facilitates conditional inqueries by doing the work of a CASE or IF-THEN-ELSE statement DECODE (col/express, search1, result1 [,search2,result2,] [, default ]) DECODE (col/express, search1, result1 [,search2,result2,] [, default ]) Slide 40 Using the DECODE Function (example) Slide 41 Built-in Function Quick Ref. Slide 42 Preventing the Selection of Duplicate Rows The default display of queries is all rows including duplicate rows Eliminate duplicate rows by using DISTINCT in the SELECT clause SQL>SELECT JOB_ID 2 FROM EMPLOYEES; SQL>SELECT JOB_ID 2 FROM EMPLOYEES; SQL>SELECT DISTINCT JOB_ID 2 FROM EMPLOYEES; SQL>SELECT DISTINCT JOB_ID 2 FROM EMPLOYEES; Slide 43 Preventing the Selection of Duplicate Rows(example) SQL>SELECT JOB_ID 2FROM EMPLOYEES; JOB_ID -------------------- SA_MAN SA_REP SH_CLERK SQL>SELECT JOB_ID 2FROM EMPLOYEES; JOB_ID -------------------- SA_MAN SA_REP SH_CLERK Display All Rows SQL>SELECT DISTINCT JOB_ID 2FROM EMPLOYEES; JOB_ID -------------------- AC_ACCOUNT AC_MGR AD_ASST AD_PRES AD_VP FI_ACCOUNT FI_MGR HR_REP SQL>SELECT DISTINCT JOB_ID 2FROM EMPLOYEES; JOB_ID -------------------- AC_ACCOUNT AC_MGR AD_ASST AD_PRES AD_VP FI_ACCOUNT FI_MGR HR_REP Display Unique Rows Duplicate Rows Slide 44 Limiting Selected Rows Slide 45 Comparison and Logical Operations Logical comparison operators = > >= < Negating Expressions Logical Operators !=^= SQL Operator NOT BETWEEN... AND... NOT IN NOT LIKE IS NOT NULL Slide 48 The ORDER BY Clause Slide 49 Exercise 2 Installing the Human Resources (HR) Schema 10000 , , , ,, sorting (descending) Slide 50 Oracle Date Format Oracle stores dates in a internal numeric format(Valid date range from January 1, 4712 BC to December 31, 9999 AD) Century,year,month,day,hours,minutes,seconds Default date display is DD-MON-RR SYSDATE is a function returning date and time DUAL id a dummy table used to view SYSDATE Slide 51 RR Date Format Slide 52 RR Date Format (examples) SELECT TO_CHAR(TO_DATE(27-OCT-98, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 1998 SELECT TO_CHAR(TO_DATE(27-OCT-98, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 1998 SELECT TO_CHAR(TO_DATE(27-OCT-17, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 2017 SELECT TO_CHAR(TO_DATE(27-OCT-17, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 2017 Slide 53 YY Date Format (examples) SELECT TO_CHAR(TO_DATE(27-OCT-98, DD-MON-YY),YYYY) "Year" FROM DUAL; Year ---- 2098 SELECT TO_CHAR(TO_DATE(27-OCT-98, DD-MON-YY),YYYY) "Year" FROM DUAL; Year ---- 2098 SELECT TO_CHAR(TO_DATE(27-OCT-17, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 2017 SELECT TO_CHAR(TO_DATE(27-OCT-17, DD-MON-RR),YYYY) "Year" FROM DUAL; Year ---- 2017 Slide 54 Display the current date SQL> SELECT SYSDATE FROM DUAL; SYSDATE ---------------- 11-OCT-03 SQL> SELECT SYSDATE FROM DUAL; SYSDATE ---------------- 11-OCT-03 SQL> SELECT TO_CHAR(SYSDATE,YYYY-MM-DD HH24:MI:SS) FROM DUAL; TODAY -------------------------------------- 2003-10-11 23:07:01 SQL> SELECT TO_CHAR(SYSDATE,YYYY-MM-DD HH24:MI:SS) FROM DUAL; TODAY -------------------------------------- 2003-10-11 23:07:01 Slide 55 Date Function MONTHS_BETWEEN(date1,date2) ADD_MONTHS(date,n) NEXT_DAY(date,char) LAST_DAY(date) ROUND(date,fmt) TRUNC(date,fmt) Slide 56 Exercise 3 3 , , Slide 57 Displaying Data from Multiple Tables Slide 58 What is a join ? A Join is used to query data from more than one table Rows are joined using common values, typically primary and foreign key values Join methods Equijoin Non-equijoin Outer Join Self Join Slide 59 Simple Join Query : Syntax Write the join condition in the WHERE clause Column names must be prefixed with the table name when the same column name appears in more than one table Precede each column name with the table name for clarity SELECT table1.column,table.column FROM table1,table2 WHERE table1.column1=table2.column2; SELECT table1.column,table.column FROM table1,table2 WHERE table1.column1=table2.column2; Slide 60 Cartesian Product A Cartesian product is formed when A join condition is omitted A join condition is invalid All rows in the first table are joined to all rows in the second table To avoid a Cartesian product,always include a valid join condition in a where clause Slide 61 Equijoin SELECT last_name, job_id, departments.department_id, department_name FROM employees, departments WHERE employees.department_id = departments.department_id SELECT last_name, job_id, departments.department_id, department_name FROM employees, departments WHERE employees.department_id = departments.department_id Slide 62 Table Aliases SELECT e.last_name,e.job_id, d.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id SELECT e.last_name,e.job_id, d.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id Slide 63 Non-Equijoin SELECT e.ename,e.sal,s.grade,s.losal,s.hisal FROM emp e,salgrade s WHERE e.sal BETWEEN s.losal AND s.hisal; SELECT e.ename,e.sal,s.grade,s.losal,s.hisal FROM emp e,salgrade s WHERE e.sal BETWEEN s.losal AND s.hisal; Slide 64 Outer Join Employees Departments SQL Slide 65 Outer Join SELECT e.first_name,d.department_name,d.department_id FROM departments d,employees e WHERE d.department_id=e.department_id(+) SELECT e.first_name,d.department_name,d.department_id FROM departments d,employees e WHERE d.department_id=e.department_id(+) SELECT e.first_name,d.department_name,d.department_id FROM departments d left outer join employees e ON d.department_id=e.department_id SELECT e.first_name,d.department_name,d.department_id FROM departments d left outer join employees e ON d.department_id=e.department_id Standard SQL Oracle SQL Slide 66 Self Join Employees(worker) Employees(manager) SQL Slide 67 Self Join SELECT w.last_name worker, m.last_name manager FROM employees w, employees m WHERE w.manager_id = m.employee_id AND w.last_name LIKE 'R%' SELECT w.last_name worker, m.last_name manager FROM employees w, employees m WHERE w.manager_id = m.employee_id AND w.last_name LIKE 'R%' Slide 68 Group Functions Group function operate on sets of rows to give on result per group Group function appear in both SELECT lists and HAVING clauses The GROUP by clause in the SELECT statement divides rows into smaller groups The HAVING clause restricts result groups Slide 69 Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE Slide 70 GROUP BY and HAVING in the SELECT statement Slide 7