Database Objects

Entry Level Technology Program

Object View

Description Logically represents subsets of data from one or more tables

Sequence Index Synonym

Generates primary key values
Improves the performance of some queries Alternative name for an object

Satyam Learning Center

ORACLE

1 of 1

Why Use Views?
Entry Level Technology Program

 To  To  To  To

restrict data access make complex queries easy provide data independence present different views of the same data

Satyam Learning Center

ORACLE

2 of 1

Entry Level Technology Program

Simple Views and Complex Views

Feature Number of tables Contain functions Contain groups of data DML operations through a view

Simple Views One No No

Complex Views One or more Yes Yes

Yes

Not always

Satyam Learning Center

ORACLE

3 of 1

Creating a View
Entry Level Technology Program

 You embed a subquery within the CREATE VIEW statement.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];

 The subquery can contain complex SELECT syntax.

Satyam Learning Center

ORACLE

4 of 1

SAL FROM EMP WHERE DEPTNO = 30. EMPVU30. Satyam Learning Center ORACLE 5 of 1 . ENAME.Creating a View Entry Level Technology Program  Create a view. that contains details of employees in department 30. View created. CREATE VIEW EMPVU30 AS SELECT EMPNO.

CREATE VIEW SALVU10 AS SELECT EMPNO ID_NUMBER. ENAME SAL*12 ANN_SALARY FROM EMP WHERE DEPTNO = 10. Satyam Learning Center ORACLE 6 of 1 . View created.  Select the columns from this view by the given alias names. NAME.Creating a View Entry Level Technology Program  Create a view by using column aliases in the subquery.

Retrieving Data from a View Entry Level Technology Program SELECT * FROM SALVU10. Satyam Learning Center ORACLE 7 of 1 .

SAL FROM EMP WHERE DEPTNO=30.Querying a View Entry Level Technology Program Oracle Server iSQL*Plus SELECT FROM * EMPVU30. EMP Satyam Learning Center ORACLE 8 of 1 . ENAME. USER_VIEWS EMPVU30 SELECT EMPNO.

SAL. ENAME. DEPTNO FROM EMP WHERE DEPTNO = 30. NAME. SALARY. CREATE OR REPLACE VIEW EMPVU30 (ID_NUMBER. Add an alias for each column name. Satyam Learning Center ORACLE 9 of 1 . DNO) AS SELECT EMPNO.  Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery.Modifying a View Entry Level Technology Program  Modify the EMPVU30 view by using CREATE OR REPLACE VIEW clause. View created.

View created. avgsal) AS SELECT d. minsal.Creating a Complex View Entry Level Technology Program Create a complex view that contains group functions to display values from two tables.DEPTNO = d.DNAME.DEPTNO GROUP BY d.SAL). CREATE VIEW dept_sum_vu (name.DNAME. MAX(e. MIN(e. DEPT d WHERE e.SAL) FROM EMP e.AVG(e.SAL). Satyam Learning Center ORACLE 10 of 1 . maxsal.

Entry Level Technology Program Rules for Performing DML Operations on a View  You can perform DML operations on simple views.  You cannot remove a row if the view contains the following:  Group functions  A GROUP BY clause  The DISTINCT keyword  The pseudocolumn ROWNUM keyword Satyam Learning Center ORACLE 11 of 1 .

Entry Level Technology Program Rules for Performing DML Operations on a View You cannot modify data in a view if it contains:  Group functions  A GROUP BY clause  The DISTINCT keyword  The pseudocolumn ROWNUM keyword  Columns defined by expressions Satyam Learning Center ORACLE 12 of 1 .

Entry Level Technology Program Rules for Performing DML Operations on a View You cannot add data through a view if the view includes:  Group functions  A GROUP BY clause  The DISTINCT keyword  The pseudocolumn ROWNUM keyword  Columns defined by expressions  NOT NULL columns in the base tables that are not selected by the view Satyam Learning Center ORACLE 13 of 1 .

CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM EMP WHERE DEPTNO = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck . View created.  Any attempt to change the department number for any row in the view fails because it violates the WITH CHECK OPTION constraint.Using the WITH CHECK OPTION Clause Entry Level Technology Program  You can ensure that DML operations performed on the view stay within the domain of the view by using the WITH CHECK OPTION clause. Satyam Learning Center ORACLE 14 of 1 .

 Any attempt to perform a DML on any row in the view results in an Oracle server error. Satyam Learning Center ORACLE 15 of 1 .Denying DML Operations Entry Level Technology Program  You can ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition.

Denying DML Operations Entry Level Technology Program CREATE OR REPLACE VIEW empvu40 (employee_number. View created. JOB FROM EMP WHERE DEPTNO = 40 WITH READ ONLY. employee_name. job_title) AS SELECT EMPNO. Satyam Learning Center ORACLE 16 of 1 . ENAME.

DROP VIEW empvu30. Satyam Learning Center ORACLE 17 of 1 . View dropped.Removing a View Entry Level Technology Program You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view.

Satyam Learning Center ORACLE 18 of 1 .  A named subquery in the FROM clause of the main query is an example of an inline view.  An inline view is not a schema object.Inline Views Entry Level Technology Program  An inline view is a subquery with an alias (or correlation name) that you can use within a SQL statement.

Summary Entry Level Technology Program In this lesson. you should have learned that a view is derived from data in other tables or views and provides the following advantages:  Restricts database access  Simplifies queries  Provides data independence  Provides multiple views of the same data  Can be dropped without removing the underlying data  An inline view is a subquery with an alias name. Satyam Learning Center ORACLE 19 of 1 .

Queries Entry Level Technology Program  1) Which Language is known by only one programmer ( Ref Tables from Exe-1 doc)  2)Display highest paid N Employees Satyam Learning Center ORACLE 20 of 1 .

you should be able to do the following:  Create. and use sequences  Create and maintain indexes  Create private and public synonyms Satyam Learning Center ORACLE 21 of 1 .Objectives Entry Level Technology Program After completing this lesson. maintain.

What Is a Sequence? Entry Level Technology Program A sequence:  Automatically generates unique numbers  Is a sharable object  Is typically used to create a primary key value  Replaces application code  Speeds up the efficiency of accessing sequence values when cached in memory Satyam Learning Center ORACLE 22 of 1 .

Entry Level Technology Program The CREATE SEQUENCE Statement Syntax 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}]. Satyam Learning Center ORACLE 23 of 1 .

Sequence created.Creating a Sequence Entry Level Technology Program  Create a sequence named DEPT_DEPTID_SEQ to be used for the primary key of the DEPARTMENTS table. CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE.  Do not use the CYCLE option. Satyam Learning Center ORACLE 24 of 1 .

max_value. last_number user_sequences. Satyam Learning Center ORACLE 25 of 1 .Confirming Sequences Entry Level Technology Program  Verify your sequence values in the USER_SEQUENCES data dictionary table. increment_by. min_value. SELECT FROM sequence_name.  The LAST_NUMBER column displays the next available sequence number if NOCACHE is specified.

Entry Level Technology Program NEXTVAL and CURRVAL Pseudocolumns  NEXTVAL returns the next available sequence value.  CURRVAL obtains the current sequence value. Satyam Learning Center ORACLE 26 of 1 . even for different users. It returns a unique value every time it is referenced.  NEXTVAL must be issued for that sequence before CURRVAL contains a value.

LOC) VALUES (dept_deptid_seq. 'Support'.  View the current value for the DEPT_DEPTID_SEQ sequence.NEXTVAL.Using a Sequence Entry Level Technology Program  Insert a new department named “Support” in location Hyderabad.’hyderabad‘).CURRVAL dual. DNAME. 1 row created. SELECT FROM dept_deptid_seq. INSERT INTO DEPT(DEPTNO. Satyam Learning Center ORACLE 27 of 1 .

 Gaps in sequence values can occur when:  A rollback occurs  The system crashes  A sequence is used in another table  If the sequence was created with NOCACHE. view the next available value.Using a Sequence Entry Level Technology Program  Caching sequence values in memory gives faster access to those values. Satyam Learning Center ORACLE 28 of 1 . by querying the USER_SEQUENCES table.

Sequence altered. cycle option. ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE. Satyam Learning Center ORACLE 29 of 1 . maximum value. minimum value.Modifying a Sequence Entry Level Technology Program Change the increment value. or cache option.

 The sequence must be dropped and re-created to restart the sequence at a different number.  Only future sequence numbers are affected.  Some validation is performed. Satyam Learning Center ORACLE 30 of 1 .Guidelines for Modifying a Sequence Entry Level Technology Program  You must be the owner or have the ALTER privilege for the sequence.

Sequence dropped. the sequence can no longer be referenced.  Once removed. Satyam Learning Center ORACLE 31 of 1 .Removing a Sequence Entry Level Technology Program  Remove a sequence from the data dictionary by using the DROP SEQUENCE statement. DROP SEQUENCE dept_deptid_seq.

What is an Index? Entry Level Technology Program An index:  Is a schema object  Is used by the Oracle server to speed up the retrieval of rows by using a pointer  Can reduce disk I/O by using a rapid path access method to locate data quickly  Is independent of the table it indexes  Is used and maintained automatically by the Oracle server Satyam Learning Center ORACLE 32 of 1 .

How Are Indexes Created? Entry Level Technology Program  Automatically: A unique index is created automatically when you define a PRIMARY KEY or UNIQUE constraint in a table definition.  Manually: Users can create nonunique indexes on columns to speed up access to the rows. Satyam Learning Center ORACLE 33 of 1 .

Satyam Learning Center ORACLE 34 of 1 . column].Creating an Index Entry Level Technology Program  Create an index on one or more columns. CREATE INDEX emp_name_idx ON emp(ename).  Improve the speed of query access to the LAST_NAME column in the EMPLOYEES table. Index created.). CREATE INDEX index ON table (column[...

When to Create an Index Entry Level Technology Program  You should create an index if:  A column contains a wide range of values  A column contains a large number of null values  One or more columns are frequently used together in a WHERE clause or a join condition  The table is large and most queries are expected to retrieve less than 2 to 4 percent of the rows Satyam Learning Center ORACLE 35 of 1 .

When Not to Create an Index Entry Level Technology Program It is usually not worth creating an index if: The table is small The columns are not often used as a condition in the query Most queries are expected to retrieve more than 2 to 4 percent of the rows in the table  The table is updated frequently  The indexed columns are referenced as part of an expression     Satyam Learning Center ORACLE 36 of 1 .

 The USER_IND_COLUMNS view contains the index name. the table name.Confirming Indexes Entry Level Technology Program  The USER_INDEXES data dictionary view contains the name of the index and its uniqueness. ic.index_name ic.index_name.table_name = 'EMP'. SELECT FROM WHERE AND ic. and the column name. Satyam Learning Center ORACLE 37 of 1 .column_name. ic.index_name = ix.uniqueness user_indexes ix.column_position col_pos. user_ind_columns ic ic.ix.

Satyam Learning Center ORACLE 38 of 1 . DROP INDEX index. Index dropped. you must be the owner of the index or have the DROP ANY INDEX privilege.  To drop an index.Removing an Index Entry Level Technology Program  Remove an index from the data dictionary by using the DROP INDEX command.  Remove the UPPER_LAST_NAME_IDX index from the data dictionary. DROP INDEX upper_last_name_idx.

Synonyms Entry Level Technology Program Simplify access to objects by creating a synonym (another name for an object). With synonyms. Satyam Learning Center ORACLE 39 of 1 . you can:  Ease referring to a table owned by another user  Shorten lengthy object names CREATE [PUBLIC] SYNONYM synonym FOR object.

DROP SYNONYM d_sum. Synonym dropped. Satyam Learning Center ORACLE 40 of 1 .Entry Level Technology Program Creating and Removing Synonyms  Create a shortened name for the DEPT_SUM_VU view. CREATE SYNONYM d_sum FOR dept_sum_vu. Synonym Created.  Drop a synonym.

you should have learned how to:  Automatically generate sequence numbers by using a sequence generator  View sequence information in the USER_SEQUENCES data dictionary table  Create indexes to improve query retrieval speed  View index information in the USER_INDEXES dictionary table  Use synonyms to provide alternative names for objects Satyam Learning Center ORACLE 41 of 1 .Summary Entry Level Technology Program  In this lesson.