Views and Index DEMO ORACLE19C 1738294684182
Views and Index DEMO ORACLE19C 1738294684182
--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.
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 created.
CUS_NAME CUS_CITY
-------------------- --------------------
KAVI MADURAI
ARUN MADURAI
VARUN MADURAI
ALLEN MADURAI
CUS_NAME
--------------------
ALLEN
ARUN
KAVI
MARUN
PARUN
PAVI
RAVI
SARUN
VARUN
9 rows selected.
ACC_NO
----------
11
12
13
14
15
16
17
18
19
9 rows selected.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
View dropped.
View created.
8 rows selected.
-- QUERY: CREATE a view that calculates the total balance per customer
View created.
8 rows selected.
View created.
CUS_NAME CUS_BALANCE
-------------------- -----------
ALLEN 275600
KAVI 112233
RAVI 50000
VARUN 34233
View created.
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
1 row created.
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.
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.
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%';
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.