Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... •...

6
1 Chapter 8B Objectives: to learn • to create and use updatable views • to use the advanced SQL JOIN operator syntax Outline • Updatable Views • SQL Join Operators – Recursive Joins & other special join types – ANSI standard syntax • Join Using • Join On – Left & Right Outer Joins 1 Fall 2010- CS275 Updatable Views • Updatable view is a view that can be used to update attributes in the base tables • Updating multiple tables as one transaction. – Oracle Update allows only one table or view on the Update. – A View can create a single transaction for updating more than one table – Create a view with columns to update from each table. – Often done as Batch Update Routines • non-event driven by user • done when system is off-line 2 Fall 2010- CS275 Updatable Views • Not all views are updatable – No GROUP BY expressions or aggregate functions – Cannot use set operators (union, minus, intersect) – Most restrictions are based on use of JOINs • Simple joins are ok. Example: Creating a view of the Product & Sales tables 3 Fall 2010- CS275 Updatable Views • Creating a view of the Product & Sales tables: Create view PSVIEW as (select m.prod_id, m.prod_qoh, s.ps_qty from prodmaster m, prodsales s where m.prod_id = s.prod_id); • To list your view objects Select object_name, object_type from user_objects where object_type in (‘view', ‘table'); 4 Fall 2010- CS275

Transcript of Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... •...

Page 1: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

1

Chapter 8B

Objectives: to learn

• to create and use

updatable views

• to use the advanced

SQL JOIN operator

syntax

Outline

• Updatable Views

• SQL Join Operators– Recursive Joins &

other special join types

– ANSI standard syntax• Join Using

• Join On

– Left & Right Outer

Joins

1Fall 2010- CS275

Updatable Views

• Updatable view is a view that can be used to

update attributes in the base tables

• Updating multiple tables as one transaction.– Oracle Update allows only one table or view on the

Update.

– A View can create a single transaction for updating

more than one table

– Create a view with columns to update from each

table.

– Often done as Batch Update Routines • non-event driven by user

• done when system is off-line

2Fall 2010- CS275

Updatable Views

• Not all views are updatable

– No GROUP BY expressions or aggregate functions

– Cannot use set operators (union, minus, intersect)

– Most restrictions are based on use of JOINs

• Simple joins are ok.

Example: Creating a view of the Product & Sales tables

3

Fall 2010- CS275

Updatable Views

• Creating a view of the Product & Sales tables:

Create view PSVIEW as

(select m.prod_id, m.prod_qoh, s.ps_qty

from prodmaster m, prodsales s

where m.prod_id = s.prod_id);

• To list your view objects

Select object_name, object_type

from user_objects

where object_type in (‘view', ‘table');

4Fall 2010- CS275

Page 2: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

2

Updatable Views

• Describe it’s columnsSQL> describe PSView

Name Null? Type----------------------------------------- --------------------PROD_ID NOT NULL VARCHAR2(4)PROD_QOH NUMBER(5,2)PS_QTY NUMBER(5,2)

• Check the system catalog for the column’s

updatable status.SQL> SELECT COLUMN_NAME, UPDATABLE, INSERTABLE, DELETABLE

FROM USER_UPDATABLE_COLUMNS

WHERE TABLE_NAME = 'PSVIEW';

COLUMN_NAME UPD INS DEL

-------------------------- ----- ----- ----

PROD_ID YES YES YES

PROD_QOH YES YES YES

PS_QTY YES YES YES

5Fall 2010- CS275

Review Database Table Joins

• Ability to combine (join) tables on common

attributes is the most important distinction

between a relational database and other

databases

• Join is performed when data are retrieved from

more than one table at a time

• Join is generally composed of an equality

comparison between the foreign key and the

primary key of related tables

6

Fall 2010- CS275

Review Joining Database Tables

• Joining Tables

– The WHERE clause shows the join of the foreign

key (in the master table) to the matching

(primary) key in the detail table.

– Designing the Query

• Start by first listing all output data including

calculations, and then relationships between tables

• Eliminate relationships unnecessary for selecting

the columns needed for the output

• When in doubt, list all relations on the where

clause.

7

Fall 2010- CS275

Review Joining Database Tables

• Inner or Natural Join

– “Whatever rows in A that match in B”

– Most common form of Joining data

– Must use the Where Clause

• When joining multiple tables, the # of join

conditions = 1 less than the # of tables on the

from clause.

• Joining takes place left to right as the tables are

listed on the From clause.

8Fall 2010- CS275

Page 3: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

3

Review Joining Database Tables

• Outer Joins – additionally select rows with no

matching values in other related table

– Left or Right

• Returns not only matching rows, but also rows with

unmatched attribute values for one table (Left or

Right as specified)

– Full

• Returns rows matching join condition AND also

rows from both tables with unmatched values

– values for the unmatched columns would be left

blank or null.

9Fall 2010- CS275

Review Specialty named Joins

• Equi Join– Where the join condition uses =

• Theta Join– Where the join condition uses a comparison that

isn’t =, but a >, <, != or a combination of >=, or <=.

• Recursive or Self Join– Joining a Table to itself

– Matching column in Table A to another column

within Table A, ie. Both sets are the same.

– Example:Select T1.ename ||’ works for ‘|| T2.ename

from emp T1, emp T2

where T1.mgr = T2.emp_no;10Fall 2010- CS275

Review Recursive or Self Join

• Aliases are used to differentiate the table from

itself when a table is joined recursively

select e.emp_num, e.emp_lname, e.emp_mgr, m.emp_lname

from employee e, employee mwhere e.emp_mgr = m.emp_num

order by e.emp_mgr;

11Fall 2010- CS275

ANSI Standard Join Syntax

SQL99 - SQL Join Operators

12

Fall 2010- CS275

Page 4: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

4

ANSI Standard Join Syntax

Cross Join

• Cartesian product of two tables

– Usually done by mistake

– No joining criteria (missing where clause)

Select * from charter, aircraft;

• New Syntax:Select col_a, col_b, col_c

from table1 CROSS JOIN table2;

• Old SyntaxSelect col_a, col_b, col_c

from T1, T2;

13

Fall 2010- CS275

ANSI Standard Join Syntax

Natural Join

• Returns rows with matching values in matching

columns

– Eliminates duplicate columns

– Tables share one or more common attributes with

common names

• New syntax

Select * from t1 natural join t2;

• Old Syntax

Select * from t1, t2 where t1.pk = t2.pk;

14

Fall 2010- CS275

ANSI Standard Join Syntax

Natural Join examples

• New syntax examples

Select cus_code, cus_lname,

inv_number, inv_date

from customer

natural join invoice;

Select inv_number, p_code,

p_descript, line_units,

line_price

from invoice natural join line

natural join product;

15Fall 2010- CS275

ANSI Standard Join Syntax

Join using

• Returns rows with matching values in Using

clause columns

• Join attribute have the same name and data type

in both tables.

16Fall 2010- CS275

Select inv_number, p_code,

p_descript, line_units,

line_price

from invoice join line

using(inv_number)

join product

using (p_code);

Page 5: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

5

ANSI Standard Join Syntax

JOIN ON Clause

• Used when tables have no common attributes

• Returns only rows that meet the join condition

– Typically includes equality comparison expression

of two columns

• Syntax: SELECT column-list FROM table1 JOIN

table2 ON join-condition

17Fall 2010- CS275

Select i.inv_number, p_code,

p_descript, line_units, line_price

from invoice i join line on

i.inv_number=line.inv_number

join product on

line.p_code = product. p_code;

ANSI Standard Join Syntax

Left Outer Join• Returns not only matching rows, but also rows with

unmatched attribute values for the left table

select p_code, v.v_code, v_name

from vendor v

left join product p

on v.v_code =product.v_code;

Older syntax

select p_code, v.v_code, v_name

from vendor v, product p

where v.v_code = p.v_code(+);

18Fall 2010- CS275

ANSI Standard Join Syntax

Outer Left Join Example

19Fall 2010- CS275

Select p_code, v.v_code, v_name

from vendor v left join product p

on v.v_code = product.v_code;

ANSI Standard Join Syntax

Right Outer Join• Returns matching rows, and also rows with

unmatched attribute values for the right table

select p_code, v.v_code, v_name

from vendor v right join product

on v.v_code = product.v_code

order by v.v_code;

Older syntax

select p_code, v.v_code,v_name

from vendor v, product p

where v.v_code(+) = p.v_code;

20Fall 2010- CS275

Page 6: Chapter 8B Updatable Views · –Oracle Update allows only one table or view on the ... • EquiJoin –Where the join condition uses = • Theta Join ... • Cartesian product of

6

ANSI Standard Join Syntax

Right Outer Join Example

21

Fall 2010- CS275

Select p_code, v.v_code, v_name

from vendor v right join product on

v.v_code = product.v_code;

ANSI Standard Join Syntax

FULL JOIN

• Full outer join returns both matched and

unmatched rows in the two tables.

• Not available with the older (+) syntax.

22

Fall 2010- CS275

Select p_code, v.v_code, v_name

from vendor v full join product

on v.v_code = product.v_code;

Summary

• Views of tables can be used to update those tables

under limited conditions.

• Operations that join tables are classified as inner

joins and outer joins

• ANSI Natural join is an inner join that returns all

rows with matching values in the matching

columns

– Eliminates duplicate columns

– Using clause specifies the join column name

– On clause specifies the join column when the

attribute names are different in the two tables.

• ANSI Outer Joins may be Left, Right, or Full joins.23Fall 2010- CS275