0% found this document useful (0 votes)
10 views6 pages

Views and Index DEMO ORACLE19C 1738294684182

The document provides an overview of creating and using views in Oracle, including examples of simple views, views with WHERE clauses, joins, and aggregations. It also discusses the limitations of modifying views and the different types of indexes that can be created to improve query performance. Additionally, it highlights the conditions under which views can be updatable and the implications of using indexes in database operations.

Uploaded by

akashkalimuthu4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Views and Index DEMO ORACLE19C 1738294684182

The document provides an overview of creating and using views in Oracle, including examples of simple views, views with WHERE clauses, joins, and aggregations. It also discusses the limitations of modifying views and the different types of indexes that can be created to improve query performance. Additionally, it highlights the conditions under which views can be updatable and the implications of using indexes in database operations.

Uploaded by

akashkalimuthu4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

--VIEWS in Oracle is a virtual table based on the result of a query

--It does not store data itself but retrieves data from the underlying base tables.
--CREATE VIEW view_name AS SELECT column1, column2, ..., columnN FROM table_name
WHERE condition;
-----------------------------------------------------------------------------------
-----------------------------
--QUERY:Create a simple view that selects customer names and their cities
SQL> CREATE VIEW customer_city_view AS SELECT CUS_NAME, CUS_CITY FROM customer;

View created.

SQL> SELECT * FROM CUSTOMER_CITY_VIEW;

CUS_NAME CUS_CITY
-------------------- --------------------
KAVI MADURAI
RAVI SIVAKASI
PAVI CHENNAI
ARUN MADURAI
VARUN MADURAI
MARUN ERODE
PARUN CHENNAI
SARUN TRICHY
ALLEN MADURAI

9 rows selected.

--VIEW WITH WHERE CALUSE


--QUERY:creating a view that only shows customers who live in a specific city
SQL> CREATE VIEW customer_city AS SELECT CUS_NAME, CUS_CITY FROM customer WHERE
CUS_CITY = 'MADURAI';

View created.

SQL> SELECT * FROM CUSTOMER_CITY;

CUS_NAME CUS_CITY
-------------------- --------------------
KAVI MADURAI
ARUN MADURAI
VARUN MADURAI
ALLEN MADURAI

--View with JOIN


--QUERY:Show the customer's name along with their account balance
SQL> SELECT CUS_NAME FROM CUSTOMER;

CUS_NAME
--------------------
ALLEN
ARUN
KAVI
MARUN
PARUN
PAVI
RAVI
SARUN
VARUN
9 rows selected.

SQL> SELECT ACC_NO FROM ACCOUNT;

ACC_NO
----------
11
12
13
14
15
16
17
18
19

9 rows selected.

SQL> INSERT INTO DEPOSIT VALUES('ALLEN',11);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('ALLEN',19);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('KAVI',12);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('RAVI',13);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('RAVI',14);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('RAVI',15);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('VARUN',16);

1 row created.

SQL> INSERT INTO DEPOSIT VALUES('VARUN',17);

1 row created.

SQL> CREATE VIEW customer_account_info AS SELECT c.CUS_NAME, c.CUS_CITY, a.ACC_NO,


a.BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a ON
A.ACC_NO = D.ACC_NO;
CREATE VIEW customer_account_info AS SELECT c.CUS_NAME, c.CUS_CITY, a.ACC_NO,
a.BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a ON
A.ACC_NO = D.ACC_NO
*
ERROR at line 1:
ORA-00955: name is already used by an existing object

SQL> DROP VIEW CUSTOMER_ACCOUNT_INFO;

View dropped.

SQL> CREATE VIEW customer_account_info AS SELECT c.CUS_NAME, c.CUS_CITY, a.ACC_NO,


a.BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a ON
A.ACC_NO = D.ACC_NO;

View created.

SQL> SELECT * FROM CUSTOMER_ACCOUNT_INFO;

CUS_NAME CUS_CITY ACC_NO BALANCE


-------------------- -------------------- ---------- ----------
ALLEN MADURAI 11 230000
ALLEN MADURAI 19 45600
KAVI MADURAI 12 112233
RAVI SIVAKASI 13 10000
RAVI SIVAKASI 14 15000
RAVI SIVAKASI 15 25000
VARUN MADURAI 16 1233
VARUN MADURAI 17 33000

8 rows selected.

--View with Aggregation

-- QUERY: CREATE a view that calculates the total balance per customer

SQL> CREATE VIEW CUS_TOTAL_BALANCE AS SELECT c.CUS_NAME, a.ACC_NO, SUM(a.BALANCE)


AS CUS_BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account
a ON A.ACC_NO = D.ACC_NO GROUP BY C.CUS_NAME,A.ACC_NO;

View created.

SQL> SELECT * FROM CUS_TOTAL_BALANCE;

CUS_NAME ACC_NO CUS_BALANCE


-------------------- ---------- -----------
ALLEN 11 230000
ALLEN 19 45600
KAVI 12 112233
RAVI 13 10000
RAVI 14 15000
RAVI 15 25000
VARUN 16 1233
VARUN 17 33000

8 rows selected.

SQL> CREATE VIEW CUS_TOTAL_BALANCE AS SELECT c.CUS_NAME, SUM(a.BALANCE) AS


CUS_BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a
ON A.ACC_NO = D.ACC_NO GROUP BY C.CUS_NAME;
CREATE VIEW CUS_TOTAL_BALANCE AS SELECT c.CUS_NAME, SUM(a.BALANCE) AS CUS_BALANCE
FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a ON A.ACC_NO
= D.ACC_NO GROUP BY C.CUS_NAME
*
ERROR at line 1:
ORA-00955: name is already used by an existing object

SQL> CREATE VIEW CUS_TOTAL_BALANCE1 AS SELECT c.CUS_NAME, SUM(a.BALANCE) AS


CUS_BALANCE FROM customer c JOIN DEPOSIT D ON C.CUS_NAME=D.CUS_NAME JOIN account a
ON A.ACC_NO = D.ACC_NO GROUP BY C.CUS_NAME;

View created.

SQL> SELECT * FROM CUS_TOTAL_BALANCE1;

CUS_NAME CUS_BALANCE
-------------------- -----------
ALLEN 275600
KAVI 112233
RAVI 50000
VARUN 34233

--NESTED VIEW - VIEW CREATED FROM OTHER VIEW


-----------------------------------------------------------------------------------
------------------------------
--QUERY:CREATE VIEW THAT SHOWS CUSTOMER WHO HAVE BALANCE > 50000 ONLY FROM EXISTING
VIEWS
SQL> CREATE VIEW CUS_BALANCE_ABOVE_50000 AS SELECT CUS_NAME, CUS_BALANCE FROM
CUS_TOTAL_BALANCE1 WHERE CUS_BALANCE > 50000;

View created.

SQL> SELECT * FROM CUS_BALANCE_ABOVE_50000;

CUS_NAME CUS_BALANCE
-------------------- -----------
ALLEN 275600
KAVI 112233

--MODIFYING VIEWS
--views are generally not updatable (meaning you can't directly INSERT, UPDATE, or
DELETE data through them) unless they meet certain conditions.
--A view is updatable when: It selects from a single table, It doesn’t contain
aggregates, GROUP BY clauses, or joins, It does not include any subqueries or
DISTINCT keywords.
--UPDATES WILL GET REFLECTED IN ORIGINAL TABLE

SQL> INSERT INTO CUSTOMER_CITY VALUES('PRIYA','VIRUDUNAGAR');

1 row created.

SQL> SELECT * FROM CUSTOMER_CITY;

CUS_NAME CUS_CITY
-------------------- --------------------
KAVI MADURAI
ARUN MADURAI
VARUN MADURAI
ALLEN MADURAI
SQL> SELECT CUS_NAME,CUS_CITY FROM CUSTOMER;
CUS_NAME CUS_CITY
-------------------- --------------------
PRIYA VIRUDUNAGAR
KAVI MADURAI
RAVI SIVAKASI
PAVI CHENNAI
ARUN MADURAI
VARUN MADURAI
MARUN ERODE
PARUN CHENNAI
SARUN TRICHY
ALLEN MADURAI

10 rows selected.
SQL> INSERT INTO CUSTOMER_CITY(CUS_NAME,CUS_CITY) VALUES('KALIS','SIVAKASI');

1 row created.

SQL> SELECT * FROM CUSTOMER_CITY;

CUS_NAME CUS_CITY
-------------------- --------------------
KAVI MADURAI
ARUN MADURAI
VARUN MADURAI
ALLEN MADURAI
SQL> SELECT CUS_NAME,CUS_CITY FROM CUSTOMER;

CUS_NAME CUS_CITY
-------------------- --------------------
PRIYA VIRUDUNAGAR
KALIS SIVAKASI
KAVI MADURAI
RAVI SIVAKASI
PAVI CHENNAI
ARUN MADURAI
VARUN MADURAI
MARUN ERODE
PARUN CHENNAI
SARUN TRICHY
ALLEN MADURAI

11 rows selected.

--COULD NOT DELETE/UPDATE FROM VIEW

--INDEX -- ndexes are used to improve the performance of database queries by


allowing the database to find rows more quickly
--B-tree Index is default
--SYNTAX: create index index_name on table_name(column_name)
SQL> create index br1 on branch(br_name);
create index br1 on branch(br_name)
*
ERROR at line 1:
ORA-01408: such column list already indexed

--by default oracle creates index on primary keys (implicitly created)

SQL> create index br1 on branch(br_city);


Index created.
--queries related branch city will execute fast
SQL> select br_city,count(*) from branch group by br_city;

BR_CITY COUNT(*)
-------------------- ----------
SIVAKASI 4
VIRUDUNAGAR 1
MADURAI 4

--UNIQUE INDEX ensures that no two rows in a table have the same value for the
indexed columns.
--unique index is automatically created when you define a unique constraint on a
column.
SQL> create unique index i1 on branch(assets);

Index created.
--bitmap index is used for columns with a low cardinality (i.e., columns with a
small number of distinct values)
SQL> create bitmap index i2 on customer(cus_city);

Index created.
--composite index is an index on multiple columns. It helps improve the performance
of queries that filter on multiple columns.
SQL> create index i4 on customer(cus_city,age);

Index created.
SQL> select * from customer where age<30 and cus_city like 'T%';

CUS_NAME CUS_CITY PHONE EMAIL


DOB AGE SUP_NAME
-------------------- -------------------- ----------
----------------------------------- --------- ---------- --------------------
SARUN TRICHY
28 KAVI

-- function-based index is created based on the result of a function or expression


SQL> CREATE INDEX idx_lower_name ON customer(lower(email));

Index created.
--Primary Index is an index automatically created on a table when a Primary Key
constraint is defined. The primary index ensures the uniqueness of the rows based
on the primary key
--Secondary Index is an index created on a table’s non-primary key columns.
(explcitly created)
SQL> create index i2 on customer(cus_city);

Index created.

You might also like