You are on page 1of 7

VIT

UNIVERSITY
(Estd. u/s 3 of UGC Act 1956)

Vellore - 632 014, Tamil Nadu, India SUB: RDBMS LAB SEM:IV School of Computing Sciences Class: B.Tech (CSE) Batch:B

VIEWS, SYNONYMS, SEQUENCES AND INDEXES

VIEWS Def: A view is a named, derived, virtual table. A view takes the output of a query and treats it as a table; therefore a view can be thought of as a stored query or a virtual table. We can use views in most places where tables can be used. To the user, accessing a view is like accessing a table. The RDBMS creates an illusion of a table, by assigning a name to the view and storing its definition in the database. The tables upon which the views are based are called as base tables. CREATION OF A VIEW: The syntax for creating a view is given by: create [or replace][[no][force]]view <view_name> [column alias name]as <query>[with[check option]read only][constraint]]; Example: SQL>create or replace view EMP_VIEW as select * from EMP; This statement creates a view named EMP_VIEW .The data in this view comes from the base table EMP. Any changes made to the base table are instantly visible through the view EMP_VIEW.We can use select statement just like on a table. SQL>select * from EMP_VIEW; When create or replace is given, view is created if it is not available otherwise it is recreated. HOW DOES RDBMS HANDLE THE VIEWS: When a reference is made by a user ,the RDBMS finds the definition of the view stored in the database .It then translates the users request that referenced the view into an equivalent request against the source tables of the view. Thus RDBMS maintains the illusion of the view.

TYPES OF VIEWS: The different types of views are Column subset view Row subset view Row-Column subset view Grouped view Joined view COLUMN SUBSET VIEW: A column subset view is one where all the rows but only some of the columns of the base table form the view. The create view Ex: SQL>create or replace view CSV as select empno, ename, sal from EMP; This view includes only columns empno, ename, sal of EMP table. Since there is no where clause it includes all the rows.

ROW SUBSET VIEW: A row subset view is one where all columns but some rows of the source table form the view. All the columns of the base table participate in the view but all rows do not. Ex: SQL> create or replace view RSV as select * from EMP where deptno=10; The where clause restricts the no. of rows to those of employees working in Department Number 10. ROW-COLUMN SUBSET VIEW: A row-column subset view is a view which includes only some rows and columns of the base table. Ex: SQL>create or replace view RCS as select EMPNO, ENAME, SAL from EMP where deptno=10; GROUPED VIEW: The query specified in the view definition can include the GROUP BY clause. This type of view is called as Grouped View. Ex: SQL>create or replace view GV (dno, avgsal) as select deptno, AVG (SAL) from emp group by deptno; JOINED VIEWS: A joined view is formed by specifying a two or more table query in the view definition. A joined view draws its data from two or more tables and presents the result as a single virtual table. Ex: SQL>create or replace view JV(empno,ename,sal,dname,loc) as select empno,ename,sal,dname,loc from EMP,DEPT where EMP.deptno=DEPT.deptno;

CREATING A READ ONLY VIEW: Use with read only clause to prevent the users from manipulating records via the view. Ex: SQL>create or replace view WRO as select * from EMP with read only; Note: A view can be created without a base table using FORCE option of create view command. Ex: SQL>create or replace force view FVIEW as select * from MYDEPT; In this query MYDEPT table does not exist, so view is created with compilation errors. When MYDEPT table is created and this query is executed, the view is automatically recompiled and become valid.

VIEW WITH CHECK OPTION: This option specifies that inserts and updates performed through the view must result in rows that the view query can select. The CHECK OPTION can be used to maintain integrity on a view. SQL>insert into RSV (empno, ename, sal, deptno) values (1000,dinesh, 5500, 20); Though the view is created for deptno 10, we are able to insert records for other department numbers .This can be restricted using WITH CHECK OPTION clause while creating a view. SQL>create view DEPTNO10_VIEW as select * from EMP where deptno=10 WITH CHECK OPTION CONSTRAINT CHK_DNO10; The above statement creates a view DEPTNO10 with a check constraint. This will enforce the view to be inserted or updated only for the department number 10. No other departments can be inserted or updated.

DROPPING A VIEW:A view can be dropped by using DROP VIEW command.A view becomes invalid if its associated base table is dropped. SQL>drop view DEPTNO10; This will not affect the base table EMP. ADVANTAGES OF VIEWS: Valid Information: Views let different users see a table from different perspectives. Only the part that is relevant to the users is visible to them. Restricted Access: Views restrict access to the table. Different users are allowed to see only certain rows or certain columns of a table.

Simplified Access: Views simplify database access. For example a view that is a join of three tables where a user does not require all the data in all three tables. Data Integrity: Data Integrity can be maintained by having WITH CHECK OPTION while creating a view. RESTRICTIONS ON VIEWS: A views query cannot select the CURRVAL or NEXTVAL pseudo columns. If a views query selects the ROWID, ROWNUM or LEVEL pseudo columns, they must have aliases in the views query. A view cant be created with an ORDER BY clause. A view cant be updated, deleted and inserted if it is a grouped view. A view created from multiple tables cant be updatable. If a view is based on a single underlying table then you can insert,update or delete rows in this view.This will actually insert,update or delete rows in the underlying table. There are restrictions again on doing this: You cannot insert if the underlying table has a NOT NULL column that does not appear in the view. You cannot insert or update if any of the views columns refrenced in insert or update consist of functions or calculations. You cannot insert ,update or delete if the view contains GROUP BY, DISTINCT or a reference to a pseudo column ROWNUM. SYNONYMS: A synonym is an alias name for a table, view, sequence or program unit. A synonym is not an object itself but it is a reference to an object. Synonyms are used to mask the real name and owner of an object. Provide public access to an object. Provide location transparency for tables, views or program units of a remote database. Simplify the SQL statements for database users. Public or Private Synonyms: A synonym can be public or private. An individual user can create a private synonym, which is available only to that user. Database administrators most often create public synonyms that make base schema object available for system wide use by any database user. SYNTAX TO CREATE A SYNONYM: Create [public] synonym <synonym name> for <table name>[@database name] Ex: SQL>create synonym EMP_SYN for EMP; Any manipulations done to EMP_SYN will inturn affect EMP. Dropping Synonym: SQL>Drop synonym EMP_SYN; Note: Only a DBA create public synonym.

Examples of public synonyms are CAT, TAB, IND, SEQ and SYN. SEQUENCES:A sequence generates a serial list of unique numbers for numeric columns of database table. SYNTAX FOR CREATING A SEQUENCE: CREATE SEQUENCE <seqname> INCREMENT BY a START WITH n [MAXVALUE n][MINVALUE n][CYCLE/NO CYCLE][CACHE/NO CACHE] Seqname: is the name of the sequence to be created. INCREMENT BY: specifies the interval between sequence numbers.This interval can be positive or negative integer ut not zero.This value can have 28 digits or less.The absolute value of this must be less than MAXVALUE- MINVALUE.If this value is negative then sequence descends else it ascends.The default value for interval is 1. START WITH-specifies the first sequence number to be generated.You can use this to start ascendin or descending sequence. For ascending sequence the default value is sequences minimum value. For descending sequence the default value is the sequences maximum value. MAXVALUE: specifies sequences maximum value. MINVALUE: specifies sequences minimum value. CYCLE: specifies that the sequence continues to generate after reaching either its maximum or minimum value based on ascending or descending sequence respectively. NO CYCLE: specifies that the sequence cannot generate more values after reaching maximum or minimum value. The default is NOCYCLE. CACHE: specifies how many values of sequence Oracle pre-allocates and keeps in memory for faster access. The minimum value for this parameter is 2.You cannot CACHE more values than will fit into a CYCLE of sequence numbers, for sequences that cycle. Thus the maximum value allowed for CACHE must be less than the value determined by the following formula: (CEIL (MAXVALUE-MINVALUE))/ABS (INCREMENT) NO CACHE: specifies that the values of the sequence are not pre-allocated. On omitting CACHE/NO CACHE parameter Oracle caches 20 sequence numbers by default. Ex:

SQL> create sequence EMPNOSEQ INCREMENT BY 1 START WITH 1000 MAXVALUE 9999. This statement created a sequence called EMPNOSEQ not associated with any table. The purpose is to create unique employee numbers. It can be used to insert in employee table as follows: SQL>create table EMP EMPNO number(4),ENAME varchar2(30)); SQL>insert into EMP values (EMPNOSEQ.NEXTVAL,&Ename); PSEUDO COLUMNS WITH SEQUENCES: To find out the current sequence number, we can apply the pseudo-column CURRVAL in a select statement. SQL>select EMPNOSEQ.CURRVAL from DUAL; To find out the next sequence number,we can apply the pseudo-column nextval in a select statement. SQL>select EMPNOSEQ.NEXTVAL from DUAL; Some more examples on sequences: SQL> CREATE SEQUENCE ENOSEQ1 INCREMENT BY 2 START WITH 5 MINVALUE 2 MAXVALUE 15CYACLE CAHCHE 5; SQL>CREATE SEQUENCE ENOSEQ2 INCREMENT BY -1 MINVALUE 5 MAXVALUE 10; DROPPING A SEQUENCE A sequence can be dropped using DROP SEQUENCE command. SQL> DROP SEQUENCE ENOSEQ1; INDEXES: Indexes are optional structures associated with tables and clusters.You can create indexes explicitly to speed up the SQL statement execution on a table. Indexes are the primary means of reducing disk I/O when properly used. An index is merely a fast access path to the data. It affects only the speed of execution. Indexes are logically and physically independent of the data in the associated table. You can create or drop index at any time without affecting the base table s or other indexes. If you drop an index applications continue to work, however access to previously indexed data might be slower. Indexes, as independent structures, require storage space. Oracle automatically maintains and uses indexes once they are created. Presence of many indexes on a table decreases performance of updates, deletes, inserts because Oracle must also update the indexes associated with the table. UNIQUE AND NON UNIQUE INDEXES:

Indexes can be unique or non-unique. Unique indexes guarantee that no two rows of a table have duplicate values in the column that define the index. Non unique index does not impose this restriction on the column values. Oracle recommends that you do not explicitly define UNIQUE indexes on tables ,as uniqueness is strictly a logical concept and should be associated with the definition of a table. Oracle enforces UNIQUE integrity constraint by automatically defining a UNIQUE index on UNIQUE key. Ex: To create unique index SQL>CREATE UNIQUE INDEX UNIENO ON EMP (EMPNO); Ex: To create non-unique index SQL>CREATE INDEX ENAME_IND ON EMP (ENAME); COMPOSITE INDEX:A composite index is an index that can be created on multiple columns in a table. Ex: SQL> CREATE INDEX ENO_ENAME_ID ON EMP (EMPNO, ENAME); Index -Only Table: An index only table keeps data sorted according to the primary key for the table. An index-only table allows you to store the entire tables data in an index. A normal index only stores the indexed columns in index but an index-only table stores all the columns of the table in the index. Since all the tables data is stored in index the rows of the table do not have ROWIDS.Therefore you cannot select ROWID pseudo-column values from indexonly table. Also you cannot create additional indexes on the table; the only valid index is the primary key index. Ex: CREATE TABLE CUSTOMER (CUSTID NUMBER (4) CONSTRAINT CUSTID_PK PRIMARY KEY, CNAME VARCHAR2 (15)) ORGANIZATION INDEX; Note:In order to create an index-only table a primary key constraint must be created in it . DROPPING INDEX: Index can be drooped using DROP INDEX command which wont affect the process of fetching records from the table. Note: UNIQUE index cannot be dropped when Unique constraint is there.

You might also like