NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright ©...

39
NTAUG NTAUG Introduction in to use of Introduction in to use of SQL SQL Peter Dominey Peter Dominey Copyright Copyright © Peter Dominey 2004, <[email protected]> © Peter Dominey 2004, <[email protected]>

Transcript of NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright ©...

Page 1: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

NTAUGNTAUG

Introduction in to use of SQLIntroduction in to use of SQLPeter DomineyPeter Dominey

Copyright Copyright © Peter Dominey 2004, <[email protected]>© Peter Dominey 2004, <[email protected]>

Page 2: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQLSQL

Some HistorySome History

Page 3: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQLSQL

Some HistorySome History Grew out of IBM’s System/R project in 1974Grew out of IBM’s System/R project in 1974

Page 4: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQLSQL

Some HistorySome History Grew out of IBM’s System/R project in 1974Grew out of IBM’s System/R project in 1974 Originally “Structured English Query Originally “Structured English Query

Language” which is why we pronounce it Language” which is why we pronounce it ‘sequel’.‘sequel’.

Page 5: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQLSQL

Some HistorySome History Grew out of IBM’s System/R project in 1974Grew out of IBM’s System/R project in 1974 Originally “Structured English Query Originally “Structured English Query

Language” which is why we pronounce it Language” which is why we pronounce it ‘sequel’.‘sequel’.

Oracle, in 1979 had SQL as it’s query Oracle, in 1979 had SQL as it’s query languagelanguage

Page 6: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQLSQL

Some HistorySome History Grew out of IBM’s System/R project in 1974Grew out of IBM’s System/R project in 1974 Originally “Structured English Query Originally “Structured English Query

Language” which is why we pronounce it Language” which is why we pronounce it ‘sequel’.‘sequel’.

Oracle, in 1979 had SQL as it’s query Oracle, in 1979 had SQL as it’s query languagelanguage

After DB2 arrived and SQL became After DB2 arrived and SQL became accepted, ANSI/ISO standards developedaccepted, ANSI/ISO standards developed

Page 7: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL Used inSQL Used in

Page 8: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL Used inSQL Used in

OracleOracle SyBaseSyBase DB2DB2 SQLServerSQLServer MySQLMySQL OthersOthers

Page 9: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL OverviewSQL Overview SQL is the standard database language SQL is the standard database language

for most commercial relational databases. for most commercial relational databases. It's wide acceptance stems from its ability It's wide acceptance stems from its ability to manage all of the necessary database to manage all of the necessary database manipulations while remaining relatively manipulations while remaining relatively easy to learn.easy to learn.

Page 10: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL OverviewSQL Overview SQL is the standard database language SQL is the standard database language

for most commercial relational databases. for most commercial relational databases. It's wide acceptance stems from its ability It's wide acceptance stems from its ability to manage all of the necessary database to manage all of the necessary database manipulations while remaining relatively manipulations while remaining relatively easy to learn.easy to learn.

English-like command structure makes it English-like command structure makes it readable, while providing for the most readable, while providing for the most complex of database functionality.complex of database functionality.

Page 11: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL OverviewSQL Overview SQL is the standard database language for most SQL is the standard database language for most

commercial relational databases. It's wide commercial relational databases. It's wide acceptance stems from its ability to manage all acceptance stems from its ability to manage all of the necessary database manipulations while of the necessary database manipulations while remaining relatively easy to learn.remaining relatively easy to learn.

English-like command structure makes it English-like command structure makes it readable, while providing for the most complex readable, while providing for the most complex of database functionality.of database functionality.

40+ SQL statements follow the same basic 40+ SQL statements follow the same basic structure, structural consistency keeps it easy to structure, structural consistency keeps it easy to read and learnread and learn

Page 12: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

All statements start withAll statements start with

Page 13: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

All statements start withAll statements start with A verb, A verb,

Page 14: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

All statements start withAll statements start with A verb, A verb,

selectselect

Page 15: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

All statements start withAll statements start with A verb, A verb,

SelectSelect InsertInsert DeleteDelete UpdateUpdate

Page 16: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

All statements start withAll statements start with A verb, A verb,

SelectSelect InsertInsert DeleteDelete UpdateUpdate

A list of tables and/or fields follow the verbA list of tables and/or fields follow the verb Followed by a clause or clausesFollowed by a clause or clauses

Page 17: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

Consider the following:Consider the following: SELECT job_name,machine FROM job SELECT job_name,machine FROM job

WHERE machine=‘somename’WHERE machine=‘somename’

Page 18: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

The verb defines this as a SELECT statement, which The verb defines this as a SELECT statement, which retrieves rows from a table. We are selecting two retrieves rows from a table. We are selecting two columns, job_name and machine from the table, job.columns, job_name and machine from the table, job.

Two clauses follow the SELECT statement with the Two clauses follow the SELECT statement with the keywords FROM and WHERE. The FROM clause keywords FROM and WHERE. The FROM clause selects the two columns from a table named “job", selects the two columns from a table named “job", while the WHERE clause includes the expression that while the WHERE clause includes the expression that we only want data from rows where the machine is we only want data from rows where the machine is ‘somename’. This query's result would be a list of jobs ‘somename’. This query's result would be a list of jobs for the machine ‘somename’. for the machine ‘somename’.

Page 19: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

Another example:Another example: SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine=‘somename’ AND owner=‘root’machine=‘somename’ AND owner=‘root’

Page 20: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

We have here created a query that no We have here created a query that no only selects the jobs that are run on a only selects the jobs that are run on a particular machine (‘somename’) but particular machine (‘somename’) but refined it to select only those that are run refined it to select only those that are run on that machine AND owned by the user on that machine AND owned by the user ‘root’‘root’

Page 21: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

A further example:A further example: SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine=‘somename’ AND owner=‘fred’ machine=‘somename’ AND owner=‘fred’ AND AND command='/sybasedv/dbaroot/bin/do_dbcc.kcommand='/sybasedv/dbaroot/bin/do_dbcc.ksh -Dmaster -SAUTOSYS_SQL_DEV1'sh -Dmaster -SAUTOSYS_SQL_DEV1'

Page 22: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

This highlights two important pointsThis highlights two important points

Page 23: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

This highlights two important pointsThis highlights two important points The any number of clauses can be The any number of clauses can be

appended to refine the statement appended to refine the statement

Page 24: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

This highlights two important pointsThis highlights two important points The any number of clauses can be The any number of clauses can be

appended to refine the statement andappended to refine the statement and The text can be included as fully as you The text can be included as fully as you

wish, so long as it is enclosed in quotes ‘’ wish, so long as it is enclosed in quotes ‘’

Page 25: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL StructureSQL Structure

This highlights two important pointsThis highlights two important points The any number of clauses can be The any number of clauses can be

appended to refine the statement andappended to refine the statement and The text can be included as fully as you The text can be included as fully as you

wish, so long as it is enclosed in quits ‘’ wish, so long as it is enclosed in quits ‘’ Please note – ignore MS’s stupid quote marks, Please note – ignore MS’s stupid quote marks,

they are just single quotesthey are just single quotes

Page 26: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL clausesSQL clauses

The extend and power of SQL lays in the The extend and power of SQL lays in the clauses you can construct.clauses you can construct.

Page 27: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL clausesSQL clauses

The extend and power of SQL lays in the The extend and power of SQL lays in the clauses you can construct.clauses you can construct. Take the earlier statement: Take the earlier statement:

SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE machine=‘somename’ AND owner=‘fred’ AND machine=‘somename’ AND owner=‘fred’ AND command='/sybasedv/dbaroot/bin/do_dbcc.ksh -command='/sybasedv/dbaroot/bin/do_dbcc.ksh -Dmaster -SAUTOSYS_SQL_DEV1'Dmaster -SAUTOSYS_SQL_DEV1'

Page 28: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘LIKE’ clauseSQL ‘LIKE’ clause

The extend and power of SQL lays in the The extend and power of SQL lays in the clauses you can construct.clauses you can construct. To make it find jobs that have similar commands we To make it find jobs that have similar commands we

could have a state: could have a state: SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine=‘somename’ AND owner=‘fred’ AND command machine=‘somename’ AND owner=‘fred’ AND command LIKE ‘%dbcc%’LIKE ‘%dbcc%’

Here we are finding anything that has the string Here we are finding anything that has the string dbcc in it from the column ‘command’dbcc in it from the column ‘command’

% is used as a wild card.% is used as a wild card.

Page 29: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘DISTINCT’ clauseSQL ‘DISTINCT’ clause

The extend and power of SQL lays in the The extend and power of SQL lays in the clauses you can construct.clauses you can construct. To find all the machines that have jobs To find all the machines that have jobs

defined, but only list each unique machines defined, but only list each unique machines name:name: SELECT DISTINCT machine FROM jobSELECT DISTINCT machine FROM job

Page 30: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘DISTINCT’ clauseSQL ‘DISTINCT’ clause

The extend and power of SQL lays in the The extend and power of SQL lays in the clauses you can construct.clauses you can construct. To find all the machines that have jobs defined, but To find all the machines that have jobs defined, but

only list each unique machines name:only list each unique machines name: SELECT DISTINCT machine FROM jobSELECT DISTINCT machine FROM job

And then order it in a simple alphabetical orderAnd then order it in a simple alphabetical order SELECT DISTINCT machine FROM job ORDER BY SELECT DISTINCT machine FROM job ORDER BY

machinemachine

Page 31: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘WHERE’ clauseSQL ‘WHERE’ clause

We touched earlier on the where clauseWe touched earlier on the where clause

Page 32: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘WHERE’ clauseSQL ‘WHERE’ clause

We touched earlier on the where clauseWe touched earlier on the where clause SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine=‘somename’machine=‘somename’

And made the use of the simplest form, equals And made the use of the simplest form, equals ‘=‘ ‘=‘

Page 33: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘WHERE’ clauseSQL ‘WHERE’ clause

We touched earlier on the where clauseWe touched earlier on the where clause SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine=‘somename’machine=‘somename’

And made the use of the simplest form and And made the use of the simplest form and equals ‘=‘ equals ‘=‘

There are other operators:There are other operators:

Page 34: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘WHERE’ clauseSQL ‘WHERE’ clause

We touched earlier on the where clauseWe touched earlier on the where clause SELECT job_name FROM job WHERE machine=‘somename’SELECT job_name FROM job WHERE machine=‘somename’

And made the use of the simplest form and equals ‘=‘ And made the use of the simplest form and equals ‘=‘ There are other operators:There are other operators:

== Equal Equal <><> Not equal Not equal >> Greater than Greater than << Less than Less than >=>= Greater than or equal Greater than or equal <=<= Less than or equal Less than or equal BETWEEN BETWEEN Between an inclusive range Between an inclusive range LIKELIKE Search for a patternSearch for a pattern

Page 35: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL ‘WHERE’ clauseSQL ‘WHERE’ clause

To extract information where you have a To extract information where you have a list of items:list of items: SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE

machine IN machine IN (‘machine1’,’machinezxt’,’machine1b’)(‘machine1’,’machinezxt’,’machine1b’)

Page 36: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL Select within SelectSQL Select within Select

SELECT job_name FROM job WHERE SELECT job_name FROM job WHERE machine IN (SELECT hostname FROM machine IN (SELECT hostname FROM keymaster)keymaster)

Page 37: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL Information from SQL Information from multiple tablesmultiple tables

Examine the following statement:Examine the following statement: SELECT keymaster.hostid, job.job_name SELECT keymaster.hostid, job.job_name

FROM keymaster, job WHERE FROM keymaster, job WHERE job.machine=keymaster.hostnamejob.machine=keymaster.hostname

Page 38: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL IntroductionSQL Introduction

As stated earlier, this was introduction to As stated earlier, this was introduction to SQL and hopefully has provided a taste SQL and hopefully has provided a taste of the things that can be done to extract of the things that can be done to extract information directly from the AutoSys information directly from the AutoSys database.database.

Page 39: NTAUG Introduction in to use of SQL Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,

SQL IntroductionSQL Introduction

As stated earlier, this was introduction to As stated earlier, this was introduction to SQL and hopefully has provided a taste SQL and hopefully has provided a taste of the things that can be done to extract of the things that can be done to extract information directly from the AutoSys information directly from the AutoSys database.database.

[email protected]@dominey.biz www.dominey.bizwww.dominey.biz