ntaug introduction in to use of sql peter dominey copyright © peter dominey 2004, copyright ©...
TRANSCRIPT
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]>
SQLSQL
Some HistorySome History
SQLSQL
Some HistorySome History Grew out of IBM’s System/R project in 1974Grew out of IBM’s System/R project in 1974
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’.
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
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
SQL Used inSQL Used in
SQL Used inSQL Used in
OracleOracle SyBaseSyBase DB2DB2 SQLServerSQLServer MySQLMySQL OthersOthers
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.
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.
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
SQL StructureSQL Structure
All statements start withAll statements start with
SQL StructureSQL Structure
All statements start withAll statements start with A verb, A verb,
SQL StructureSQL Structure
All statements start withAll statements start with A verb, A verb,
selectselect
SQL StructureSQL Structure
All statements start withAll statements start with A verb, A verb,
SelectSelect InsertInsert DeleteDelete UpdateUpdate
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
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’
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’.
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’
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’
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'
SQL StructureSQL Structure
This highlights two important pointsThis highlights two important points
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
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 ‘’
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
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.
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'
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.
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
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
SQL ‘WHERE’ clauseSQL ‘WHERE’ clause
We touched earlier on the where clauseWe touched earlier on the where clause
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 ‘=‘ ‘=‘
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:
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
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’)
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)
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
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.
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