Agenda for 02/16/2006

19
Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options available via SQLPlus. Learn how to use more than one table in a query. Discuss how DBMS processes multiple tables. Explain the different types of joins. Describe join conditions.

description

Agenda for 02/16/2006. Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options available via SQLPlus. Learn how to use more than one table in a query. Discuss how DBMS processes multiple tables. - PowerPoint PPT Presentation

Transcript of Agenda for 02/16/2006

Page 1: Agenda for 02/16/2006

Agenda for 02/16/2006

• Answer any questions about SQL project.

Do you want to see any of the answers for the queries?

• Discuss additional formatting options available via SQLPlus.

• Learn how to use more than one table in a query.

Discuss how DBMS processes multiple tables.

Explain the different types of joins.

Describe join conditions.

Page 2: Agenda for 02/16/2006

Sample Database ERD

Emp

PK EmpID

Name Address City State Zip HireDate OfficePhone BillingRateFK1 MgrID

Time

PK,FK1 EmpIDPK ContractIDPK DateTimePK,FK2 WorkTypeID

Amount

Work

PK WorkTypeID

StdBillRate Description

allocates

is allocated to

is of

describes

manages

Assume referential integrity is NOT maintained in this database

Page 3: Agenda for 02/16/2006

Accessing data from multiple tables

• Why do you want to access data from multiple tables in a single query?

To provide more complete information in a result table.

To support decision making.

• What happens when multiple tables are accessed in a single query?

Page 4: Agenda for 02/16/2006

Vocabulary Words

Select List

Result Table

Join

Cartesian Product/Cross Join

Join Condition

Multiple Table Access

SELECT *FROM emp,

time;

Page 5: Agenda for 02/16/2006

Eliminate columns to enhance readability

SELECT name,

contractid,

datetime,

amount/60

FROM emp,

time;

Does this SQL code produce a reasonable response?

Let’s eliminate some columns from the select list to make a more readable result table.

Page 6: Agenda for 02/16/2006

Combining Tables Based on a Shared Column Option 1 (older syntax - using WHERE clause)

SELECT name,contractid,datetime,amount/60

FROM time,emp

WHERE time.empid = emp.empid;

The shared column is usually the foreign key that sustains the relationship between the tables on the ERD.

Page 7: Agenda for 02/16/2006

Combining Tables Based on a Shared Column Option 2 (newer syntax – using join condition)

SELECT name,contractid,datetime,amount/60

FROM timeINNER JOIN empON time.empid = emp.empid

Page 8: Agenda for 02/16/2006

Combining tables on a shared column that has the Same name: Natural Join

SELECT name,contractid,datetime,amount/60

FROM timeNATURAL JOIN emp

Page 9: Agenda for 02/16/2006

Viewing all rows from one of the tables in a join Option 1 (older syntax using WHERE clause)

Syntax for a left outer-join in Oracle:

SELECT name,time.empidcontractid,datetime,amount/60

FROM time,emp

WHERE time.empid = emp.empid (+)

All data in the table on the left side of the condition will be displayed because the plus sign (+) is on the right side of the condition.

Page 10: Agenda for 02/16/2006

Viewing all rows from one of the tables in a join Option 2 (newer syntax using join condition)

SELECT name,time.empidcontractid,datetime,amount/60

FROM timeLEFT OUTER JOIN empON emp.empid = time.empid

Imagine that the time table is “left” (because it is declared first) and the emp table is “right” (because it is declared second)

Page 11: Agenda for 02/16/2006

Viewing all rows from the other table in a join Option 1 (older syntax using WHERE clause)

Syntax for a right outer-join in Oracle:

SELECT name,time.empidcontractid,datetime,amount/60

FROM time,emp

WHERE time.empid (+) = emp.empid

Page 12: Agenda for 02/16/2006

Right outer join – Viewing all rows in other table Option 2 (newer syntax using join condition)

SELECT name, time.empid, contractid, datetime, amount/60

FROM timeRIGHT OUTER JOIN empON time.empid = emp.empid;

Remember that the time table is “left” (because it is declared first) and the emp table is “right” (because it is declared second)

Page 13: Agenda for 02/16/2006

Full outer join – Viewing all rows in both tables Option 2 (newer syntax using join condition –

option 1 is not available)

SELECT name, time.empid, contractid, datetime, amount/60

FROM timeFULL OUTER JOIN empON time.empid = emp.empid;

Page 14: Agenda for 02/16/2006

SELECT emp.name,time.empid,time.worktypeid,work.description,datetime,amount/60

FROM timeINNER JOIN empON time.empid = emp.empidINNER JOIN workON time.worktypeid = work.worktypeid;

Combining more than two tables into a single result table

Page 15: Agenda for 02/16/2006

Displaying all data in TIME table

COLUMN emp_name heading “Employee Name”COLUMN descr heading “Type of Work”

SELECT NVL(emp.name,'NOT IN EMPLOYEE TABLE') emp_name, time.empid, time.worktypeid, NVL(work.description,'NOT IN WORK TABLE’) descr, datetime, amount/60

FROM timeLEFT OUTER JOIN empON time.empid = emp.empidLEFT OUTER JOIN workON time.worktypeid = work.worktypeidORDER BY emp.name;

Page 16: Agenda for 02/16/2006

Summarizing Data

COLUMN descr heading “Type of Work”SELECT NVL(work.description, ‘No Description’) descr,

round(sum(amount/60),2)FROM timeLEFT OUTER JOIN workON work.worktypeid = time.worktypeid GROUP BY work.description;

The GROUP BY statement is frequently used with a result table created by

multiple underlying tables

Page 17: Agenda for 02/16/2006

Recursive Relationship with Employee to Assign Manager

Emp

PK EmpID

Name Address City State Zip HireDate OfficePhone BillingRateFK1 MgrID

Time

PK,FK1 EmpIDPK ContractIDPK DateTimePK,FK2 WorkTypeID

Amount

Work

PK WorkTypeID

StdBillRate Description

allocates

is allocated to

is of

describes

manages

Page 18: Agenda for 02/16/2006

Self-join also called a recursive joinOption 1 – (older syntax using WHERE clause)

SELECT worker.empid "worker#",worker.name "worker name",manager.empid "manager#",manager.name "manager name"

FROM emp worker,emp manager

WHERE worker.mgrid = manager.empid;

What change is necessary to make all employees appear whether there is a

manager?

Page 19: Agenda for 02/16/2006

Self-join also called a recursive joinOption 2 – (newer syntax using join condition)

SELECT worker.empid "worker#",worker.name "worker name",manager.empid "manager#",manager.name "manager name"

FROM emp workerINNER JOIN emp managerON worker.mgrid = manager.empid;

What change is necessary to make all employees appear whether there is a manager?