Banner and the SQL Select Statement: Part One (A First Example)
-
Upload
benecia-urban -
Category
Documents
-
view
78 -
download
1
description
Transcript of Banner and the SQL Select Statement: Part One (A First Example)
![Page 1: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/1.jpg)
Banner and the SQL Select Statement: Part One (A First Example)
Mark HollidayDepartment of Mathematics and
Computer ScienceWestern Carolina University
30 September 2005 and 7 October 2005(updated: 2 November 2005)
![Page 2: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/2.jpg)
Acknowledgements
Thanks to Lynn Franz and Stan Hammer for their suggestions, slides, handouts, and time.
Thanks to Larry Hammer for suggesting this project.
![Page 3: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/3.jpg)
Presentation Slides
http://cs.wcu.edu/~holliday/LectureNotes/Banner/BannerSqlSelectPart1.ppt
![Page 4: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/4.jpg)
Outline The Goal The Concepts
A First Example Single Table Selects Joins Multiple Connected Select Statements
![Page 5: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/5.jpg)
The Goal
Generate a report that answers a question about the data in Banner.
Banner => Query => Report
![Page 6: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/6.jpg)
A First Example
Outline The Relational Model: Single Table Lab 1: TOAD, Schema Browser Some Structured Query Language (SQL)
Basics Lab 2: TOAD, SQL Editor
![Page 7: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/7.jpg)
Single Table Selects
Outline WHERE clause: single condition, multiple
conditions Lab 3: Order By; Aggregate Functions Lab 4: Group By; Having Lab 5:
![Page 8: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/8.jpg)
Joins
Outline Why multiple tables? Inner Joins Lab 6: Outer Joins Lab 7:
![Page 9: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/9.jpg)
Multiple Connected Select Statements
Outline Set Operators Lab 8: Subqueries
Use directly: FROM clause Use as a set: new operators Use as a single value: aggregate functions
Lab 9: A Query Development Methodology
![Page 10: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/10.jpg)
The Relational Model: Single Table
Banner is a database application running on Oracle.
Oracle is a Relational Database Management System (RDBMS)
A RDBMS implements the relational model.
![Page 11: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/11.jpg)
RDBMSRDBMS Flavors??? (Franz/Hammer/Holliday)
Ingres,Postgres
Oracle
MS Access
DB2, Informix
Microsoft SQL Server
ANSI SQL/92ANSI SQL/92
MySQL
![Page 12: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/12.jpg)
Relational Database Model (Hammer)
Relational comes from set theory’s relation
2127JamisonErnie2
3210ChamberlinBrian3
2929HammerStan1
PhoneLastName
FirstName
ID Sets are called tables (or relations) and table elements are rows; rows consists of columns (or attributes)
Set of
Employees
i.e. all rows inEmployeetable
EMPLOYEE TABLE
![Page 13: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/13.jpg)
A sample table structure:A sample table structure: (Franz)(Franz)
![Page 14: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/14.jpg)
Constraints Constraints (Franz)(Franz)
What are (column) constraints?
A constraint is basically a rule associated with a column that the data entered into that column must follow.
When tables are created, it is common for one or more columns to have constraints associated with them.
Various constraints can be placed upon the individual columns.
![Page 15: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/15.jpg)
Constraints Constraints (Franz)(Franz)
The three most common constraints are:
• "unique" -- specifies that no two records can have the same value in a particular column; they must all be unique
• "not null" -- specifies that a column can't be left blank
• "primary key" -- defines a unique identification of each record (or row) in a table
![Page 16: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/16.jpg)
Some Banner Tables Some Banner Tables (Franz)(Franz)
Telephone Info
SPRTELE
Personal Info
SPBPERS
Addresses
SPRADDR
People(students, staff, faculty)
SPRIDENUnique identifier - PIDM
People(students, staff, faculty)
SPRIDENUnique identifier - PIDM
Addresses
SPRADDR
Personal Info
SPBPERS
Telephone Info
SPRTELE
![Page 17: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/17.jpg)
Advantages of relational databases are:Advantages of relational databases are: (Franz/Hammer)(Franz/Hammer)
Built-in multilevel integrity Duplicates are removed Primary keys are enforced Valid relationships between tables
Logical and Physical data independence from the database applications
Changes to logical database design and/or changes by vendor to the database’s physical implementation do NOT adversely affect applications built upon it
![Page 18: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/18.jpg)
Advantages of relational databases (cont.)Advantages of relational databases (cont.) (Franz/Hammer)(Franz/Hammer)
Data consistency and accuracyYou can impose various levels of
integrity within the database
Easy data retrievalInformation can be viewed in an almost
unlimited number of ways [either from one table or multiple related tables]
![Page 19: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/19.jpg)
Laboratory One Objectives:
See TOAD (Tool for Oracle Application
Development) Schema Browser
Steps Starting Toad Overall Appearance Using the Schema Browser
![Page 20: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/20.jpg)
Laboratory One: Starting Toad Desktop Icon or Start, Programs, Quest Software, TOAD
Useful Documents: Toad User’s Guide Toad Getting Started Guide shortcuts not currently working
Connect to the Banner database TRNG data set TRAINXX username
![Page 21: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/21.jpg)
Laboratory One: Starting Toad Connect to Banner RDBMS
TRNG database TRAINXX username
Once in Banner do File/End Connection File/Start Connection
![Page 22: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/22.jpg)
Laboratory One: Appearance Your new home! Lots of stuff! Three key tools are opened by default
SQL Editor (top window in middle) discussed in the later laboratories
SQL Modeler (underneath window in middle) used to graphically create a SQL statement not discussed further
Schema Browser (right side)
![Page 23: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/23.jpg)
Laboratory One: Appearance Minimize or close the SQL Editor and SQL
Modeler Maximize the Schema Browser
two panes appear two text fields above the left pane
Top left text field specifies the table owner defaults to TRAINxx no tables shown
![Page 24: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/24.jpg)
Laboratory One: Schema Browser Change table owner to SATURN
main table owner in Banner many tables are now listed in the left pane in
alphabetical order Second text field is used to narrow the list of
tables shown in the left pane * is the wildcard that matches any string
=> all tables listed
![Page 25: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/25.jpg)
Laboratory One: Schema Browser Change the second text field to S*
the left pane now only lists the tables owned by SATURN that start with a S character
In the left pane select the SPRIDEN table the right pane now shows information about this
table many tabs (focus on just a few)
![Page 26: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/26.jpg)
Laboratory One: Schema Browser Default right pane tab: Columns
shows the table structure row for each column in the table column name, data type, whether can be null, …
Data tab: the data (row values) in the current instance of the SPRIDEN table
Minimize or close the schema browser
![Page 27: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/27.jpg)
Laboratory One: SQL Editor Maximize the SQL Editor window Alternatively, open from the Database tab
along the top of the Toad window
top pane is where you enter a SQL statement press the Execute Current Stmt icon
leftmost icon in toolbar (looks like a page) The result set (table) appears in the bottom
pane
![Page 28: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/28.jpg)
WHAT IS SQL? (Hammer)
Structured Query Language to create and manipulate data Query language of relational databases Pronounced: “es queue el” Commonly: “sequel” Ubiquitous, de facto standard
![Page 29: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/29.jpg)
SQL (Hammer)
Set-based language Can operate on an entire table, or multiple
tables all at once Declarative Language (non-procedural);
express what you want without details about where or how data is located
SQL is Not a procedural language Use PL-SQL or Visual Basic w/SQL
A Procedural Language is a computer language where the programmer specifies an explicit sequences of steps to follow to produce a result
![Page 30: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/30.jpg)
Using SQL Stand-alone SQL statements
issue explicitly report generation ---- US!
Embedded within a Host Language needed sometimes (cursors) Oracle: PL/SQL Java/JDBC (Java DataBase Connectivity)
![Page 31: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/31.jpg)
SQL Statements: Sublanguages (Franz)
DDL ( Data Definition Language )1. create and destroy databases2. create and destroy database objects
(tables)3. alter tables
DML ( Data Manipulation Language ) --- US!
![Page 32: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/32.jpg)
DDL (Data Definition Language) DDL (Data Definition Language) (Franz/Holliday)(Franz/Holliday)
CREATE DATABASE Banner
This statement creates an empty database named "employees" on your DBMS (DataBase Management System).
CREATE TABLE spriden (spriden_first_name varchar2(15), spriden_last_name varchar2(60) not null, spriden_pidm number(8, 0) not null)
![Page 33: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/33.jpg)
Data Types Data Types (Franz/Hammer)(Franz/Hammer)
Number value with a maximum number of digits of "size" total, with a maximum number of "d" digits to the right of the decimal.
number(size,d) Date value date
Number value with a max number of column digits specified in parenthesis.
number(size)
Variable-length character string. Max size is specified in parenthesis.
varchar(size)
Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
char(size)
![Page 34: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/34.jpg)
DDL continued….. DDL continued….. (Franz/Holliday)(Franz/Holliday)
DROP TABLE spriden
DROP DATABASE banner
These last two commands are dangerous!
(You will not be using any of these commands in Banner except possibly with views which will be discussed much later)
![Page 35: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/35.jpg)
Some SQL Basics DDL ( Data Definition Language )
1. DML ( Data Manipulation Language ) types of updates
insert delete modify
retrieve (read-only; select statement) -- US!
![Page 36: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/36.jpg)
DML (Data Manipulation Language)DML (Data Manipulation Language)
INSERT INTO spriden
(spriden_pidm, spriden_first_name, sprident_last_name, spriden_mi)
values (12345678, ‘john’, ‘smith’, null)
![Page 37: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/37.jpg)
DML (Data Manipulation Language)DML (Data Manipulation Language)
DELETE FROM spriden SWHERE S.spriden_last_name = ‘Jones’
UPDATE spriden SSET S.spriden_last_name = ‘Thompson’WHERE S.spriden_pidm = 12345678
![Page 38: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/38.jpg)
DML: SELECT Statement DML: SELECT Statement (Franz)(Franz)
SELECT [ALL | DISTINCT] column1[,column2]
FROM table1[,table2]
[WHERE "conditions"]
[GROUP BY "column-list"]
[HAVING "conditions”]
[ORDER BY "column-list" [ASC | DESC] ]
![Page 39: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/39.jpg)
SELECT [ALL | DISTINCT] (franz)
SELECT spriden_last_name FROM spriden
This statement would retrieve all the last names in spriden.
If ‘distinct’ was omitted, (i.e., ALL was the default) these records might appear:
SELECT DISTINCT spriden_last_name FROM spriden
Conversely, if ‘distinct’ was specifically used, duplicate records would disappear:
WilsonWilson
ThompsonSmithSmithSmithJones
spriden_last_name
Wilson
Thompson
Smith
Jones
spriden_last_name
![Page 40: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/40.jpg)
SELECT * (franz)
Asterisk is used as a wildcard.The statement below will select all columns and rows in our table.
SELECT *FROM stvethn
Below is a subset of the data returned from this query:
…Asian or Pacific Islander O
……
Amer Indian/Alaskan Native I
…White, Not of Hispanic OriginW …STVETHN_DESC STVETHN_CODE
![Page 41: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/41.jpg)
SELECT Statement (franz)SELECT Statement (franz)
SELECT "column_name" FROM "table_name"
As we saw previously, the SELECT keyword allows us to grab all information from a column (or columns) on a table. Depending upon how the data is structured, there may be redundancies. To select each DISTINCT element, we add DISTINCT after SELECT.
SELECT DISTINCT "column_name" FROM "table_name"
![Page 42: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/42.jpg)
Laboratory Two: Simple Select Objectives:
Lean to use the TOAD SQL Editor on a simple select statement
Steps Starting the SQL Editor Six example select statements
![Page 43: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/43.jpg)
Laboratory Two: SQL Editor Maximize the SQL Editor window Alternatively, open from the Database tab
along the top of the Toad window
top pane is where you enter a SQL statement press the Execute Current Stmt icon
leftmost icon in toolbar (looks like a page) The result set (table) appears in the bottom
pane
![Page 44: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/44.jpg)
Laboratory Two: SQL Editor English Query:
“Find the last names of all people.”
Enter in top pane Press the Execute One Stmt icon Observe result in bottom pane
![Page 45: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/45.jpg)
Laboratory Two: SQL Editor SQL Query (first version):select saturn.spriden.spriden_last_namefrom saturn.spriden;
![Page 46: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/46.jpg)
Laboratory Two: SQL Editor Second version:
select spriden.spriden_last_namefrom spriden;
Third version:
select s.spriden_last_namefrom spriden s;
![Page 47: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/47.jpg)
Laboratory Two: SQL Editor fourth version:
select spriden_last_namefrom spriden;
a related, but different, query:
select distinct spriden_last_namefrom spriden;
![Page 48: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/48.jpg)
Laboratory Two: SQL Editor Another example sql statement:
select * from stvethn;
![Page 49: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/49.jpg)
Single Table Selects
Outline WHERE clause: single condition, multiple
conditions Lab 3: Aliases; Order By; Aggregate Functions Lab 4: Group By; Having Lab 5:
![Page 50: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/50.jpg)
Joins
Outline Why multiple tables? Inner Joins Lab 6: Outer Joins Lab 7:
![Page 51: Banner and the SQL Select Statement: Part One (A First Example)](https://reader035.fdocuments.in/reader035/viewer/2022062316/5681388c550346895da03ae6/html5/thumbnails/51.jpg)
Multiple Connected Select Statements
Outline Set Operators Lab 7: Subqueries
Use directly: FROM clause Use as a set: new operators Use as a single value: aggregate functions
Lab 8: