Download SQL – The Director`s Cut

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational algebra wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Database model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Join (SQL) wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
SQL
The Director’s Cut
Steve Coleman
UTOUG Fall Symposium
October 26, 2016
Background
• Database developer since 2006
•
•
•
•
Quattro Pro
Excel
SQL Server/Access
Oracle
• PL/SQL Developer
• Data Detective
• 10+ years of OJT
Mitsakes, I’ve made a few
https://criticalmissives.files.wordpress.com/2014/04/mistakes-were-made.jpg
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)
Logical Query Processing
What you write
5 - SELECT
1 - FROM
2 - WHERE
3 - GROUP BY
4 - HAVING
6 - ORDER BY
What actually happens
1
2
3
4
5
6
-
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
Survey says…
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
WHERE h.start_date > TO_DATE('13-JAN-2001', 'dd-MON-yyyy');
Virtually the same
… FROM employees
EMPLOYEE_ID
FIRST_NAME
LAST_NAME
EMAIL…
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
EMAIL…
EMPLOYEE_ID
START_DATE
JOB_ID…
100
Steven
King
SKING
(null)
(null)
(null)
101
Neena
Kochhar
NKOCHHAR
101
21-SEP-97
AC_ACCOUNT
101
Neena
Kochhar
NKOCHHAR
101
28-OCT-01
AC_MGR
102
Lex
De Haan
LDEHAAN
102
13-JAN-01
IT_PROG
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');
A parenthetical example
SELECT *
FROM hr.employees
WHERE department_id IN (30,80)
AND job_id =LIKE
'PU_MAN'
'%MAN'
OR (job_id like '%MAN' AND commission_pct >= .4)
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))
What’s in an Alias?
ANSI Isolation Levels
Isolation Level
Dirty Read
Non-repeatable
Phantom
READ UNCOMMITTED
Yes
Yes
Yes
READ COMMITTED
No
Yes
Yes
REPEATABLE READ
No
No
Yes
SERIALIZABLE
No
No
No
Dirty Read: uncommitted data
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
Database Isolation Levels
Oracle
SQL SERVER
READ UNCOMMITTED
READ COMMITTED (default)
READ COMMITTED (default)
REPEATABLE READ
SNAPSHOT (2005+)
SERIALIZABLE
READ-ONLY
SERIALIZABLE
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);
SELECT COUNT(*) FROM table_a;
ALTER TABLE table_a ADD (col_b VARCHAR2(1));
UPDATE table_a SET col_b = ‘A’ WHERE col_a = 1;
Double the fun
SELECT COUNT(*)
INTO l_counter
FROM tables …
WHERE …
IF l_counter > 0 THEN
UPDATE …
WHERE …
…
Table is hit twice for no reason
Try this instead
UPDATE …
WHERE …
…
l_counter := SQL%ROWCOUNT
…in the details
Some mistakes happen long before code is written
More commonly known as….
The
Hellenic
Republic
Greece
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
A letter to the editor
• EDIT
• 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
Let’s review
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