0% found this document useful (0 votes)
56 views

A View Is Also Called Virtual Table Stored Query Window Create View Viewname As Query (With Check Option) (With Read Only)

1. A view is a virtual table that is defined by a query. Views allow restricting access to data, simplifying queries, and increasing data independence. 2. Constraints are used to define rules for the data in database tables to maintain integrity. Common constraints include primary keys, foreign keys, unique constraints, check constraints, and not null constraints. 3. Reports allow displaying data from database queries in a formatted, readable way. Actions like break, compute, format, and spool control report display and output.

Uploaded by

chowdareiam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

A View Is Also Called Virtual Table Stored Query Window Create View Viewname As Query (With Check Option) (With Read Only)

1. A view is a virtual table that is defined by a query. Views allow restricting access to data, simplifying queries, and increasing data independence. 2. Constraints are used to define rules for the data in database tables to maintain integrity. Common constraints include primary keys, foreign keys, unique constraints, check constraints, and not null constraints. 3. Reports allow displaying data from database queries in a formatted, readable way. Actions like break, compute, format, and spool control report display and output.

Uploaded by

chowdareiam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

A view is also called

1. Virtual Table

2. Stored Query

3. Window

CREATE VIEW VIEWNAME


AS QUERY
[with check option] [with read
only]
Example
CREATE VIEW empview
As
SELECT empno, ename,
deptno
FROM
Empno
employee;
Ename Job Salary comm

Empno Ename Salary


View
Why a view is used

 To restrict access to data of a


table
 To simplify the query

 To increase data
Independence

NOTE: The table on which the view is based is


called as BASE table
Simplifying Query using View
CREATE VIEW empvu10
AS
SELECT empno,ename,job
FROM emp
WHERE deptno =10
Using View

 Desc empvu10
 Select * from empvu10
Create a view by using
column aliases in sub query
 Create view salvu30
As select empno employee_number,
ename name, sal salary
From emp
Where deptno =30
Modifying the view

 CREATE OR REPLACE VIEW


EMPVW10
 (NAME,DESIGNATION,SALARY) AS
 SELECT ENAME,JOB,SAL FROM EMP
 WHERE DEPTNO=10
Creating a complex view

 Create view dept_sum_vu


(name, minsal, maxsal, avgsal)
As select d.dname, min(e.sal),
max(e.sal), avg(e.sal)
From emp e, dept d
Where e.deptno=d.deptno
Group by d.dname;
Using the WITH CHECK
OPTION clause

 Create or replace view empvu20


As select *
From emp
Where deptno=20
With check option constraint empvu20_ck;
• Any attempt to change the deptno for
any row will fail because WITH CHECK
OPTION constraint
Denying DML operators
 Create or replace view empvu10
as select empno, ename, job,
From emp
Where deptno =10
With read only;

*any attempt to perform a DML on any


row will result in oracle server error.
Removing a view

 Drop view <viewname>


 Drop view empvu10;
Inline views
 Select a.ename, a.sal, a.deptno, b.maxsal
From emp a, (select deptno, max(sal) maxsal
From emp group by deptno ) b
Where a.deptno=b.deptno
And a.sal < b.maxsal;
* An inline view is a subquery with an
alias that you can use within SQL
statement
TOP-N Analysis

 Select rownum as rank, ename, sal


From (select ename, sal, from emp
Order by sal desc)
Where rownum <= 3;
* To display the top three earners
names and salaries from the emp
table
DATABASE OBJECTS
 OBJECT DESCRIPTION
TABLE basic unit of storage
composed of rows and
columns
View logically represents
subsets of data from one
or more tables
Sequence generates primary key values
Index improves the performance of
some queries
Synonym alternative name of an object
Cluster it is an area where we can store multiple
tables (in joins)
Creating a sequence

 Create sequence <sequencename>


[increment by integer]
[start with integer]
[maxvalue integer]
[Minvalue integer]
[cycle]
[cache integer]
Creating a sequence

 Create sequence rollno


increment by 1
start with 1
maxvalue 100;
Pseudo columns
 Currval returns the current value
of sequence
 Nextval returns the next value
sequence
 Rowid return rowid of a row
 Rownum returns the number
indicating in which order
oracle select rows
Usage of sequence

 Insert into students


values (rollno.nextval, ’ramesh’,2500);
 Insert into students

values (rollno.currval, ’rajesh’,4000);


Dropping a siequence

 Drop sequence <sequencename>


 Drop sequence rollno
index

 To speed of searching for a particular


value thereby improving the
performance of query
 To enforce uniqueness
 Create index <indexname> on table
(column)

Examples
 Create index empno_idx
on emp(empno);
 Create index empno_name_idx
on emp(empno,ename);

Dropping
Drop index <indexname>
Drop index empno_idx
clustering

 Clustering is a method of of storing


Tables that are often used together (in
joining) in one area of the disk. As
tables that are related are stored in
one area of the disk, performance of
the joining will improve
Creating a cluster

 Create cluster <clustername> [column


(column datatype)]
 Create cluster no (sno number(2));
Creating cluster index

 Create index idx_clus


on cluster no;
Placing a table into a
cluster
Create table stu
(sno number(2),…………) cluster no
(sno)
Create table address
(sno number(2),…………) cluster no
(sno)
synonyms

 To simplify accessing tables owned by


other users, create synonym.
 Example
create synonym emp for scott.emp;
reports
 A report Is information provided in a neat
and understandable format.
 The following are the action specifies what
to do in the reports.
 Skip :prints specified number of
empty lines
 Break :specifies on which column
the data has to be grouped
 Compute :computes the given function
 Ttitle : sets the top title
 Btitle : sets the bottom title
 Heading: if ‘|’ is used then heading is
split into multiple lines
 Justify : used to align heading either
to left, centre or right
 Format : used to display in the given
format
 Spool :used to store the result of the
query into an operating
system file
 Clear : used to clear the all given
conditions
Sample report
 ttitle "emp report"
 btitle "the end"
 Break on job skip 2
 compute sum of sal on job
 Set pagesize 42
 Set linesize 50
 Column sal format 99,999
 Column ename format a20 heading ‘employee|name’
 Column job heading ‘designation’ format a20
 spool d:\sai.doc
 select ename,job,sal from emp
 order by job;
 spool off
To clear the settings:
Ttitle off
Btitle off
Clear break
Clear column
Adding a constraint

 Alter table <tablename>


add [constraint <constraintname>
constraint type(column)]
 Example

alter table emp


Add constraint emp_pk primary
key(empno);
Dropping a constraint

 Alter table emp


drop constraint emp_pk;

If the constraint associated foreign key


use the cascade option
Alter table emp
Drop primary key cascade
Disabling and enabling

 Alter table emp


Disable constraint emp_pk cascade;

 Alter table emp


Enable constraint emp_pk
Viewing the constraints

 Select constraint_name,constrint_type,
search condition
From user_constraints
Where table_name =‘emp’;
Viewing the columns
associated with constraints
 Select constraint_name,column_name
from user_cons_columns
Where table_name = ‘emp’

You might also like