Presentation is loading. Please wait.

Presentation is loading. Please wait.

Steve Coleman UTOUG Fall Symposium October 26, 2016

Similar presentations


Presentation on theme: "Steve Coleman UTOUG Fall Symposium October 26, 2016"— Presentation transcript:

1 Steve Coleman UTOUG Fall Symposium October 26, 2016
SQL The Director’s Cut Steve Coleman UTOUG Fall Symposium October 26, 2016

2 Background Database developer since 2006 PL/SQL Developer
Quattro Pro Excel SQL Server/Access Oracle PL/SQL Developer Data Detective 10+ years of OJT

3 Mitsakes, I’ve made a few
Let’s go through a couple of common, easy-to-make mistakes

4 Understand your tools What SQL IS Set-based language Relational data
ANSI compliant (SQL 92) What SQL IS NOT Object-oriented Procedural Unstructured ANSI compliant (SQL 92) SQL is a query language, not a procedural programming language.

5 Logical Query Processing
What you write What actually happens 5 - SELECT 1 - FROM 1 - FROM 2 - WHERE 2 - WHERE 3 - GROUP BY 3 - GROUP BY 4 - HAVING 4 - HAVING 5 - SELECT 6 - ORDER BY 6 - ORDER BY

6 Survey says… If you know the data you may be lulled into a sense of false security because it just so happens that King, Kochhar, and De Haan are the top 3 salaries. But remember that SELECT is executed before ORDER BY and ROWNUM is assigned during the SELECT phase. You can not assume an order to a query. If you need order you must ORDER BY. And if you need the top n results you must order in an inner query.

7 LEFT INNER JOIN SELECT e.first_name ,e.last_name ,e.hire_date
,e.job_id ,h.* FROM hr.employees e LEFT OUTER JOIN hr.job_history h ON h.employee_id = e.employee_id There are 107 total employees. So 110 rows means that some employees have more than one history record. WHERE h.start_date > TO_DATE('13-JAN-2001', 'dd-MON-yyyy');

8 Virtually the same … FROM employees
EMPLOYEE_ID FIRST_NAME LAST_NAME 100 Steven King SKING 101 Neena Kochhar NKOCHHAR 102 Lex De Haan LDEHAAN LEFT OUTER JOIN job_history ON employee_id = employee_id EMPLOYEE_ID FIRST_NAME LAST_NAME START_DATE JOB_ID… 100 Steven King SKING (null) 101 Neena Kochhar NKOCHHAR 21-SEP-97 AC_ACCOUNT 28-OCT-01 AC_MGR 102 Lex De Haan LDEHAAN 13-JAN-01 IT_PROG

9 LEFT OUTER JOIN SELECT e.first_name ,e.last_name ,e.hire_date
,e.job_id ,h.* FROM hr.employees e LEFT OUTER JOIN hr.job_history h ON h.employee_id = e.employee_id AND h.start_date > TO_DATE('13-JAN-2001', 'dd-MON-yyyy'); Remember that the WHERE clause is processed after the FROM and JOINS. In the WHERE clause only records meeting the criteria will be returned. Employees with no history don’t match the WHERE criteria, so the record is thrown out. All filters on data in the RIGHT table of a LEFT JOIN must be included in the ON clause

10 A parenthetical example
SELECT * FROM hr.employees WHERE department_id IN (30,80) AND job_id = 'PU_MAN' OR (job_id like '%MAN' AND commission_pct >= .4) AND job_id LIKE '%MAN'

11 A parenthetical example (cont.)
SELECT * FROM hr.employees WHERE department_id IN (30,80) AND ( job_id = 'PU_MAN' OR (job_id like '%MAN' AND commission_pct >= .4))

12 What’s in an Alias? 1. Does the departments table have employee_id?
No. Okay, so what about the employees table? Yes. Great, let’s just use that 4. So we’re left with employee_id = employee_id

13 ANSI Isolation Levels Isolation Level Dirty Read Non-repeatable
Phantom READ UNCOMMITTED Yes READ COMMITTED No REPEATABLE READ SERIALIZABLE Dirty Read: uncommitted data Transaction isolation levels are defined in terms of 3 phenomena that can happen when reading data in a database where writes are occurring concurrently. Each isolation level is defined by which of these phenomena are allowed to occur. Non-repeatable Read: A record read at time T1 may give different results when read at T2 Phantom Read: A query ran at time T1 may give different results at T2 because more data satisfies the query criteria than before

14 Database Isolation Levels
Oracle SQL SERVER READ UNCOMMITTED READ COMMITTED (default) REPEATABLE READ SNAPSHOT (2005+) SERIALIZABLE READ-ONLY You need to understand how your tool handles concurrency so you don’t make mistakes. The purpose of READ UNCOMMITTED is to allow for a non-blocking read, which Oracle already does.

15 To COMMIT or not COMMIT The database doesn’t define transaction boundaries. You do that, whether you realize it or not Session 1 Session 2 CREATE TABLE table_a (col_a integer); INSERT INTO table_a VALUES(1); ALTER TABLE table_a ADD (col_b VARCHAR2(1)); SELECT COUNT(*) FROM table_a; UPDATE table_a SET col_b = ‘A’ WHERE col_a = 1;

16 Double the fun SELECT COUNT(*) Try this instead INTO l_counter
FROM tables … WHERE … IF l_counter > 0 THEN UPDATE … Try this instead UPDATE … WHERE … l_counter := SQL%ROWCOUNT Table is hit twice for no reason

17 Greece …in the details The Hellenic Republic
Some mistakes happen long before code is written More commonly known as…. The Hellenic Republic Greece

18 A wrinkle in time...queries
Effective dates Start Date End Date Does it mean that it ENDS on this date, or that it ENDED as of this date What time? Make a plan to write date-range queries consistently

19 A letter to the editor EDIT PEER REVIEW
Take a mental break between writing and editing Be critical Do I even need to do this at all PEER REVIEW Don’t over-explain Provide some context, but don’t be too helpful If you can’t give a solid answer to questions, be worried Don’t assume the author is competent

20 Let’s review

21 SQL – The Director’s Cut
Know your tools and environment Know your business and requirements Edit your own code Edit someone else’s code Great code has as much to do with what you leave out as it does what you put in


Download ppt "Steve Coleman UTOUG Fall Symposium October 26, 2016"

Similar presentations


Ads by Google