You are on page 1of 7

CHAPTER NO :-10 DATABASE OBJECTS THERE ARE 5 DATABASE OBJECTS.

1) TABLE : TABLE IS NOTHING BUT BASIC STORAGE OF UNIT WHERE DATA IS STORE IN FOR M OF ROWS AND COLUMNS. 2) VIEW : IT IS A LOGICAL REPRESENTATION OF DATA IN ANOTHER WORDS WE CAN ALSO S AY THAT IT IS A VIRTUAL TABLE. 3) SEQUENCES : GENERATES NUMBERIC DATA . EG : PRIMARY KEY.

4) INDEX : USED TO IMPROVE PERFORMANCE OF THE QUERIES. 5) SYNONYM : ANOTHER NAME TO AN OBJECTS. -----------------------------------------------------------------------------------THERE ARE FEW NAMING RULES FOR DATABSE OBJECTS Must begin with a letter Must be 1 30 characters long Must contain only A Z, a z, 0 9, _, $, and # Must not duplicate the name of another object owned by the same user Must not be an Oracle server reserved word Note: Names are case-insensitive. For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES. VIEWS ============ A view contains no data of its own the data which we see is of its base table. T he tables on which a view is based are called base tables. The view is stored as a SELECT sta tement in the data dictionary.We can use view to change data of an actual table. Advantages of Views Views restrict access to the data because the view can display selected columns from the table. Views can be used to make simple queries to retrieve the results of complicated queries. For example, views can be used to query information from multiple table s without the user knowing how to write a join statement. Views provide groups of users access to data according to their particular crite ria. Views are divided into two category 1) SIMPLE VIEW 2) COMPLEX VIEW The basic difference is related to the DML (INSERT, UPDATE, and DELETE) operatio ns.

A simple view is one that: Derives data from only one table Contains no functions or groups of data Can perform DML operations through the view A complex view is one that: Derives data from many tables Contains functions or groups of data Does not always allow DML operations through the view Syntax ======= CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]]; In the syntax: OR REPLACE re-creates the view if it already exists FORCE creates the view regardless of whether or not the base tables exist NOFORCE creates the view only if the base tables exist (This is the default.) The number of aliases must match the number of expressions selected by the view. subquery is a complete SELECT statement (You can use aliases for the columns in the SELECT list.) WITH CHECK OPTION specifies that only those rows that are accessible to the view can be inserted or updated constraint is the name assigned to the CHECK OPTION constraint WITH READ ONLY en sures that no DML operations can be performed on this view ================================================================================ ================================================================================ ====== create table e1 as select * from employees; create view v1 as select employee_id,first_name,email from e1; create or replace view v1 as select employee_id,first_name,email,department_id from e1; NOTE :-With the OR REPLACE option, a view can be created even if one exists with this name already, thus replacing the old version of the view for its owner. Th is ------ means that the view can be altered without dropping update v1 set employee_id=10 where employee_id=198; create or replace view v1 as select employee_id eid ,first_name fname,email id,d

epartment_id did from e1; update v1 set fname='King' where eid=10;

update v1 set fname='raj' where eid=10; select * from v1;

create or replace view v1 as select employee_id,first_name,email,department_id from e1 where department_id=30; select * from v1; create or replace view v1 as select e.employee_id,e.last_name,e.salary,d.department_id,d.department_name from e1 e join d1 d on e.department_id=d.department_id update v1 set salary=99999 where employee_id=10; ---- ERROR create or replace view v1 as select employee_id,last_name,department_id from e1 where department_id=50 with check option; NOTE :- If there is an attempt to perform DML operations on rows that the view h as not selected, an error is displayed, along with the constraint name if that h as been ----- specified. update v1 set department_id=20 where department_id=50; select * from v1; update v1 set employee_id=999 where employee_id=10; select * from v1; create or replace view v1

as select employee_id,last_name,department_id from e1 where department_id=50 with read only; NOTE :- Any attempt to remove a row from a view with a read-only constraint resu lts in an error: ------update v1 set employee_id=198 where employee_id=999; select * from v1; create or replace force view v1 as select employee_id,last_name,salary from aaaa; select * from user_objects where object_name='V1'; create table aaaa as select * from employees; select * from user_objects where object_name='V1'; select * from v1; select * from user_objects where object_name='V1'; create or replace view v1 as select distinct salary from e1; update v1 set salary=9999 where salary=13000; select * from v1; drop view v1; View Information DESCRIBE user_views SELECT text FROM user_views WHERE view_name = 'EMP_DETAILS_VIEW'; SEQUENCES -----------

CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}]; In the syntax: -------------sequence is the name of the sequence generator INCREMENT BY n specifies the interval between sequence numbers, where n is an in teger (If this clause is omitted, the sequence increments by 1.) START WITH n specifies the first sequence number to be generated (If this clause is omitted, the sequence starts with 1.) MAXVALUE n specifies the maximum value the sequence can generate NOMAXVALUE specifies a maximum value of 10^27 for an ascending sequence and a descending sequence (This is the default option.) MINVALUE n specifies the minimum sequence value NOMINVALUE specifies a minimum value of 1 for an ascending sequence and (10^26) f or a descending sequence (This is the default option.) create sequence s1 start with 100 increment by 10 minvalue 1 cache 3 maxvalue 200 nocycle; create table e2(employee_id number(10),last_name varchar2(10),salary number(10), department_id number(10)) NEXTVAL returns the next available sequence value. It returns a unique value eve ry time it is referenced, even for different users. CURRVAL obtains the current sequence value. NEXTVAL must be issued for that sequence before CURRVAL contains a value. insert into e2 values(s1.nextval,'Meghna','52000',20); select * from e2; insert into d2 values (s1.currval-1,'Admin',200,1700); select * from d2; 1 for

ALTER SEQUENCE sequence [INCREMENT BY n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}]; You must be the owner or have the ALTER privilege for the sequence. Only future sequence numbers are affected. The sequence must be dropped and re-created to restart the sequence at a different number. DESCRIBE user_sequences SELECT sequence_name, min_value, max_value, increment_by, last_number FROM user_sequences; (The LAST_NUMBER column displays the next available sequence number if NOCACHE is specified.) -------------------------------------------------------------------------------------------------------------------------------------------------------------------

An index: ------------Is a schema object Can be 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 that it indexes Is used and maintained automatically by the Oracle server How Are Indexes Created? 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. create index x1 on d2(department_id); select * from user_indexes where table_name='D2'; select * from d2; --------------------------------------------------------------------------------

---------------------------------------------------------------USER_SYNONYMS View The USER_SYNONYMS dictionary view describes private synonyms (synonyms that are owned by you). You can query this view to find your synonyms. You can query ALL_SYNONYMS to fin d out the name of all of the synonyms that are available to you and the objects on which these synonyms apply. The columns in this view are: SYNONYM_NAME: Name of the synonym TABLE_OWNER: Owner of the object that is referenced by the synonym TABLE_NAME: Name of the table or view that is referenced by the synonym DB_LINK: Name of the database link reference (if any) create synonym emp for hr.employees

You might also like