Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 3 Displaying Data from Multiple Tables.
Advertisements

Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Copyright س Oracle Corporation, All rights reserved. 14 Controlling User Access.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
Controlling User Access. Objectives After completing this lesson, you should be able to do the following: Create users Create roles to ease setup and.
Subqueries.
Subqueries.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.
4 Displaying Data from Multiple Tables Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Controlling User Access Asif Sohail University of the.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1 (SQL) Other Database Objects Asif Sohail University of the.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Copyright  Oracle Corporation, All rights reserved. 14 Controlling User Access.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Controlling User Access
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
CH3 Part2 Displaying Data from Multiple Tables (Join) Sub queries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 5 Sequences.
(SQL) Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from Multiple Tables (Join Sub queries Sub queries Sub queries Sub queries Creating Sequences Creating Sequences Creating Sequences Creating Sequences Creating Views Creating Views Creating Views Creating Views Dynamic SQL Queries Dynamic SQL Queries Dynamic SQL Queries Dynamic SQL Queries Controlling User Access Controlling User Access Controlling User Access Controlling User Access Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from Multiple Tables (Join Sub queries Sub queries Sub queries Sub queries Creating Sequences Creating Sequences Creating Sequences Creating Sequences Creating Views Creating Views Creating Views Creating Views Dynamic SQL Queries Dynamic SQL Queries Dynamic SQL Queries Dynamic SQL Queries Controlling User Access Controlling User Access Controlling User Access Controlling User Access CH3 Part2

Displaying Data from Multiple Tables (Join)

ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins View data that generally does not meet a join condition by using outer joins Join a table to itself Join a table to itself After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins View data that generally does not meet a join condition by using outer joins Join a table to itself Join a table to itself

EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. Obtaining Data from Multiple Tables EMPDEPT EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON

What Is a Join? Use a join to query data from more than one table. Use a join to query data from more than one table. Write the join condition in the WHERE clause. Write the join condition in the WHERE clause. Prefix the column name with the table name when the same column name appears in more than one table. Prefix the column name with the table name when the same column name appears in more than one table. Use a join to query data from more than one table. Use a join to query data from more than one table. Write the join condition in the WHERE clause. Write the join condition in the WHERE clause. Prefix the column name with the table name when the same column name appears in more than one table. Prefix the column name with the table name when the same column name appears in more than one table. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

Cartesian Product A Cartesian product is formed when: A Cartesian product is formed when: A join condition is omitted A join condition is omitted A join condition is invalid A join condition is invalid All rows in the first table are joined to all rows in the second table All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition in a WHERE clause. To avoid a Cartesian product, always include a valid join condition in a WHERE clause. A Cartesian product is formed when: A Cartesian product is formed when: A join condition is omitted A join condition is omitted A join condition is invalid A join condition is invalid All rows in the first table are joined to all rows in the second table All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition in a WHERE clause. To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

Generating a Cartesian Product ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. EMP (14 rows) DEPT (4 rows) EMPNOENAME...DEPTNO KING BLAKE MILLER EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON “Cartesian product: 14*4=56 rows”

Types of Joins Equijoin Non-equijoin Outer join Self join

What Is an Equijoin? EMP DEPT EMPNO ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS rows selected. Foreign key Primary key

Retrieving Records with Equijoins SQL> SELECT emp.empno, emp.ename, emp.deptno, 2dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC KING NEW YORK 7698 BLAKE CHICAGO 7782 CLARK NEW YORK 7566 JONES DALLAS rows selected.

Qualifying Ambiguous Column Names Use table prefixes to qualify column names that are in multiple tables. Use table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. Improve performance by using table prefixes. Distinguish columns that have identical names but reside in different tables by using column aliases. Distinguish columns that have identical names but reside in different tables by using column aliases. Use table prefixes to qualify column names that are in multiple tables. Use table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. Improve performance by using table prefixes. Distinguish columns that have identical names but reside in different tables by using column aliases. Distinguish columns that have identical names but reside in different tables by using column aliases.

Additional Search Conditions Using the AND Operator EMPDEPT EMPNO ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTINGNEW YORK 30SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS rows selected.

Using Table Aliases Simplify queries by using table aliases. Simplify queries by using table aliases. SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc 3 FROM emp e, dept d 4 WHERE e.deptno= d.deptno;

Joining More Than Two Tables NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. CUSTOMER CUSTID ORDID rows selected. CUSTID ORDID rows selected.ORD ORDID ITEMID rows selected. ORDID ITEMID rows selected.ITEM

Non-EquijoinsNon-Equijoins EMPSALGRADE “salary in the EMP table is between low salary and high salary in the SALGRADE table” EMPNO ENAME SAL KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES rows selected. GRADE LOSAL HISAL

Retrieving Records with Non-Equijoins ENAME SAL GRADE JAMES SMITH ADAMS rows selected. SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal;

Outer Joins EMP DEPT No employee in the OPERATIONS department ENAMEDEPTNO KING10 BLAKE30 CLARK10 JONES20... DEPTNO DNAME ACCOUNTING 30 SALES 10 ACCOUNTING 20RESEARCH... 40OPERATIONS

Outer Joins You use an outer join to also see rows that do not usually meet the join condition. You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). Outer join operator is the plus sign (+). You use an outer join to also see rows that do not usually meet the join condition. You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). Outer join operator is the plus sign (+). SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+); SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);

Using Outer Joins SQL> SELECTe.ename, d.deptno, d.dname 2 FROMemp e, dept d 3 WHEREe.deptno(+) = d.deptno 4 ORDER BYe.deptno; ENAME DEPTNO DNAME KING 10 ACCOUNTING CLARK 10 ACCOUNTING OPERATIONS 15 rows selected.

Self Joins EMP (WORKER) EMP (MANAGER) “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMPNOENAME MGR KING 7698BLAKE CLARK JONES MARTIN ALLEN7698 EMPNOENAME KING 7698BLAKE

Joining a Table to Itself WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;

SummarySummary Equijoin Non-equijoin Outer join Self join SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

Sub queries

ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Describe the types of problems that subqueries can solve Define subqueries Define subqueries List the types of subqueries List the types of subqueries Write single-row and multiple-row subqueries Write single-row and multiple-row subqueries After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Describe the types of problems that subqueries can solve Define subqueries Define subqueries List the types of subqueries List the types of subqueries Write single-row and multiple-row subqueries Write single-row and multiple-row subqueries

Using a Subquery to Solve a Problem “Who has a salary greater than Jones’?” “Who has a salary greater than Jones’?” “Which employees have a salary greater than Jones’ salary?” Main Query ? “What is Jones’ salary?” ? Subquery

SubqueriesSubqueries The subquery (inner query) executes once before the main query. The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). The result of the subquery is used by the main query (outer query). The subquery (inner query) executes once before the main query. The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). The result of the subquery is used by the main query (outer query). SELECTselect_list FROMtable WHEREexpr operator (SELECTselect_list FROMtable);

2975 SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno=7566); Using a Subquery ENAME KING FORD SCOTT ENAME KING FORD SCOTT

Guidelines for Using Subqueries Enclose subqueries in parentheses. Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Place subqueries on the right side of the comparison operator. Do not add an ORDER BY clause to a subquery. Do not add an ORDER BY clause to a subquery. Use single-row operators with single-row subqueries. Use single-row operators with single-row subqueries. Use multiple-row operators with multiple-row subqueries. Use multiple-row operators with multiple-row subqueries. Enclose subqueries in parentheses. Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Place subqueries on the right side of the comparison operator. Do not add an ORDER BY clause to a subquery. Do not add an ORDER BY clause to a subquery. Use single-row operators with single-row subqueries. Use single-row operators with single-row subqueries. Use multiple-row operators with multiple-row subqueries. Use multiple-row operators with multiple-row subqueries.

Types of Subqueries Single-row subquery Single-row subquery Main query Subquery returns CLERK Multiple-row subquery CLERKMANAGER Main query Subquery returns Multiple-column subquery CLERK 7900 MANAGER 7698 Main query Subquery returns

Single-Row Subqueries Return only one row Return only one row Use single-row comparison operators Use single-row comparison operators Return only one row Return only one row Use single-row comparison operators Use single-row comparison operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

Executing Single-Row Subqueries CLERK 1100 ENAME JOB MILLER CLERK ENAME JOB MILLER CLERK SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4(SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8(SELECT sal 9FROMemp 10WHEREempno = 7876);

Using Group Functions in a Subquery 800 ENAME JOB SAL SMITH CLERK 800 ENAME JOB SAL SMITH CLERK 800 SQL> SELECTename, job, sal 2 FROMemp 3 WHEREsal = 4(SELECTMIN(sal) 5FROMemp);

HAVING Clause with Subqueries The Oracle Server executes subqueries first. The Oracle Server executes subqueries first. The Oracle Server returns results into the HAVING clause of the main query. The Oracle Server returns results into the HAVING clause of the main query. The Oracle Server executes subqueries first. The Oracle Server executes subqueries first. The Oracle Server returns results into the HAVING clause of the main query. The Oracle Server returns results into the HAVING clause of the main query. 800 SQL> SELECTdeptno, MIN(sal) 2 FROMemp 3 GROUP BYdeptno 4 HAVINGMIN(sal) > 5(SELECTMIN(sal) 6FROMemp 7WHEREdeptno = 20);

What Is Wrong with This Statement? ERROR: ORA-01427: single-row subquery returns more than one row no rows selected ERROR: ORA-01427: single-row subquery returns more than one row no rows selected SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4(SELECT MIN(sal) 5FROM emp 6GROUP BY deptno); Single-row operator with multiple-row subquery

Will This Statement Work? no rows selected Subquery returns no values SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4(SELECTjob 5FROMemp 6WHEREename='SMYTHE');

Multiple-Row Subqueries Return more than one row Return more than one row Use multiple-row comparison operators Use multiple-row comparison operators Return more than one row Return more than one row Use multiple-row comparison operators Use multiple-row comparison operators Operator IN ANY ALL Meaning Equal to any member in the list Compare value to each value returned by the subquery Compare value to every value returned by the subquery

Using ANY Operator in Multiple-Row Subqueries EMPNO ENAME JOB MARTIN SALESMAN 7521 WARD SALESMAN EMPNO ENAME JOB MARTIN SALESMAN 7521 WARD SALESMAN SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4(SELECTsal 5 FROMemp 6WHEREjob = 'CLERK') 7 AND job <> 'CLERK';

Using ALL Operator in Multiple-Row Subqueries EMPNO ENAME JOB KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST EMPNO ENAME JOB KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal > ALL 4 (SELECTavg(sal) 5 FROMemp 6GROUP BYdeptno);

SummarySummary Subqueries are useful when a query is based on unknown values. Subqueries are useful when a query is based on unknown values. SELECTselect_list FROMtable WHEREexpr operator (SELECT select_list FROM table);

Creating Sequences

What Is a Sequence? Automatically generates unique numbers Automatically generates unique numbers Is a sharable object Is a sharable object Is typically used to create a primary key value Is typically used to create a primary key value Replaces application code Replaces application code Speeds up the efficiency of accessing sequence values when cached in memory Speeds up the efficiency of accessing sequence values when cached in memory Automatically generates unique numbers Automatically generates unique numbers Is a sharable object Is a sharable object Is typically used to create a primary key value Is typically used to create a primary key value Replaces application code Replaces application code Speeds up the efficiency of accessing sequence values when cached in memory Speeds up the efficiency of accessing sequence values when cached in memory

The CREATE SEQUENCE Statement Define a sequence to generate sequential numbers automatically. Define a sequence to generate sequential numbers automatically. CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}]; CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];

Creating a Sequence Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. Do not use the CYCLE option. Do not use the CYCLE option. Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. Create a sequence named DEPT_DEPTNO to be used for the primary key of the DEPT table. Do not use the CYCLE option. Do not use the CYCLE option. SQL> CREATE SEQUENCE dept_deptno 2 INCREMENT BY 1 3 START WITH 91 4 MAXVALUE NOCACHE 6 NOCYCLE; Sequence created. SQL> CREATE SEQUENCE dept_deptno 2 INCREMENT BY 1 3 START WITH 91 4 MAXVALUE NOCACHE 6 NOCYCLE; Sequence created.

Confirming Sequences Verify your sequence values in the USER_SEQUENCES data dictionary table. Verify your sequence values in the USER_SEQUENCES data dictionary table. The LAST_NUMBER column displays the next available sequence number. The LAST_NUMBER column displays the next available sequence number. Verify your sequence values in the USER_SEQUENCES data dictionary table. Verify your sequence values in the USER_SEQUENCES data dictionary table. The LAST_NUMBER column displays the next available sequence number. The LAST_NUMBER column displays the next available sequence number. SQL> SELECTsequence_name, min_value, max_value, 2 increment_by, last_number 3 FROMuser_sequences; SQL> SELECTsequence_name, min_value, max_value, 2 increment_by, last_number 3 FROMuser_sequences;

NEXTVAL and CURRVAL Pseudocolumns NEXTVAL returns the next available sequence value. NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. CURRVAL obtains the current sequence value. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value. NEXTVAL returns the next available sequence value. NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. CURRVAL obtains the current sequence value. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value.

NEXTVAL and CURRVAL Pseudocolumns NEXTVAL returns the next available sequence value. NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. CURRVAL obtains the current sequence value. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value. NEXTVAL returns the next available sequence value. NEXTVAL returns the next available sequence value. It returns a unique value every time it is referenced, even for different users. CURRVAL obtains the current sequence value. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value.

Using a Sequence Insert a new department named “MARKETING” in San Diego. Insert a new department named “MARKETING” in San Diego. View the current value for the DEPT_DEPTNO sequence. View the current value for the DEPT_DEPTNO sequence. Insert a new department named “MARKETING” in San Diego. Insert a new department named “MARKETING” in San Diego. View the current value for the DEPT_DEPTNO sequence. View the current value for the DEPT_DEPTNO sequence. SQL> INSERT INTOdept(deptno, dname, loc) 2 VALUES(dept_deptno.NEXTVAL, 3 'MARKETING', 'SAN DIEGO'); 1 row created. SQL> INSERT INTOdept(deptno, dname, loc) 2 VALUES(dept_deptno.NEXTVAL, 3 'MARKETING', 'SAN DIEGO'); 1 row created. SQL> SELECTdept_deptno.CURRVAL 2 FROMdual; SQL> SELECTdept_deptno.CURRVAL 2 FROMdual;

Using a Sequence Caching sequence values in memory allows faster access to those values. Caching sequence values in memory allows faster access to those values. Gaps in sequence values can occur when: Gaps in sequence values can occur when: A rollback occurs A rollback occurs The system crashes The system crashes A sequence is used in another table A sequence is used in another table View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table. View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table. Caching sequence values in memory allows faster access to those values. Caching sequence values in memory allows faster access to those values. Gaps in sequence values can occur when: Gaps in sequence values can occur when: A rollback occurs A rollback occurs The system crashes The system crashes A sequence is used in another table A sequence is used in another table View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table. View the next available sequence, if it was created with NOCACHE, by querying the USER_SEQUENCES table.

Modifying a Sequence Change the increment value, maximum value, minimum value, cycle option, or cache option. Change the increment value, maximum value, minimum value, cycle option, or cache option. SQL> ALTER SEQUENCE dept_deptno 2 INCREMENT BY 1 3 MAXVALUE NOCACHE 5 NOCYCLE; Sequence altered. SQL> ALTER SEQUENCE dept_deptno 2 INCREMENT BY 1 3 MAXVALUE NOCACHE 5 NOCYCLE; Sequence altered.

Guidelines for Modifying a Sequence You must be the owner or have the ALTER privilege for the sequence. You must be the owner or have the ALTER privilege for the sequence. Only future sequence numbers are affected. Only future sequence numbers are affected. The sequence must be dropped and re-created to restart the sequence at a different number. The sequence must be dropped and re-created to restart the sequence at a different number. You must be the owner or have the ALTER privilege for the sequence. You must be the owner or have the ALTER privilege for the sequence. Only future sequence numbers are affected. Only future sequence numbers are affected. The sequence must be dropped and re-created to restart the sequence at a different number. The sequence must be dropped and re-created to restart the sequence at a different number.

Removing a Sequence Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. Once removed, the sequence can no longer be referenced. Once removed, the sequence can no longer be referenced. Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. Once removed, the sequence can no longer be referenced. Once removed, the sequence can no longer be referenced. SQL> DROP SEQUENCE dept_deptno; Sequence dropped. SQL> DROP SEQUENCE dept_deptno; Sequence dropped.

Creating Views

What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV BLAKE MANAGER MAY CLARK MANAGER JUN JONES MANAGER APR MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB FORD ANALYST DEC SMITH CLERK DEC SCOTT ANALYST DEC ADAMS CLERK JAN MILLER CLERK JAN EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV BLAKE MANAGER MAY CLARK MANAGER JUN JONES MANAGER APR MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB FORD ANALYST DEC SMITH CLERK DEC SCOTT ANALYST DEC ADAMS CLERK JAN MILLER CLERK JAN EMP Table EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV CLARK MANAGER JUN MILLER CLERK JAN JONES MANAGER APR SCOTT ANALYST DEC ADAMS CLERK JAN SMITH CLERK DEC FORD ANALYST DEC BLAKE MANAGER MAY MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV CLARK MANAGER JUN MILLER CLERK JAN JONES MANAGER APR SCOTT ANALYST DEC ADAMS CLERK JAN SMITH CLERK DEC FORD ANALYST DEC BLAKE MANAGER MAY MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB EMPNO ENAME JOB KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK EMPVU10 View EMPVU10 View

Why Use Views? To restrict data access To restrict data access To make complex queries easy To make complex queries easy To allow data independence To allow data independence To present different views of the same data To present different views of the same data To restrict data access To restrict data access To make complex queries easy To make complex queries easy To allow data independence To allow data independence To present different views of the same data To present different views of the same data

Simple Views and Complex Views FeatureSimple ViewsComplex Views Number of tablesOneOne or more Contain functionsNoYes Contain groups of dataNoYes DML through viewYesNot always

Creating a View You embed a subquery within the CREATE VIEW statement. You embed a subquery within the CREATE VIEW statement. The subquery can contain complex SELECT syntax. The subquery can contain complex SELECT syntax. The subquery cannot contain an ORDER BY clause. The subquery cannot contain an ORDER BY clause. You embed a subquery within the CREATE VIEW statement. You embed a subquery within the CREATE VIEW statement. The subquery can contain complex SELECT syntax. The subquery can contain complex SELECT syntax. The subquery cannot contain an ORDER BY clause. The subquery cannot contain an ORDER BY clause. CREATE [OR REPLACE] VIEW view_name AS subquery; CREATE [OR REPLACE] VIEW view_name AS subquery;

Creating a View Create a view, EMPVU10, that contains details of employees in department 10. Create a view, EMPVU10, that contains details of employees in department 10. Describe the structure of the view by using the SQL*Plus DESCRIBE command. SQL> DESCRIBE empvu10 SQL> CREATE VIEW empvu10 2 AS SELECTempno, ename, job 3 FROMemp 4 WHEREdeptno = 10; View created.

Creating a View Create a view by using column aliases in the subquery. Create a view by using column aliases in the subquery. Select the columns from this view by the given alias names. Select the columns from this view by the given alias names. Create a view by using column aliases in the subquery. Create a view by using column aliases in the subquery. Select the columns from this view by the given alias names. Select the columns from this view by the given alias names. SQL> CREATE VIEW salvu30 2 AS SELECTempno EMPLOYEE_NUMBER, ename NAME, 3sal SALARY 4 FROMemp 5 WHEREdeptno = 30; View created.

Retrieving Data from a View EMPLOYEE_NUMBER NAME SALARY BLAKE MARTIN ALLEN TURNER JAMES WARD rows selected. EMPLOYEE_NUMBER NAME SALARY BLAKE MARTIN ALLEN TURNER JAMES WARD rows selected. SQL> SELECT * 2 FROMsalvu30;

Querying a View USER_VIEWS USER_VIEWS EMPVU10 SELECTempno, ename, job FROMemp WHEREdeptno = 10; USER_VIEWS USER_VIEWS EMPVU10 SELECTempno, ename, job FROMemp WHEREdeptno = 10; SQL*Plus SELECT * FROM empvu10; EMP 7839KINGPRESIDENT 7782CLARKMANAGER 7934MILLERCLERK

Modifying a View Modify the EMPVU10 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name. Modify the EMPVU10 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name. Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. Modify the EMPVU10 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name. Modify the EMPVU10 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name. Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. SQL> CREATE OR REPLACE VIEW empvu10 2 (employee_number, employee_name, job_title) 3 AS SELECT empno, ename, job 4 FROMemp 5 WHEREdeptno = 10; View created.

Removing a View Remove a view without losing data because a view is based on underlying tables in the database. Remove a view without losing data because a view is based on underlying tables in the database. SQL> DROP VIEW empvu10; View dropped. DROP VIEW view;

Dynamic SQL Queries

ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Produce queries that require an input variable Produce queries that require an input variable Produce more readable output Produce more readable output After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Produce queries that require an input variable Produce queries that require an input variable Produce more readable output Produce more readable output

Interactive Reports I want to input query values at runtime....sal = ? … … deptno = ? ….. ename = ?... User

Substitution Variables Use SQL*Plus substitution variables to temporarily store values. Use SQL*Plus substitution variables to temporarily store values. Single ampersand (&) Single ampersand (&) Double ampersand (&&) Double ampersand (&&) DEFINE and ACCEPT commands DEFINE and ACCEPT commands Pass variable values between SQL statements. Pass variable values between SQL statements. Use SQL*Plus substitution variables to temporarily store values. Use SQL*Plus substitution variables to temporarily store values. Single ampersand (&) Single ampersand (&) Double ampersand (&&) Double ampersand (&&) DEFINE and ACCEPT commands DEFINE and ACCEPT commands Pass variable values between SQL statements. Pass variable values between SQL statements.

Using the & Substitution Variable Use a variable prefixed with an ampersand (&) to prompt the user for a value. Use a variable prefixed with an ampersand (&) to prompt the user for a value. SQL> SELECTempno, ename, sal, deptno 2 FROMemp 3 WHEREempno = &employee_num; 7369 Enter value for employee_num: 7369 EMPNO ENAME SAL DEPTNO SMITH

Using the SET VERIFY Command Toggling the display of the text of a command before and after SQL*Plus replaces substitution variables with values. Toggling the display of the text of a command before and after SQL*Plus replaces substitution variables with values. SQL> SET VERIFY ON SQL> SELECTempno, ename, sal, deptno 2 FROMemp 3 WHEREempno = &employee_num; Enter value for employee_num: 7369 old 3: WHERE empno = &employee_num new 3: WHERE empno =

Character and Date Values with Substitution Variables Use single quotation marks for date and character values. Use single quotation marks for date and character values. SQL> SELECT ename, deptno, sal*12 2 FROM emp 3 WHERE job='&job_title'; ANALYST Enter value for job_title: ANALYST ENAME DEPTNO SAL* SCOTT FORD

Specifying Column Names, Expressions, and Text at Runtime Use substitution variables to supplement the following: Use substitution variables to supplement the following: WHERE condition WHERE condition ORDER BY clause ORDER BY clause Column expression Column expression Table name Table name Entire SELECT statement Entire SELECT statement Use substitution variables to supplement the following: Use substitution variables to supplement the following: WHERE condition WHERE condition ORDER BY clause ORDER BY clause Column expression Column expression Table name Table name Entire SELECT statement Entire SELECT statement

Specifying Column Names, Expressions, and Text at Runtime SQL> SELECTempno, ename, job, &column_name 2 FROMemp 3 WHERE&condition 4 ORDER BY&order_column; sal Enter value for column_name: sal sal>=3000 Enter value for condition: sal>=3000 ename Enter value for order_column: ename EMPNO ENAME JOB SAL FORD ANALYST KING PRESIDENT SCOTT ANALYST 3000

Using the && Substitution Variable Use the double-ampersand (&&) if you want to reuse the variable value without prompting the user each time. Use the double-ampersand (&&) if you want to reuse the variable value without prompting the user each time. SQL> SELECTempno, ename, job, &&column_name 2 FROMemp 3 ORDER BY&column_name; deptno Enter value for column_name: deptno EMPNO ENAME JOB DEPTNO KING PRESIDENT CLARK MANAGER MILLER CLERK rows selected.

Defining User Variables You can predefine variables using one of two SQL*Plus commands: You can predefine variables using one of two SQL*Plus commands: DEFINE: Create a CHAR datatype user variable DEFINE: Create a CHAR datatype user variable ACCEPT: Read user input and store it in a variable ACCEPT: Read user input and store it in a variable If you need to predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command. If you need to predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command. You can predefine variables using one of two SQL*Plus commands: You can predefine variables using one of two SQL*Plus commands: DEFINE: Create a CHAR datatype user variable DEFINE: Create a CHAR datatype user variable ACCEPT: Read user input and store it in a variable ACCEPT: Read user input and store it in a variable If you need to predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command. If you need to predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command.

The ACCEPT Command Creates a customized prompt when accepting user input Creates a customized prompt when accepting user input Explicitly defines a NUMBER or DATE datatype variable Explicitly defines a NUMBER or DATE datatype variable Hides user input for security reasons Hides user input for security reasons Creates a customized prompt when accepting user input Creates a customized prompt when accepting user input Explicitly defines a NUMBER or DATE datatype variable Explicitly defines a NUMBER or DATE datatype variable Hides user input for security reasons Hides user input for security reasons ACCEPT variable [datatype] [FORMAT format] [PROMPT text] [HIDE] ACCEPT variable [datatype] [FORMAT format] [PROMPT text] [HIDE]

Using the ACCEPT Command ACCEPT dept PROMPT 'Provide the department name: ' SELECT * FROM dept WHERE dname = UPPER('&dept') / Sales Provide the department name: Sales DEPTNO DNAME LOC SALES CHICAGO

DEFINE and UNDEFINE Commands A variable remains defined until you either: A variable remains defined until you either: Use the UNDEFINE command to clear it Use the UNDEFINE command to clear it Exit SQL*Plus Exit SQL*Plus You can verify your changes with the DEFINE command. You can verify your changes with the DEFINE command. To define variables for every session, modify your login.sql file so that the variables are created at startup. To define variables for every session, modify your login.sql file so that the variables are created at startup. A variable remains defined until you either: A variable remains defined until you either: Use the UNDEFINE command to clear it Use the UNDEFINE command to clear it Exit SQL*Plus Exit SQL*Plus You can verify your changes with the DEFINE command. You can verify your changes with the DEFINE command. To define variables for every session, modify your login.sql file so that the variables are created at startup. To define variables for every session, modify your login.sql file so that the variables are created at startup.

Using the DEFINE Command Create a variable to hold the department name. Create a variable to hold the department name. DEFINE DEPTNAME = "sales" (CHAR) Use the variable as you would any other variable. SQL> DEFINE deptname = sales SQL> DEFINE deptname SQL> SELECT * 2 FROM dept 3 WHERE dname = UPPER('&deptname');

Controlling User Access

ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Create users Create users Create roles to ease setup and maintenance of the security model Create roles to ease setup and maintenance of the security model Use the GRANT and REVOKE statements to grant and revoke object privileges Use the GRANT and REVOKE statements to grant and revoke object privileges After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: Create users Create users Create roles to ease setup and maintenance of the security model Create roles to ease setup and maintenance of the security model Use the GRANT and REVOKE statements to grant and revoke object privileges Use the GRANT and REVOKE statements to grant and revoke object privileges

Controlling User Access Databaseadministrator Users Username and password privileges

PrivilegesPrivileges Database security: Database security: System security System security Data security Data security System privileges: Gain access to the database System privileges: Gain access to the database Object privileges: Manipulate the content of the database objects Object privileges: Manipulate the content of the database objects Schema: Collection of objects, such as tables, views, and sequences Schema: Collection of objects, such as tables, views, and sequences Database security: Database security: System security System security Data security Data security System privileges: Gain access to the database System privileges: Gain access to the database Object privileges: Manipulate the content of the database objects Object privileges: Manipulate the content of the database objects Schema: Collection of objects, such as tables, views, and sequences Schema: Collection of objects, such as tables, views, and sequences

System Privileges More than 80 privileges are available. More than 80 privileges are available. The DBA has high-level system privileges: The DBA has high-level system privileges: Create new users Create new users Remove users Remove users Remove tables Remove tables Back up tables Back up tables More than 80 privileges are available. More than 80 privileges are available. The DBA has high-level system privileges: The DBA has high-level system privileges: Create new users Create new users Remove users Remove users Remove tables Remove tables Back up tables Back up tables

Creating Users The DBA creates users by using the CREATE USER statement. The DBA creates users by using the CREATE USER statement. SQL> CREATE USER scott 2 IDENTIFIED BY tiger; User created. SQL> CREATE USER scott 2 IDENTIFIED BY tiger; User created. CREATE USER user IDENTIFIED BY password;

User System Privileges GRANT privilege [, privilege...] TO user [, user...]; GRANT privilege [, privilege...] TO user [, user...]; An application developer may have the following system privileges: – CREATE SESSION – CREATE TABLE – CREATE SEQUENCE – CREATE VIEW – CREATE PROCEDURE An application developer may have the following system privileges: – CREATE SESSION – CREATE TABLE – CREATE SEQUENCE – CREATE VIEW – CREATE PROCEDURE Once a user is created, the DBA can grant specific system privileges to a user.

Granting System Privileges The DBA can grant a user specific system privileges. The DBA can grant a user specific system privileges. SQL> GRANT create table, create sequence, create view 2 TO scott; Grant succeeded. SQL> GRANT create table, create sequence, create view 2 TO scott; Grant succeeded.

What Is a Role? Allocating privileges without a role Allocating privileges with a role Privileges Users Manager

Creating and Granting Privileges to a Role SQL> CREATE ROLE manager; Role created. SQL> CREATE ROLE manager; Role created. SQL> GRANT create table, create view 2 to manager; Grant succeeded. SQL> GRANT create table, create view 2 to manager; Grant succeeded. SQL> GRANT manager to BLAKE, CLARK; Grant succeeded. SQL> GRANT manager to BLAKE, CLARK; Grant succeeded.

Changing Your Password The DBA creates your user account and initializes your password. The DBA creates your user account and initializes your password. You can change your password by using the ALTER USER statement. You can change your password by using the ALTER USER statement. The DBA creates your user account and initializes your password. The DBA creates your user account and initializes your password. You can change your password by using the ALTER USER statement. You can change your password by using the ALTER USER statement. SQL> ALTER USER scott 2 IDENTIFIED BY lion; User altered.

Object Privilege TableViewSequenceProcedure ALTER  DELETE *  EXECUTE  INDEX * INSERT *  REFERENCES * SELECT *  UPDATE *  Object Privileges

Object privileges vary from object to object. Object privileges vary from object to object. An owner has all the privileges on the object. An owner has all the privileges on the object. An owner can give specific privileges on that owner’s object. An owner can give specific privileges on that owner’s object. Object privileges vary from object to object. Object privileges vary from object to object. An owner has all the privileges on the object. An owner has all the privileges on the object. An owner can give specific privileges on that owner’s object. An owner can give specific privileges on that owner’s object. GRANTobject_priv [(columns)] ONobject TO{user|role|PUBLIC} [WITH GRANT OPTION]; GRANTobject_priv [(columns)] ONobject TO{user|role|PUBLIC} [WITH GRANT OPTION];

Granting Object Privileges SQL> GRANTselect 2 ONemp 3 TOsue, rich; Grant succeeded. SQL> GRANTselect 2 ONemp 3 TOsue, rich; Grant succeeded. SQL> GRANTupdate (dname, loc) 2 ONdept 3 TOscott, manager; Grant succeeded. SQL> GRANTupdate (dname, loc) 2 ONdept 3 TOscott, manager; Grant succeeded. Grant query privileges on the EMP table. Grant query privileges on the EMP table. Grant privileges to update specific columns to users and roles.

Using WITH GRANT OPTION and PUBLIC Keywords Allow all users on the system to query data from Alice’s DEPT table. Allow all users on the system to query data from Alice’s DEPT table. SQL> GRANTselect, insert 2 ONdept 3 TOscott 4 WITH GRANT OPTION; Grant succeeded. SQL> GRANTselect, insert 2 ONdept 3 TOscott 4 WITH GRANT OPTION; Grant succeeded. SQL> GRANTselect 2 ONalice.dept 3 TOPUBLIC; Grant succeeded. SQL> GRANTselect 2 ONalice.dept 3 TOPUBLIC; Grant succeeded. Give a user authority to pass along the privileges.

Confirming Privileges Granted Data Dictionary TableDescription ROLE_SYS_PRIVSSystem privileges granted to roles ROLE_TAB_PRIVSTable privileges granted to roles USER_ROLE_PRIVSRoles accessible by the user USER_TAB_PRIVS_MADEObject privileges granted on the user’s objects USER_TAB_PRIVS_RECDObject privileges granted to the user USER_COL_PRIVS_MADEObject privileges granted on the columns of the user’s objects USER_COL_PRIVS_RECDObject privileges granted to the user on specific columns

How to Revoke Object Privileges You use the REVOKE statement to revoke privileges granted to other users. You use the REVOKE statement to revoke privileges granted to other users. Privileges granted to others through the WITH GRANT OPTION will also be revoked. Privileges granted to others through the WITH GRANT OPTION will also be revoked. You use the REVOKE statement to revoke privileges granted to other users. You use the REVOKE statement to revoke privileges granted to other users. Privileges granted to others through the WITH GRANT OPTION will also be revoked. Privileges granted to others through the WITH GRANT OPTION will also be revoked. REVOKE {privilege [, privilege...]|ALL} ON object FROM {user[, user...]|role|PUBLIC} [CASCADE CONSTRAINTS]; REVOKE {privilege [, privilege...]|ALL} ON object FROM {user[, user...]|role|PUBLIC} [CASCADE CONSTRAINTS];

Revoking Object Privileges As user Alice, revoke the SELECT and INSERT privileges given to user Scott on the DEPT table. As user Alice, revoke the SELECT and INSERT privileges given to user Scott on the DEPT table. SQL> REVOKEselect, insert 2 ONdept 3 FROMscott; Revoke succeeded. SQL> REVOKEselect, insert 2 ONdept 3 FROMscott; Revoke succeeded.

SummarySummary StatementAction CREATE USERAllows the DBA to create a user GRANTAllows the user to give other users privileges to access the user’s objects CREATE ROLEAllows the DBA to create a collection of privileges ALTER USERAllows users to change their password REVOKERemoves privileges on an object from users

Practice Overview Granting other users privileges to your table Granting other users privileges to your table Modifying another user’s table through the privileges granted to you Modifying another user’s table through the privileges granted to you Creating a synonym Creating a synonym Querying the data dictionary views related to privileges Querying the data dictionary views related to privileges Granting other users privileges to your table Granting other users privileges to your table Modifying another user’s table through the privileges granted to you Modifying another user’s table through the privileges granted to you Creating a synonym Creating a synonym Querying the data dictionary views related to privileges Querying the data dictionary views related to privileges