Steve Coleman UTOUG Fall Symposium October 26, 2016

Slides:



Advertisements
Similar presentations
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Advertisements

18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Nikolay Kostov Telerik Corporation
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
3 Copyright © 2006, Oracle. All rights reserved. Manipulating Large Data Sets.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
Database Programming Sections 7–Multi-row sub queries, IN, ANY, ALL, Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE,
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Using SET Operators Fresher Learning Program January, 2012.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Thinking in Sets and SQL Query Logical Processing.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Using Subqueries to Solve Queries
Restricting and Sorting Data
Basic select statement
Interacting with the Oracle Server
ATS Application Programming: Java Programming
Interacting with the Oracle Server
Manipulating Data.
Isolation Levels Understanding Transaction Temper Tantrums
Displaying Data from Multiple Tables
Using the Set Operators
Displaying Data from Multiple Tables
Database Management Systems 2
Using Subqueries to Solve Queries
Using the Set Operators
Restricting and Sorting Data
Batches, Transactions, & Errors
Chapter 2 Views.
Oracle Join Syntax.
Manipulating Data.
Using Subqueries to Solve Queries
Chapter 2 Views.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Batches, Transactions, & Errors
Lecture 3 Finishing SQL
Retrieving Data by Using Subqueries
Sioux Falls, SD | Hosted by (605) SQL
Using Subqueries to Solve Queries
IST 318 Database Administration
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Using the Set Operators
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Lesson 3.
Isolation Levels Understanding Transaction Temper Tantrums
Including Constraints
Restricting and Sorting Data
Presentation transcript:

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

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

Mitsakes, I’ve made a few Let’s go through a couple of common, easy-to-make mistakes 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) SQL is a query language, not a procedural programming language.

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

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.

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');

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… 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

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

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'

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? 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

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

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.

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;

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

Greece …in the details The Hellenic Republic 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 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

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