ch 7 continued. substrings returns substrings format: substr(stringvalue, m, n) where m is the...

32
Ch 7 Continued

Upload: brendan-carpenter

Post on 04-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Ch 7 Continued

Page 2: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Substrings

Returns substrings

Format:

Substr(stringvalue, m, n)

Where m is the starting value and n is the length of characters (count)

Page 3: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Assume orders have the format:”

Abc1234

cdf2345etc..

Get the first and last part of the order

Select substr (order_numb, 1,3),substr (order_numb,4,4)

From order;

Page 4: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

INSTR function

Allows searching for a string of characters, gives the position of the string but does Not cut off anything

Format:

Instr(string, set, start,occurrence)

Start is the start of the search set in the string

Occurrence is the position of occurrence that you want to search

Page 5: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Search for first “/” in p_code

SQL> select p_code, instr(p_code,'/') from product;;

• P_CODE INSTR(P_CODE,'/')• -------- -----------------• 11QER/31 6• 13-Q2/P2 6• 14-Q1/L3 6• 1546-QQ2 0• 1558-QW1 0• 2232/QTY 5• 2232/QWE 5• 2238/QPD 5• 23109-HB 0• 23114-AA 0• 54778-2T 0

• P_CODE INSTR(P_CODE,'/')• -------- -----------------• 89-WRE-Q 0• PVC23DRT 0• SM-18277 0• SW-23116 0• WR3/TT3 4

• 16 rows selected.

Page 6: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

JOINING TABLES

when information needed is in more than one table, we need to join tables;

WHERE clause in the select SQL statement creates a join. Note some queries can also be answered using sub query

Page 7: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Rules FOR joining

WHERE attribute1 condition attribute2

Ex: where employee.ssn=student.ssn

Value(s) from one table are matched value(s) from other tables all matching values are attached

allows joining of tables based on common attribute domains

without the WHERE clause it will produce a Cartesian product also

Page 8: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Give the names of salesperson and their customers in maryland

SQL>Select cust_name, Sales_name from Customer C, salesperson Swherec.sales_numb= s. sales_numb’AndUpper(c.cust_st) =‘MD’;

C & S are aliases for tables Customer and Salesperson respectively

Page 9: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Types of joins (see page 294)

• Outer join– Left– Right– Full

Format:– Select From table1 LEFT [outer] join table 2

ON Joint condition– Select from table1 RIGHT join table2 ON join-

condition

Page 10: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Give the customer order information of all customers also

include customers that do not have orders Select *

From customer left join order on customer.cust_numb=order.cust_numb;

Give the salesperson’s information of all that have customers also include salesperson’s that do not have orders

Select *From customer right join salesperson on

salesperson.sales_numb=.customer.salse_numb;

Page 11: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Correlated subqueries

• It executes once for each row in the outer query

• Like a nested loop

Normal subqueryFor X = 1 TO 2PRINT “X = “XEND

Page 12: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Normal sub queryFor X = 1 TO 2PRINT “X = “XENDCORRELATED SUB QUERY:FOR X = 1 TO 2

FOR Y = 1 TO 3PRINT “X= ‘, “Y= ‘YEND

END

Page 13: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Get the names of salespeople that have earned more commission than average commission of all salespeople

Select sales_name

From salesperson

Where commission >(select avg (commission) from salesperson);

Get the names of salespeople that have earned more commission than average commission of all salespeople from that state

Select sales_name

From salesperson sa

Where commission > (select avg (commission) from

Salesperson where ????? = ?????));

Page 14: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

INDEX (p 181..B&F)

An index creates a separate file, that has two columns

• 1st col contains the value and• 2nd col contain primary key or record#

index are used to optimize query access– unique vs non-unique– take space

• automatically updated every time some changes are made to affected data.

Page 15: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Format:• CREATE [UNIQUE] INDEX index-name ON

table-name(col name)

Ex:

Create index sales_name_index on slaesperson (sales_name)

• DROP INDEX index-name• Drop index sales_name_index;

Page 16: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Security

two ways to control access internally:

• VIEWS

• GRANT

• VIEWS.. already discussed (session 4)

Page 17: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

GRANT/REVOKE

Privileges:System User

User Privileges:• A user can grant privileges to anything that is in

their account or they ownFormat:• GRANT object privilege [cols,] ON object to user

[WITH GRANT OPTION];• Table name MUST be preceded by the owner's

user name

Page 18: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Student XYZ (ordb022)grants access to student YYZ (acct: ordb021)

• GRANT SELECT, UPDATE ON STUDENT TO ORDB021;

This will allow ORDB021 TO ACCESS STUDENT table of ORDB022. Privilege is granted by ORDB022 from his/her account.

• However, when ORDB021 attempts to access it from his/her account as:

• SELECT * FROM STUDENT; (assuming no STUDENT table in his/her account) will give an error message.

• Why?

Page 19: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Problem is table is not on user ordb021 account

Must qualify it like in the trial exercise.

Easier to create synonyms

Create synonym STU FOR ORDB022.STUDENT;

Page 20: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Revoke

• REVOKE object privileges ON object FROM user;

REVOKE select on STUDENT from ORDB021;

• Will take select privileges from ORDB021 related to table STUDENT of ORDB222

Page 21: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Procedural SQL

SQL does NOT support IF – THEN –ELSEorDO WHILE – END

Use persistent Stored module (PSM)• A block of code that has sql and extension • This can be shared by multiple users• Like a module

Page 22: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

ORACLE implementation

Through PL/SQLCan be invoked by users

Format:Declare

<dec section>BEGIN

<execuable section>END; Declare: defines/initialize variablesExecutable commands: IF_THEN_ELSE, LOOPS

Page 23: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

example:DECLARE

pi constant NUMBER (9,7) :=3.1415926;//declares a variable pi of constant value of size (9,7)radius INTEGER (5); //variable radius of integer typearea NUMBER (14,2);

BEGINradius :=3 //assigns an initial value to variable radiusarea = :=pi*power(radius,2); Insert into AREAS values (radius, area);//inserts value in table AREAS

END; /

Page 24: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Cursor can be used for multiple entries (get data from another table)BF(ch 21) & An Introduction to PL/SQL by Loney & koch)

DECLARE pi constant NUMBER (9,7) :=3.1415926;//declares a variable pi of area NUMBER (14,2);

Cursor rad_cursor isselect * from RADIUS_VAL;

Rad_val rad_cursor%ROWTYPE;

BEGINOpen rad_cursor;

Fetch rad_cursor into rad-valradius :=3

area = :=pi*power(radval.radius,2);

Insert into AREAS values (radval.radius, area);

Close rad_cursor;END; /

Page 25: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

triggers

Like an alarm clock

It is automatically invoked by an event, i.e.

Add

Delete

Modify

Can be used to enforce rules that can not be enforced in design, generate derived columns

Page 26: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Format:(SEE PAGE 330-333)

Create or replace trigger trigger_name[before/after] [delete/insert/update of col_name] on

table_name[for each rwo][declare]

[var name data type etc..]BEGINPL/SQL INTERFACEEND;

Page 27: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Ch 9: DBD

Page 28: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

SDLC

Plan

Analysis

Design

Implement

maintenance

Page 29: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

DBLC (page 365)

• Initial studyDefine objectives/scope

• Database design..

Data Analysis & Requirements:

Develop conceptual model (ERD)..DBMS independent

Create logical (translate ERD into tables,views etc)..DBMS dependent

Physical design (storage structure, access method, optimization..)

Hardware dependent (page 383)

Page 30: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

ImplementationCreate DBMSLoad dataSecurityBack ups and recoverystandards

• Test & evaluationrobust

• OperationIs it doing what it is supposed to do?

Page 31: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

Maintenance

• Audits

• Back ups

• disaster planning

• Usage statistics

Page 32: Ch 7 Continued. Substrings Returns substrings Format: Substr(stringvalue, m, n) Where m is the starting value and n is the length of characters (count)

design

• Top down vs bottom up

• Centralized vs decentralized