18
Oracle’s take on joins Where it differs from ANSI standard

Oracle’s take on joins Where it differs from ANSI standard

Embed Size (px)

Citation preview

Page 1: Oracle’s take on joins Where it differs from ANSI standard

Oracle’s take on joins

Where it differs from ANSI standard

Page 2: Oracle’s take on joins Where it differs from ANSI standard

ANSI Standard SQL join

Select supplier_name,

Stock_description

from supplier join stock on supplier.supplier_id = stock.supplier_id;

• This also works in Oracle 10g, but Oracle’s native method is…

Page 3: Oracle’s take on joins Where it differs from ANSI standard

Oracle SQL join

Select supplier_name,

Stock_description

from supplier, stock where supplier.supplier_id = stock.supplier_id;

• If you followed the relational algebra discussion in Semester 1, you will know that this is a selection and a projection on a Cartesian product

Page 4: Oracle’s take on joins Where it differs from ANSI standard

This makes it easy…

• To do an inequality join:– Select

Page 5: Oracle’s take on joins Where it differs from ANSI standard

Outer join

• This shows us all the data from one table and anything that joins it from another table:– E.g. all the customers and any orders they

have made.– Or All the suppliers and any stock they

supply.

Page 6: Oracle’s take on joins Where it differs from ANSI standard

Standard outer join

• Select customer_name, corderno from customer left join corder on customer.customer_id = corder.customer_id;

• These will work in Oracle, but Oracle prefers:

Page 7: Oracle’s take on joins Where it differs from ANSI standard

Oracle outer join

• Select customer_name, corderno from customer, corder where customer.customer_id(+) = corder.customer_id;– The (+) is on the side that may return nulls.

Page 8: Oracle’s take on joins Where it differs from ANSI standard

Exercise

• Write the query to return all supplier names and the description of the stock they supply, from the builder schema.– Using ‘left join’– Using (+)

Page 9: Oracle’s take on joins Where it differs from ANSI standard

Non-equality joins

• These concern joins that are NOT necessarily based on referential integrity

• E.g.– Select all stock for a given supplier (equi-join)

where the stock value (i.e. unit_price * stock_level) is less than the amount owed to the supplier (non-equi-join)

– Try this in Oracle.

Page 10: Oracle’s take on joins Where it differs from ANSI standard

Self-join

• See the employee example in Tutorial week 11 semester 1, where the employees table has ‘reports to’ field, indicating another row in the same table.

Page 11: Oracle’s take on joins Where it differs from ANSI standard

Substitution variables

• In order to force data entry, a ‘substitution variable’ can be used.

• This is done by placing an ampersand & in front of the variable name. When SQL*Plus interprets the line, it immediately requests data entry from the userSQL> select * from builder.customer where customer_id

= &cid;– SQL* plus will respond by:

Enter value for cid: 3

Page 12: Oracle’s take on joins Where it differs from ANSI standard

Further uses for substitution variables

• Substitution variables can be used to replace actual parameters in a procedure call, or even fields:

• E.g.

SQL> Select &fld1,&fld2 from builder.customer;

Enter value for fld1: ‘customer_id’

Enter value for fld2: ‘customer_name’

Page 13: Oracle’s take on joins Where it differs from ANSI standard

Things to remember

• When entering values for substitution variables, the same rules apply as when using inserts:– Numerics can be entered without quotes– Strings require single quotes– Dates should be surrounded by single quotes.

Page 14: Oracle’s take on joins Where it differs from ANSI standard

Use of ‘ACCEPT’

• If you want to change the prompt, you may use the ‘Accept’ command. This command:– Sets up a variable that retains its value for the entire

session– Doesn’t use an ampersand (&)– Allows the programmer to design the prompt. E.g.

ACCEPT st_code PROMPT: ‘Please enter a 4-character stock code, surrounded by single quotes’;

Page 15: Oracle’s take on joins Where it differs from ANSI standard

Script files

A script file is an anonymous block.

It can be opened and executed, without being saved to the database.

Often, you need to run a sequence of SQL statements together, to select, update, insert or delete rows.

Sometimes, when a new row is being added, it is necessary to generate a key (e.g. each new student gets a new student number)

Page 16: Oracle’s take on joins Where it differs from ANSI standard

Declaring sequences• A sequence is an object that is defined in the database and can be

incremented whenever a new value (such as a key) is needed.• The sequence is created and given a name, an increment, a start

value and a possible maximum value.• E.g.

Create SEQUENCE empIdSeqIncremented by 1Start with 100Maxvalue 400;

• This creates an object empIdSeq of type Sequence in the database.• To get rid of it, you must DROP SEQUENCE empIdSeq• To use it:

Insert into staff values (empIdSeq.nextval,’Harry’,’Foreman’);

• To see what the current value is;Select empIdSeq.currval from DUAL;

Page 17: Oracle’s take on joins Where it differs from ANSI standard

The full syntax for a sequence

Create sequence sequencenameIncrement by nMaxvalue x / NoMaxValueMinvalue m / nominvalueCycle / nocycle when max is reached, start at min again / not

Cache c / nocache Oracle pre-generates and saves numbers in main memory. / not

Order / noorder The numbers are generated in chronological order / not

Page 18: Oracle’s take on joins Where it differs from ANSI standard

Creating Indexes

• Sometimes it is necessary to look up data in an order that is not related to the key.

• To do this, an index can be used:• E.g.

CREATE INDEX ORDDATE ON SORDER (DELIVEREDDATE);

This allows us to look up the orders by the date on which they were delivered. If I were in charge of taking in orders, this index may suit me very well.

– Only use indexes when considered absolutely necessary. Every insert or delete must update any indexes that are declared for the table.