Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories...

41
Introduction to SQL Ahmet Oezcimen

Transcript of Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories...

Page 1: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Introduction to SQL

Ahmet Oezcimen

Page 2: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Agenda1. What is SQL?2. SQL data types3. Statement categories in SQL4. Data modelling example5. CREATE & DROP command structures6. SELECT, INSERT, UPDATE & DELETE command structures7. Operators8. Functions9. Basic SQL statements10. Advanced SQL statements11. Tips and techniques for better SQL12. Tools using SQL13. SQL*Plus14. Literature15. Questions

Page 3: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

1. What is SQL?

• SQL stands for Structured Query Language

• SQL is an ANSI (American National Standards Institute) standard language

• SQL works with Oracle, Sybase, Informix, MS SQL Server, DB2, MySQL, etc..

• SQL is easy to learn

Page 4: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

2. SQL data types

• VARCHAR2 – contains text string up to 4,000b• NUMBER - contains numeric data• DATE – contains date data• BLOB – Large binary object up to 4G• CLOB – Large character-based object uo to 4G• BFILE – Large external file• Etc …

Page 5: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

3. Statement categories in SQL • DDL ( Data Definition Language )

– CREATE, ALTER, DROP, etc…

• DML ( Data Manipulation Language )– SELECT, UPDATE, DELETE, INSERT

• Transaction Control Commands– COMMIT, ROLLBACK, SAVEPOINT, etc…

• Session Control Commands– ALTER SESSION

• DCL (Data Control Language)– GRANT, REVOKE, etc…

• Etc…

Page 6: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

4. Data modelling exampleDepartment

(deptno)

Job(jobno)

Interview(intno)

Candidate(cno)

Explanation:A deparment might have more than one job therefore also might invite more than one candidate and for one job might more than one interview and one candidate might be invited several times.

Page 7: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

4. Data modelling example (cont.)

Deparment : deptno number(5), deptname varchar2(25), depthead varchar2(35)

Job : jobno number(5), jobtitle varchar2(25),salary number(5), deptno number(5)

Candidate: cno number(5), cname varchar2(25), deptno number(5), ctel varchar2(20), position varchar(25)

Interview: intno number(5), intdate date, jobno number(5), cno number(5)

Page 8: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

5. CREATE & DROP command structures

CREATE TABLE <table_name> (<column 1>,..,<column n>)

TABLESPACE <tablespace_name>;

DROP TABLE <table_name>;

Page 9: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

6. SELECT, INSERT, UPDATE & DELETE command structures

SELECT [distinct] <column 1>,..,<column n>FROM <table 1 or view 1> [alias],.., <table n or view n> [alias]WHERE <condition>GROUP BY <column 1>,..,<column n> HAVING <condition>CONNECT BY PRIOR <condition> START WITH <condition>ORDER BY <column 1>,..,<column n> [ASC or DESC];

DELETE FROM <table> WHERE <condition>;

INSERT INTO <table> [(<column list>)]VALUES (value list);

UPDATE <table> SET <column> = <value>[WHERE <condition>];

Page 10: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

7. Operators

• Arithmetical operators– /, *, +, -, =, !=, <>, >, <, >=, <=

• Boolean operators– NOT, NOR, AND, OR

• Logical operators– IN, EXISTS, LIKE

Page 11: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

8. Functions• Text functions

– Lpad(x,y [,z]), rpad(x,y [,z]), lower(x), upper(x), initcap(x), length(x), substr(x,y[,z]),instr(x,y), trim(), replace(x,y [,z])

• Math functions

– Abs(x), ceil(x), floor(x), mod(x,y), round(x,y), sign(x), sqrt(x), trunc(x,y), vsize(x), Nvl(), decode(), greatest(x,y,…), least(x,y,…)

• Date functions

– Sysdate, add_months(x,y), last_day(x), months_between(x,y), new_time(x,y,z), next_day(x)

• Conversion functions

– To_char(x), to_number(x), to_date(x), etc…

• Group functions

– avg(x), count(x), max(x), min(x), sum(x), etc…

Page 12: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

9. Basic SQL statement• The most easiest SQL is just to select a table

– Select * from department;

– select deptname, depthead from department;

• Conditional Selection– Select * from department where depthead=‘Alvis Brazma’;

• Relational operators– Select * from job where salary > 2000;

• More complex conditions– Select * from job where deptno=1 or deptno=2;

– Select * from job where deptno=3 and salary > 2000;

• Using IN & BETWEEN– Select * from job where deptno in (1,2);

– Select * from job where deptno between 1 and 3;

• Using LIKE– Select * from department where depthead like ‘Alvis%’;

Page 13: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

10. Advanced SQL statements• Joins & outer joins

– Select * from department, job where department.deptno=job.deptno;– select * from department a, job b where a.deptno=b.deptno(+);

• Group by– Select max(salary) from job;– Select jobtitle,max(salary) from job group by jobtitle;

• Subqueries– select * from job where deptno in (select deptno from department);

• Exists & all– select * from job where exists (select * from department);– select * from job where salary >= all (select salary from job);

• Union– select deptno,deptname from department union select jobno,jobtitle from job;

• Intersect & minus– select deptno from job minus select deptno from job where jobbtitle=‘DBA’;– select jobtitle from job where deptno=1 intersect select jobtitle from job where

deptno=2;

Page 14: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL

Index will not be used when a function

is performed on an indexed field.

SELECT *

FROM department

WHERE SUBSTR(DEPTNAME, 1, 3) = ‘Arr’

Page 15: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

• If you cannot avoid using a function:

– Use the INDEX hint.

SELECT /*+ INDEX(DEPARTMENT DEPT_IDX_1) */ *

FROM DEPARTMENT

WHERE SUBSTR(DEPTNAME, 1, 3) = ‘Arr’

Page 16: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Replace SELECT * FROM job WHERE SALARY +1000 = :NEWSALARY

with SELECT * FROM job WHERE SALARY = :NEWSALARY -1000

11. Tips and techniques for better SQL (cont.)

Page 17: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Indexes are not used, and a full table scan is done

when the WHERE clause contains:

!= (NOT EQUALS)

LIKE '%SA%'

Page 18: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Depending on the range of the numbers in a BETWEEN, the optimizer will choose to do a full table scan or use the index.

SELECT * FROM job WHERE salary BETWEEN 2500 AND 3500

Page 19: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

When retrieving a large portion of the table’s data,

full table scans are likely to offer the best performance.

When retrieving small number of rows, avoid full table scans.

Page 20: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Sometimes DO disable the index.

SELECT * FROM job WHERE SALARY + 0 = '10000'

SELECT * FROM departmentWHERE deptname || '' = ‘A'

Page 21: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Select the smallest table or smallest result set first.

SELECT a.deptname

FROM department a, job b

WHERE a.deptno=b.deptno

(assume a.deptno and b.deptno are indexed)

Page 22: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Small table should drive the large table. If job is a large table and department is a small one. Disable index on department. This changes the table driving path.

Replace SELECT * FROM job large, department small WHERE large.deptno = small.deptno

WithSELECT * FROM job large, department smallWHERE large.deptno = small.deptno || ''

Page 23: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

A table join is normally better than sub-query Replace:

SELECT * FROM job aWHERE a.deptno IN

(SELECT b.deptno FROM department b) With:

SELECT A.* FROM job A, department B WHERE A.job = B.department

Page 24: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

These statements have the same result.Assume table job, department relationship is one to many.

SELECT * FROM jobWHERE deptno IN (SELECT deptno FROM department)

SELECT * FROM jobWHERE EXISTS (SELECT deptno FROM department WHERE job.deptno = department.deptno)

11. Tips and techniques for better SQL (cont.)

Page 25: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Use IN operator

IN checks all rows. Only use IN if the table in the subquery is extremely small.

SELECT *

FROM job

WHERE deptno IN (SELECT deptno FROM department)

Page 26: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Use EXISTS operator

EXISTS makes use of the index on the deptno column in the department table. For large tables, it may be faster.

SELECT * FROM jobWHERE EXISTS (SELECT deptno FROM department

WHERE job.deptno = department.deptno)

Page 27: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

If A.STATE and B.STATE have a unique index,

Replace: SELECT a.deptno, b.deptno FROM department a, job bWHERE a.deptno = b.deptno (+)

With:SELECT a.deptname, b.jobtitleFROM department a, job b WHERE a.deptno = b.deptno UNION ALL SELECT c.deptname, NULL FROM department c WHERE NOT EXISTS (SELECT 'X' FROM job d

WHERE c.deptno = d.deptno)

Page 28: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

11. Tips and techniques for better SQL (cont.)

Replace:SELECT * FROM department a, job b, candidate cWHERE a.deptno=b.deptno

and b.deptno=c.deptno and c.deptno=1

With:SELECT * FROM A,B,CFROM department a, job b, candidate cWHERE a.deptno=b.deptno

and b.deptno=c.deptno and a.deptno=1 and b.deptno=1 and c.deptno=1

Page 29: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

12. Tools using SQL

• Oracle SQL*Plus

• Oracle PL/SQL

• Oracle Forms

• Oracle Reports

• Oracle ProC

• Java-JDBC

• Perl-CGI

• etc …

Page 30: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

13. SQL*Plus• Starting SQL*Plus

– Sqlplus [<username>/<password>] [@<db name>] [@<script-name>]

• Running a script– SQL>Start <script-name>– SQL>@<script-name>

• Exiting SQL*Plus– exit

• Some important SQL*Plus commands– Host or !, set, spool, rem, accept, prompt, @ or start,

commit, rollback, help, desc, ttitle, btitle, break, etc …

Page 31: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

14. Literature

• Oracle8i The complete reference Autors:Kevin Loney, George Koch

• Learning SQL

Autors: Richard W.Earp,Sikha S.Bagui

• Oracle SQL and PL/SQL Handbook Autors: John Adolph Palinski

Page 32: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.
Page 33: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Glossary –Table: department

Deptno Deptname Depthead

2 MSD Kim Henrick

3 Swiss-Prot Rolf Apweiler

4 TrEMBL Rolf Apweiler

1 ArrayExpress Alvis Brazma

5 Personnel Keith Wiliamson

Page 34: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Glossary - Table: jobJobno Jobtitle Salary Deptno

6 Database coordinator 5000 2 7 Database curator 4000 2 8 Software Engineer 3000 2 9 WEB Developer 3500 2 10 DBA 2000 2 11 Database coordinator 5000 3 12 Database curator 4000 3 13 Software Engineer 3000 3 14 WEB Developer 3500 3 15 DBA 2000 3 16 Database coordinator 5000 4 17 Database curator 4000 4 18 Software Engineer 3000 4 19 WEB Developer 3500 4 20 DBA 2000 4 1 Database coordinator 5000 1 2 Database curator 4000 1 3 Software Engineer 3000 1 4 WEB Developer 3500 1 5 DBA 2000 1

Page 35: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Glossary - Table: candidate

Cno Cname Ctel Position Deptno

1 Richard 0044-1243-123456

Database coordinator

1

2 Don 0044-1243-123456

Database coordinator

1

3 Donald 0044-1243-123456

Database coordinator

1

4 Marie 0044-1243-123456

Database coordinator

1

5 Elisa 0044-1243-123456

Database coordinator

1

Page 36: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Glossary - Table: interview

Intno Intdate Jobno Cno

1 2002-02-02 00:00:00.0 1 1

Page 37: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Exercises

1. Very simple queries2. Queries by using WHERE condition3. Queries by using ORDER BY4. Queries by using functions5. Queries by using joins6. Queries by using subqueries7. Queries by using GROUP BY8. Using queries for any other database (ARRAYS,

EXPERIMENTS, PROTOCOLS)

Page 38: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Some AE examples

- Show me arrays that use PCR products:

Select i.identifier as array, o.value as technology

From tt_identifiable i, tt_physicalarraydesign a,

tt_featuregroup f, tt_ontologyentry o

where i.id = a.id and a.id = f.t_arraydesign_id and

f.technologytype_id = o.id and

lower( o.value ) like '%pcr%'

Page 39: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Some AE examples

- Show me all the bibliographic references that are books:

select b.title as Title, b.authors as Author

from tt_bibliographicreference b,

tt_parameters_t_bibliogra l, tt_ontologyentry o

where b.id = l.t_bibliographicreference_id

and l.parameters_id = o.id

and lower( o.value ) like '%book%'

Page 40: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Some AE examples

- Produce a table of all the experiments and the arrays:

select distinct ie.identifier as experiment, ia.identifier as arraydesign

from tt_identifiable ie, tt_experiment e, tt_bioassays_t_experiment eb,

tt_physicalbioassay pba, tt_bioassaycreation bac, tt_array a,

tt_identifiable ia

where ie.id = e.id and e.id = eb.t_experiment_id

and eb.bioassays_id = pba.id and pba.bioassaycreation_id = bac.id

and bac.array_id = a.id and ia.id = a.arraydesign_id

order by ie.identifier asc

Page 41: Introduction to SQL Ahmet Oezcimen. Agenda 1. What is SQL? 2. SQL data types 3. Statement categories in SQL 4. Data modelling example 5. CREATE & DROP.

Some AE examples

-Produce a table of all the experiments and the species they study:

select distinct i.identifier as experiment, o.value as species

from tt_identifiable i, tt_biomaterials_experiments eb,

tt_characteris_t_biomateri bo, tt_ontologyentry o

where i.id = eb.experiments_id

and eb.biomaterials_id = bo.t_biomaterial_id

and bo.characteristics_id = o.id

and o.category like '%species%'

order by i.identifier