36
STARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? These are the tables document.docx by RT-- 14 July 2022 1 of 36

Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

STARTING UP

1 Open your Oracle and access your account.

2 Did you load the script that populates the database?These are the tables

document.docx by RT-- 16 September 2023 1 of 31

Page 2: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

Les01 -- SELECT – review but using Oracle

These cover the commands used on the courseDBS211, DBS311

NOT MANY …..

document.docx by RT-- 16 September 2023 2 of 31

Page 3: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

document.docx by RT-- 16 September 2023 3 of 31

Much of this will be a quick review at this time

Page 4: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

2

document.docx by RT-- 16 September 2023 4 of 31

Page 5: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

SCHEMA – working with this semester

ERD of the database used in 20203 for DBS311

Note and improvement might be to add to EMPLOYEES salary and commission percentMake more sales reps connect to a few more customers

document.docx by RT-- 16 September 2023 5 of 31

Page 6: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

3 (1-4) _ repeats DBS201 – and why?

3 ACTIONS ON TABLES  1 PROJECTION 2 SELECTION 3 JOINS Done through SELECT statement

SELECT – Retrieving data from a table

1 PROJECTION -- Retrieving specific columns of data such as ALL student names and phone numbers

2 SELECTION – Returns only rows that meet the specific restriction such as all male students from a table of students

document.docx by RT-- 16 September 2023 6 of 31

Page 7: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

3 JOIN – Returning data from 2 different tables such as course name and the student name

More on this in a later week’s lesson

document.docx by RT-- 16 September 2023 7 of 31

Page 8: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

FORMAT of the SELECT statement

document.docx by RT-- 16 September 2023 8 of 31

Page 9: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

SQL STYLEIt is important NOT to use run on sentences

SELECT LAST_NAME, FIRST_NAME, ADDRESS1, ADDRESS2, CITY, PROV, PCODE FROM EMPLOYEES WHERE UPPER(CITY) = UPPER (‘TORONTO’);

Vs

SELECT LAST_NAME, FIRST_NAME, ADDRESS1, ADDRESS2, CITY, PROV, PCODE FROM EMPLOYEES WHERE UPPER(CITY) = UPPER (‘TORONTO’);

OR

SELECT LAST_NAME, FIRST_NAME, ADDRESS1, ADDRESS2, CITY, PROV, PCODE

FROM EMPLOYEES WHERE UPPER(CITY) = UPPER (‘TORONTO’);

OR

SELECT LAST_NAME, FIRST_NAME, ADDRESS1, ADDRESS2, CITY, PROV, PCODE

FROM EMPLOYEES WHERE UPPER(CITY) = UPPER (‘TORONTO’);

More data later about style

document.docx by RT-- 16 September 2023 9 of 31

This and similar styles will not be marked and so will get zero

Page 10: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROJECTION –

DEMOUse the DESCRIBE command to see the structure of a specific table.

Left panel and click on table demo this

document.docx by RT-- 16 September 2023 10 of 31

Page 11: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

SELECTION --

SELECT department_id, location_idFROM departments;

Demo cut and paste for labsAlso fixed font -- courier new

document.docx by RT-- 16 September 2023 11 of 31

Can also do UPPER and LOWER case like this

Page 12: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

document.docx by RT-- 16 September 2023 12 of 31

MAIN POINT is readability

LAYOUT is important. Bad layout will have marks deducted.

Page 13: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

9

document.docx by RT-- 16 September 2023 13 of 31

Same as any programming language

Page 14: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

Note how it adds to the salary an additional 300

As in ALL languages it is important to know the order of operation when there are multiple operators

SELECT last_name, salary, salary + 300FROM employees;

Depending on table may have 54 rows or 107

document.docx by RT-- 16 September 2023 14 of 31

Page 15: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

Look at the EMPLOYEES tableWe will assume the salary value for now is monthly

Show 1) salary.

2) Show yearly salary after you raised everyone's monthly salary by $100

document.docx by RT-- 16 September 2023 15 of 31

This will be a major error for many

Page 16: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

11

Raise everyone’s salary (which we will assume is monthly) by 100 dollars then multiply it out by 12 to see yearly salary

Not a large difference for EACH employee.For the company with 20 employees that is 24,000Now if you have 1000 employees, that raise is 24 million.

Would be nice if we could total the salaries to see the results.

SELECT last_name, salary, 12*salary + 100FROM employees;

document.docx by RT-- 16 September 2023 16 of 31

288,100 is the result

289,200

Page 17: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

12

SELECT last_name, salary, commission_pctFROM employees;

Some outputRajs 3500 Davies 3100 Matos 2600 Vargas 2500 Zlotkey 10500 .2Abel 11000 .3Vargas 8600 .2Grants 7000 .15de Man 7000 .15Whalen 4400

document.docx by RT-- 16 September 2023 17 of 31

Page 18: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM for you to solve

SHOW1 Last name2 Salary3 Multiply the monthly salary by 12 to get yearly salary.Then multiply it by the percent commission earned to get to the full yearly earnings

document.docx by RT-- 16 September 2023 18 of 31

Page 19: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM1 Last name2 Salary3 Multiply the monthly salary by 12 to get yearly salary.Then multiply it by the percent commission earned to get to the full yearly earnings

SELECT last_name, salary, salary * 12 * commission_pctFROM employees;

THIS PROBLEM WILL HAPPEN TO YOU MANY TIMES

What Happens ….

document.docx by RT-- 16 September 2023 19 of 31

Page 20: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

SELECT last_name, salary * 12 * commission_pctFROM employees

NOTE: Column Names are not nice – need to fix it with an aliasLAST_NAME SALARY*12*COMMISSION_PCT ------------------------- ------------------------ King Kochhar De Haan Hunold Ernst Lorentz Mourgos Rajs Davies Matos Vargas Zlotkey 25200 Abel 39600 Taylor 20640 Grant 12600 Whalen Hartstein Fay Higgins Gietz

20 rows selected

document.docx by RT-- 16 September 2023 20 of 31

Page 21: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

NOTE: Alias names maximum length of 30

Try these.

SELECT last_name AS name, commission_pct as commFROM employees;

SELECT last_name AS "Last Name", commission_pct as commFROM employees;

-- This last one will not work as there is a space in the alias name

document.docx by RT-- 16 September 2023 21 of 31

Page 22: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM:

Display last name and job id from the employees table and look at the result

SELECT last_name, job_id AS "Employees"FROM employees;

CUT and PASTE to word or blackboard

Results are spread out and title Employees ---- in wrong location ……

LAST_NAME Employees------------------------- ----------Abel SA_REP Davies ST_CLERK De Haan AD_VP Ernst IT_PROG Fay MK_REP Gietz AC_ACCOUNT

WantedLAST_NAME Employees ------------------------- ---------- Abel SA_REP Davies ST_CLERK De Haan AD_VP

document.docx by RT-- 16 September 2023 22 of 31

Page 23: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

Change the look with a CONCATENATE OPERATOR

select last_name || job_id as "Employees"from employees;

STILL NOT NICE LOOKING ………. Need to improve it.

How ?????

ASIDE: There is a concatenate function available later

document.docx by RT-- 16 September 2023 23 of 31

Notice this works whereas on the iSeries it "appeared" to not work.

Page 24: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

ANSWER:

select last_name || ' ' || job_id as "Employees"from employees;

Employees ------------------------------------Abel SA_REPArmarillo SA_REPBergsteige SA_REPBrigade SA_REP

document.docx by RT-- 16 September 2023 24 of 31

Page 25: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM Question 4:Revise previous SQL require Last name and job id with the ‘is a’ between it but nicely displayedGive it a good title

Example:Ron is a Dean

(answer on next page)

document.docx by RT-- 16 September 2023 25 of 31

NOTE:

Single quotes for literals

Double quotes for alias names

Page 26: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

ANSWER Question 4:Revise previous SQL require Last name and job id with the ‘is a’ between it but nicely displayedGive it a good title

select last_name || ' is a ' || job_id as "Employees" from employees;

note the spacing in the quotesAlways look at your output carefully

document.docx by RT-- 16 September 2023 26 of 31

Page 27: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

ALTERNATE – do not recall ever seeing it used … but

SELECT last_name|| q'[ has the job: ]' || job_id FROM employees;

document.docx by RT-- 16 September 2023 27 of 31

Page 28: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM Question 5:A question from the consultant hired to get more sales.

How many departments does the company have? Using the employees table, display all the departments

document.docx by RT-- 16 September 2023 28 of 31

Page 29: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

ANSWER Question 5

SELECT department_id -- could use department names but then need a join

FROM employees;

Did you do this.Look carefully at the results, is that what the consultant really wanted?-- 54 rows

SELECT DISTINCT department_idFROM employees;

DEPARTMENT_ID ------------- 10 20 50 60 80 90 110

8 rows selected.

document.docx by RT-- 16 September 2023 29 of 31

See the duplicates as there are 20 rows displayed

Page 30: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

PROBLEM:Show the distinct salary and job id within department. Meaning if two people in department 20 have the same salary and job_id only show one of them.

ERROR see message … does not look like the error is correct

Error starting at line : 1 in command -SELECT DISTINCT (salary, job_id), department_idFROM employeesError at Command Line : 1 Column : 24 this usually will help youError report -SQL Error: ORA-00907: missing right parenthesis00907. 00000 - "missing right parenthesis"

WHY?

If you use DISTINCT, it must be for ALL selected columns

SELECT DISTINCT salary, job_id, department_idFROM employees

Correction will generate 42 rows

document.docx by RT-- 16 September 2023 30 of 31

Page 31: Welcome | ICT Seneca - School of Information ... · Web viewSTARTING UP 1 Open your Oracle and access your account. 2 Did you load the script that populates the database? 3 Open Blackboard

SQLPLUS command and NOT SQL

document.docx by RT-- 16 September 2023 31 of 31

This is not SQL it is a SQL*Plus command and does not need a semicolon