Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... ·...

84
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Sunnie Chung

Transcript of Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... ·...

Page 1: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 5

More SQL:

Complex

Queries,

Triggers, Views,

and Schema

Modification

Sunnie Chung

Page 2: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Chapter 5 Outline

� More Complex SQL Retrieval Queries

� Specifying Constraints as Assertions and Actions as Triggers

� Views (Virtual Tables) in SQL

� Schema Change Statements in SQL

Page 3: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

More Complex SQL Retrieval

Queries� Additional features allow users to specify

more complex retrievals from database:

� Nested queries, joined tables, outer joins,

aggregate functions, and grouping

Page 4: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Comparisons Involving NULL

and Three-Valued Logic� Meanings of NULL

� Unknown value

� Unavailable or withheld value

� Not applicable attribute

� Each individual NULL value considered to be different from every other NULL value

� SQL uses a three-valued logic:

� TRUE, FALSE, and UNKNOWN

Page 5: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Comparisons Involving NULL

and Three-Valued Logic (cont’d.)

Page 6: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Comparisons Involving NULL

and Three-Valued Logic (cont’d.)� SQL allows queries that check whether an

attribute value is NULL

� IS or IS NOT NULL

Page 7: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries, Tuples,

and Set/Multiset Comparisons� Nested queries

� Complete select-from-where blocks within

WHERE clause of another query

� Outer query

� Comparison operator IN, NOT IN, EXISTS, NOT EXISTS

� Compares value v with a set (or multiset) of

values V

� Evaluates to TRUE if v is one of the elements in

V

Page 8: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries (cont’d.)

Page 9: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries (cont’d.)

� Use tuples of values in comparisons

� Place them within parentheses

Page 10: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

� Use other comparison operators to compare a single value v

� = ANY (or = SOME) operator

• Returns TRUE if the value v is equal to some value

in the set V and is hence equivalent to IN

� Other operators that can be combined with ANY

(or SOME): >, >=, <, <=, and <>

Nested Queries (cont’d.)

Page 11: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries with ALL

Select Fname, Lname

From Employee E

Where E.Salary > ALL ( Select E2.Salary

From Employee E2

Where E2.Dno = 5 );

Select E1.Fname, E1.Lname

From Employee E1

Where E1.Salary > ( Select MAX(E2.Salary)

From Employee E2

Where E2.Dno = 5 );

Page 12: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries with ALL

Select E1.Fname, E1.Lname

From Employee E1

Where E1.Salary < ALL ( Select E2.Salary

From Employee E2

Where E2.Dno = 5 );

Select E1.Fname, E1.Lname

From Employee E1

Where E1.Salary < ( Select MIN(E2.Salary)

From Employee E2

Where E2.Dno = 5 );

Page 13: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries with Any

Select E1.Fname, E1.Lname

From Employee E1

Where E1.Salary > Any ( Select E2.Salary

From Employee E2

Where E2.Dno = 5 );

Select E1.Fname, E1.Lname

From Employee E1

Where E1.Salary < Any ( Select E2.Salary

From Employee E2

Where E2.Dno = 5 );

Page 14: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Nested Queries (cont’d.)

� Avoid potential errors and ambiguities

� Create tuple variables (aliases) for all tables

referenced in SQL query

Page 15: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Correlated Nested Queries

� Correlated nested query

� Evaluated once for each tuple in the outer

query

� Nested subquery evaluated multiple times:

Repeated for # of tuples in the outer table

Page 16: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The EXISTS and UNIQUE

Functions in SQL� EXISTS function

� Check whether the result of a correlated nested

query is empty or not

� EXISTS and NOT EXISTS

� Typically used in conjunction with a correlated

nested query

� SQL function UNIQUE(Q)

� Returns TRUE if there are no duplicate tuples in

the result of query Q

Page 17: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

IN / EXISTS

Select *

From Employee E

Where E.ssn IN (Select D.Essn

From Dependent D);

Only employees that have any dependent will be selected. --3 tuples

Select *

From Employee E

Where EXISTS (Select D.Essn

From Dependent D);

All the employees (whether they have any dependent or not) will be selected. -- 8 tuples

Page 18: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Exists with SubQuery

Select *

From Employee E

Where Exists (Select D.essn

From Dependent D); <------ WRONG !

All the employees (whether they have any dependent or not) will be selected as long as Dependent table has any tuple. -- 8 tuples

Select *

From Employee E

Where Exists (Select D.essn

From Dependent D

Where E.ssn = D.essn); ����----- Correct !

Only employees that have any dependent will be selected. --3 tuples

Page 19: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

IN with SubQuery

Select *

From Employee E

Where E.ssn IN (Select D.Essn

From Dependent D);

Only employees that have any dependent will be selected. --3 tuples

E.ssn IN (Select D.Essn From D) is same as adding E.ssn = D.Essn in the subquery as below.

Select *

From Employee E

Where E.ssn IN (Select D.Essn

From Dependent D

Where E.ssn = D.Essn);

Only employees that have any dependent will be selected. --3 tuples

Page 20: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Exists/Not Exists

with Correlated SubQuery Select *

From Employee E

Where Exists (Select D.essn

From Dependent D

Where E.ssn = D.essn);

Select *

From Employee E

Where Not Exists (Select D.essn

From Dependent D

Where E.ssn = D.essn);

Page 21: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Exists AND Not Exists

with Correlated SubQuery

Select *

From Employee E

Where Exists (Select Dp.essn

From Dependent Dp

Where E.ssn = Dp.essn)

And

Not Exists (Select D.mgrssn

From Department D

Where E.ssn = D.mgrssn);

Page 22: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

IN/NOT IN with Correlated SubQuery

Select *

From Employee E

Where E.Dno IN (Select D.Dnumber

From Department D

Where E.ssn = D.mgrssn);

Select *

From Employee E

Where E.Dno NOT IN (Select D.Dnumber

From Department D

Where E.ssn = D.mgrssn);

Page 23: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

SET Division

Q: Find the name of employees who work on all theprojects controlled by department number 5.

This can be expressed with Division in Relational Algebra:

EMP_PROJ / Projects_Houston_Pno

Page 24: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Using the Universal Quantifier in

Queries

Page 25: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Transforming the Universal and

Existential Quantifiers� Transform one type of quantifier into other

with negation (preceded by NOT)

� AND and OR replace one another

� Negated formula becomes unnegated

� Unnegated formula becomes negated

Page 26: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Q3A: Expressing Universal Quantifier For All P(x) Q3A: Expressing Universal Quantifier For All P(x) Q3A: Expressing Universal Quantifier For All P(x) Q3A: Expressing Universal Quantifier For All P(x)

with Existential Quantifier NOT EXISTS (~P(x)) in with Existential Quantifier NOT EXISTS (~P(x)) in with Existential Quantifier NOT EXISTS (~P(x)) in with Existential Quantifier NOT EXISTS (~P(x)) in

SQLSQLSQLSQL

Page 27: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Expressing Universal Quantifier For All

P(x) with NOT EXISTS (~P(x)) {e.LNAME, e.FNAME | EMPLOYEE (e) AND

((∀∀∀∀ x) (NOT (PROJECT(x)) OR NOT (x. DNUM=5))

OR ((∃∃∃∃ w) (WORKS_ON (w) AND w.ESSN=e.SSN AND x.PNUMBER=w.PNO)))}

SELECT e.fname, e.lname

FROM employee e

WHERE NOT EXISTS ( (SELECT pnumber

FROM project p

WHERE p.dnum = 5)

MINUS

(SELECT w. pno

FROM works_on w

WHERE e.ssn = w.essn));

Page 28: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Expressing Universal Quantifier For All

P(x) with NOT EXISTS (~P(x)) SELECT e.lname

FROM employee e, department d

WHERE e.sex=’F’ and e.ssn = d.mgrssn and

NOT EXISTS (SELECT *

FROM works_on w1

WHERE (w1.pno IN

(SELECT pnumber

FROM project

WHERE plocation=‘Stafford’)

AND

NOT EXISTS (SELECT *

FROM works_on w2

WHERE w2.essn = e.ssn

and w2.pno = w1.pno)));

Page 29: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Joined Tables in SQL and Outer

Joins� Joined table

� Permits users to specify a table resulting from

a join operation in the FROM clause of a query

� The FROM clause in Q1A

� Contains a single joined table

Page 30: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Joined Tables in SQL and Outer

Joins (cont’d.)� Specify different types of join

� NATURAL JOIN

� Various types of OUTER JOIN

� NATURAL JOIN on two relations R and S

� No join condition specified

� Implicit EQUIJOIN condition for each pair of

attributes with same name from R and S

� One join column (the same name) from Right

side table (S) is removed from the join result.

Page 31: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Regular Join (Comma Syntax)

Select *

From Employee E, Department D

Where E.dno = D.Dnumber

Returns:

Matching Tuples on E.dno = D.Dnumber with

All the Employee Columns

and

All the Department Columns

Page 32: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Joined Tables in SQL and Outer

Joins (cont’d.)� Inner join

� Default type of join in a joined table

� Tuple is included in the result only if a matching

tuple exists in the other relation

� LEFT OUTER JOIN

� Every tuple in left table must appear in result

� If no matching tuple

• Padded with NULL values for attributes of right table

Page 33: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Left Outer Join

Select D.dnumber, D.dname, E.dno, E.ssn

From Department D LEFT OUTER JOIN Employee E

On D.dnumber = E.dno;

Dnumber dname dno ssn

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

1 Headquarter 1 888665555

4 Admin 4 987654321

4 Admin 4 987987987

4 Admin 4 999887777

5 Research 5 123456789

5 Research 5 333445555

5 Research 5 453453453

5 Research 5 666884444

7 Automation NULL NULL

Page 34: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Outer Join

Select *

From Employee E

LEFT OUTER JOIN Dependent D

ON D.essn = E.ssn;

Returns:

All the columns of E with all the columns of D for the matching ssn with essn

+

All the columns of E with Null padded columns of D for the unmatching ssn with essn.

Page 35: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Outer Join

Select *

From Employee E

LEFT OUTER JOIN Dependent D

ON D.essn = E.ssn

Where D.relationship = ‘spouse’;

Returns

LOJ with ON condition D.essn = E.ssn first, then apply Where condition D.relationship = ‘spouse’.

Page 36: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Outer Join

Select *

From Employee E

LEFT OUTER JOIN Dependent D

ON D.essn = E.ssn AND

D.relationship = ‘spouse’;

Returns:

All the columns of E with all the columns of D for the matching ssn with essn AND D.relationship with spouse

+

All the columns of E with Null padded columns of D for the unmatching ssn and D.relationship.

Page 37: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Joined Tables in SQL and Outer

Joins (cont’d.)� RIGHT OUTER JOIN

� Every tuple in right table must appear in result

� If no matching tuple

• Padded with NULL values for the attributes of left

table

� FULL OUTER JOIN

� Can nest join specifications

Page 38: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Join with Using, Join with ON

Both allow joining of tables. The difference is that USING requires the join columns to have the same name:

select emp.ename, dept.dname from emp join dept using (deptno);

The ON version works also when the join columns have different names:

select emp.ename, emp2.ename manager_name

from emp join emp emp2 on (emp.mgr = emp2.empno);

An important difference is that the ON clause preserves the columns from each joined table separately, which the USING clause merges the columns from the joined tables into a single column.

Oracle

select department_name, city

from departments JOIN locations USING (location_id); -- specify the same column name -- for both of the tables for the join

select department_name, city

from departments dept join locations loc

on (d.location_id = l.id); -- specify different column names for the tables for the join.

Page 39: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Joined Tables in SQL and Outer

Joins (cont’d.)� RIGHT OUTER JOIN

� Every tuple in right table must appear in result

� If no matching tuple

• Padded with NULL values for the attributes of left

table

� FULL OUTER JOIN

� Can nest join specifications

Page 40: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aggregate Functions in SQL

� Used to summarize information from multiple tuples into a single-tuple summary

� Grouping

� Create subgroups of tuples before

summarizing

� Built-in aggregate functions

� COUNT, SUM, MAX, MIN, and AVG

� Functions can be used in the SELECTclause or in a HAVING clause

Page 41: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aggregate Functions in SQL

(cont’d.)� NULL values discarded when aggregate

functions are applied to a particular column

Page 42: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Grouping: The GROUP BY and

HAVING Clauses� Partition relation into subsets of tuples

� Based on grouping attribute(s)

� Apply function to each such group independently

� # of group = # of unique values in Group by Col

� GROUP BY clause

� Specifies grouping attributes

� Should appear in Select clause as well

� If NULLs exist in grouping attribute

� Separate group created for all tuples with a NULL

value in grouping attribute

Page 43: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aggregate Function Without

Group By Select R.a

From R

Where R.b > (Select COUNT (*)

From S

Where R.c = S.c)

Page 44: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aggregate Function in Having

Select R.a, R.b

From R Left Outer Join S

ON R.c = S.c

Group by R.a, R.b

Having R.b > MAX (S.b)

Page 45: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Aggregation with Group BY

Select E.dno, Count(*)

From Employee E, Department D

Where E.dno = D.Dnumber

Group By E.dno;Will return:

Dno Count(E.ssn)

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

1 1

4 3

5 4

Page 46: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By with Aggregation

Select D.dnumber, Count(E.dno)

From Department D Left Outer Join Employee E

ON D.Dnumber = E.dno

Group By D.dnumber;

Will return:

Dnumber Count(E.dno)

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

1 1

4 3

5 4

7 0

Page 47: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By with Aggregation

For Empty group,

Count(*) = 0,

MAX, MIN, AVE, SUM return NULL

Select D.dnumber, MAX(E.salary)

From Department D LEFT OUTER JOIN Employee E

ON D.Dnumber = E.dno

Group By D.dnumber;

Will return:

Dnumber MAX(E.salary)

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

1 55000

4 43000

5 40000

7 NULL

Page 48: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By with Having Condition

Select D.dnumber, Count(E.dno)

From Department D Left Outer Join Employee E

ON D.Dnumber = E.dno

Group By D.dnumber

Having Count(E.dno) > 2;

Will return:

Dnumber Count(E.dno)

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

4 3

5 4

Page 49: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Grouping: The GROUP BY and

HAVING Clauses (cont’d.)� HAVING clause

� Provides a condition on the summary

information

Page 50: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Select E.dno, Count(*)From Employee EGroup By E.dno;Will return:Dno Count(*)------ ---------------------1 1 4 35 4

SubQ of Q28: Select E.dno

From Employee EGroup By E.dno

Having COUNT(*) > 2;Dno------45

Q28: Select D.dnumber, Count(*)

From Employee E, Department D

Where D.Dnumber = E.dno and E.salary > 40000 and And (SubQ);

Dnumber Count(*)------------- ----------------------4 1

Page 51: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By Having in WHERECorrect Version Q28:Select Distinct E.ssn, E.lname

From Employee E, Department D

Where E.salary > 40000 and

E.Dno = D.Dnumber and

E.Dno in (Select E1.Dno

From Employee E1

Group By E1.Dno

Having COUNT(*) > 5);

Page 52: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By Having in WHERE(Subquery in WHERE)

SELECT LNAME

FROM EMPLOYEE E

WHERE E.SSN IN (SELECT ESSN

FROM WORKS_ON

GROUP BY ESSN

HAVING COUNT(PNO) >= 3);

Page 53: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Group By Having in FROM(Subquery in FROM)Select D.dnumber, Count(*)

From Employee E, Department D,

( Select E.dno

From Employee E

Group By E.dno

Having COUNT(*) > 2) as Temp(high_dno)

Where D.dnumber = E.dno and E.salary > 40000 and Temp.high_dno = e.dno

Group by D.dnumber;

Page 54: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Discussion and Summary of

SQL Queries

Page 55: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Example Query

(a) Query Q1

SELECT P.ins_class, Max(P.age)

FROM Patients P, Claims C

WHERE P.pid = C.pid AND P.age > 50

GROUP BY P.ins_class

HAVING COUNT (*) > 1;

Page 56: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Query Processing Semantics

SELECT P.ins_class, Max(P.age)

FROM Patients P, Claims C

WHERE P.pid = C.pid AND P.age > 50

GROUP BY P.ins_class

HAVING COUNT (*) > 1;

1. Construct a joined table of all the tables in the FROM list,and apply the predicates in WHERE.

2. Eliminate unwanted columns, Keep the columns listed inSELECT, GROUP-BY.

3. Sort the result by the columns in the GROUP BY clause toidentify the groups.

4. Apply the predicates in the HAVING clause to each group.

5. Continued with more steps following…

Page 57: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Processing Semantics - Continued

SELECT P.ins_class, Max(P.age)

FROM Patients P, Claims C

WHERE P.pid = C.pid AND P.age > 50

GROUP BY P.ins_class

HAVING COUNT (*) > 1;

5. Generate the answer row per the remaining groupcolumns generated by applying aggregate operators toeach group.

6. Eliminate the duplicates if there is DISTINCT in theSELECT clause.

Page 58: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Processing Semantics

Construct a joined table of all the tables in the FROM list, and applythe predicates in WHERE.

Eliminate unwanted columns, Keep the columns listed in SELECT,GROUP-BY.

Sort the result by the columns in the GROUP BY clause to identifythe groups.

Apply the predicates in the HAVING clause to each group.

Generate the answer row per the remaining group columnsgenerated by applying aggregate operators to each group.

Eliminate the duplicates if there is DISTINCT in the SELECTclause.

Page 59: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying Constraints as

Assertions and Actions as

Triggers� CREATE ASSERTION

� Specify additional types of constraints outside

scope of built-in relational model constraints

� CREATE TRIGGER

� Specify automatic actions that database

system will perform when certain events and

conditions occur

Page 60: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specifying General Constraints

as Assertions in SQL� CREATE ASSERTION

� Specify a query that selects any tuples that

violate the desired condition

� Use only in cases where it is not possible to use CHECK on attributes and domains

Page 61: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

What is checked in SubQ

NOT EXISTS ( Select *

From Employee E, Employee M, Department D

Where E.salary > M.salary and

E.dno = D.Dnumber and D.mgr_ssn = M.ssn);

1. To get each dept manager’s info:

Select *

From Department D, Employee M

Where D.mgr_ssn = M.ssn;

2. To get salary of each employee who is working for the dept whose manager is mgr_ssn to compare

Select *

From Employee E, Employee M, Department D

Where E.salary > M.salary and

E.dno = D.Dnumber and D.mgr_ssn = M.ssn;)

Page 62: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

SubQuery in Select

Select tbls.owner, tbls.table_name,

(Select count(column_name) as total_columns

From all_tab_columns Tabcols

Where Tabcols.owner = Tbls.owner and Tabcols.table_name =

Tbls.table_name) Subquery2

From all_tables Tbls;

Must return a single value.

Usually an aggregate function:

Min, Max, Ave, Sum, Count without Group By

Page 63: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Introduction to Triggers in SQL

� CREATE TRIGGER statement

� Used to monitor the database

� Typical trigger has three components:

� Event(s)

� Condition

� Action

Page 64: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Views (Virtual Tables) in SQL

� Concept of a view in SQL

� Single table derived from other tables

� Considered to be a virtual table

Page 65: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specification of Views in SQL

� CREATE VIEW command

� Give table name, list of attribute names, and a

query to specify the contents of the view

Page 66: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Altering Existing View

Replace View Existing_View_Name As

Select… From… Where…;

Replace View Works_On1 As

Select Fname, Lname, Pno, Pname, Hours

From Employee, Works_On, Project

Where Ssn = Essn and Pno = Pnumber

Page 67: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Specification of Views in SQL

(cont’d.)� Specify SQL queries on a view

� View always up-to-date

� Responsibility of the DBMS and not the user

� DROP VIEW command

� Dispose of a view

Page 68: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

View Implementation, View

Update, and Inline Views� Complex problem of efficiently

implementing a view for querying

� Query modification approach

� Modify view query into a query on underlying

base tables

� Disadvantage: inefficient for views defined via

complex queries that are time-consuming to

execute

Page 69: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

View Implementation

� View materialization approach

� Physically create a temporary view table when

the view is first queried

� Keep that table on the assumption that other

queries on the view will follow

� Requires efficient strategy for automatically

updating the view table when the base tables

are updated

Page 70: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

View Implementation (cont’d.)

� Incremental update strategies

� DBMS determines what new tuples must be

inserted, deleted, or modified in a materialized

view table

Page 71: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

View Update and Inline Views

� Update on a view defined on a single table without any aggregate functions

� Can be mapped to an update on underlying

base table

� View involving joins

� Often not possible for DBMS to determine

which of the updates is intended

Page 72: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

� Clause WITH CHECK OPTION

� Must be added at the end of the view definition

if a view is to be updated

� In-line view

� Defined in the FROM clause of an SQL query

View Update and Inline Views

(cont’d.)

Page 73: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Schema Change Statements in

SQL� Schema evolution commands

� Can be done while the database is operational

� Does not require recompilation of the database

schema

Page 74: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The DROP Command

� DROP command

� Used to drop named schema elements, such

as tables, domains, or constraint

� Drop behavior options:

� CASCADE and RESTRICT

� Example:

� DROP SCHEMA COMPANY CASCADE;

Page 75: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The ALTER Command

� Alter table actions include:

� Adding or dropping a column (attribute)

� Changing a column definition

� Adding or dropping table constraints

� Example:

� ALTER TABLE COMPANY.EMPLOYEE ADD

COLUMN Job VARCHAR(12);

� To drop a column

� Choose either CASCADE or RESTRICT

Page 76: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

The ALTER Command (cont’d.)

� Change constraints specified on a table

� Add or drop a named constraint

Page 77: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

GRANThttps://msdn.microsoft.com/en-us/library/ms188371.aspx

SQL GRANT is a command used to provide access or privileges on the database objects to the users.

The Syntax for the GRANT command is:

GRANT privilege_name ON object_name TO {user_name |PUBLIC |role_name} [WITH GRANT OPTION];

privilege_name is the access right or privilege granted to the user. Some of the access rights are ALL, CREATE TABLE, DROP TABLE, EXECUTE, and SELECT, INSERT/DELETE/UPDATE.

object_name is the name of an database object like TABLE, VIEW, STORED PROC and SEQUENCE.

user_name is the name of the user to whom an access right is being granted.

PUBLIC is used to grant access rights to all users.

ROLES are a set of privileges grouped together.

WITH GRANT OPTION - allows a user to grant access rights to other users.

Example:

GRANT Select ON Employee TO PUBLIC WITH GRANT OPTION;

GRANT SELECT ON Person.Address TO [AdventureWorks2012\RosaQdM];

Page 78: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

REVOKE

The REVOKE command removes user access

rights or privileges to the database objects.

The Syntax :

REVOKE privilege_name

ON object_name

FROM {user_name |PUBLIC |role_name};

Example:

REVOKE Select ON Employee FROM Public;

Page 79: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

COMMIT

Use the COMMIT statement to end your current transaction and make permanent all changes performed in the transaction.

A transaction is a sequence of SQL statements that a Database treats as a single unit

This statement also erases all savepoints in the transaction and releases transaction locks.

Page 80: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

COMMIT

Until you commit a transaction:

You can see any changes you have made during the transaction by querying the modified tables, but other users cannot see the changes.

After you commit the transaction, the changes are visible to other users' statements that execute after the commit.

You can roll back (undo) any changes made during the transaction with the ROLLBACK statement

Page 81: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

COMMIT

Example:

USE AdventureWorks2012;

GO

BEGIN TRANSACTION;

GO

DELETE FROM HumanResources.JobCandidate WHERE JobCandidateID = 13;

GO

COMMIT TRANSACTION;

GO

Page 82: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

RollBack

The ROLLBACK statement is the inverse of the COMMIT statement.

It undoes some or all database changes made during the current transaction

Page 83: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Rollback with TransactionUSE tempdb;

GO

CREATE TABLE ValueTable ([value] int;)

GO

DECLARE @TransactionName varchar(20) = 'Transaction1';

--The following statements start a named transaction, --insert two rows, and then roll back --the transaction named in the variable @TransactionName.

--Another statement outside of the named transaction inserts two rows.

--The query returns the results of the previous statements.

BEGIN TRAN @TransactionName

INSERT INTO ValueTable VALUES(1), (2);

ROLLBACK TRAN @TransactionName;

INSERT INTO ValueTable VALUES(3),(4);

SELECT [value] FROM ValueTable;

DROP TABLE ValueTable;

--Results --value

------------- --3

--4

Page 84: Elmasri 6e Ch05AddedAllSetDivision Nov2016eecs.csuohio.edu/~sschung/cis430/Elmasri_6e... · 2016-11-12 · Title: Microsoft PowerPoint - Elmasri_6e_Ch05AddedAllSetDivision_Nov2016

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Summary

� Complex SQL:

� Nested queries, joined tables, outer joins,

aggregate functions, grouping

� CREATE ASSERTION and CREATE TRIGGER

� Views

� Virtual or derived tables