Skip to main content

Difference view and Materialized view in Oracle SQL PLSQL Interview Question and Answer.

 "This is excepted question in oracle interview. It is asked in oracle technical support,PLSQL developer, SQL Analyst and SQL developer."

INTRODUCTION:

View and Materialized view are called as the database object or the named query. View is the virtual table which stores the select statement from the base table by execution of the sql query at the runtime. View Query is running for the view definition. In the case of the materialized view, the data from the base table is loaded in the temporary or physical memory location by which the materialized view gets refreshed and all the dependent tables on the materialized view are also get updated after refresh operation. Materialized views are used in lakhs lines of code. In the data lake, tables are dependent on the master table to get updated where more than lakhs line of codes, today every reporting is based on this, in that case materialized view are used and preferred on the master tables.

An activity of materialized view will be refreshed by the below command, and it is having the physical data  retreived from the base table is shown with the help of select statement till refresh.
exec DBMS_MVIEW.refresh('Table name which you want to refresh'); Materialized view is used because view is failed to display the data. View and materialized view are used to provide the result of the select query. There is process of updating MV is called as MV maintenance.

View is the important concept of the database management system.Whenever an query is fired,view returns the updated data from the base table or the original table and change is clearly seen in it.

View and materialized view can be dropped using the syntax:

drop view view_name;

drop materialized materialized_view_name;

QUERY:

Below are the shared queries for your practice as Lab session.
create table emp(EMPNO NUMBER(4),ENAME VARCHAR2(10),JOB VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7,2),COMM NUMBER(7,2),DEPTNO NUMBER(2));

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7369,'SMITH','CLERK',7902,'17-Dec-80','800','',20);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7499,'ALLEN','SALESMAN',7698,'20-Feb-81','1600','300',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7521,'WARD','SALESMAN',7698,'22-Feb-81','1250','500',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7566,'JONES','MANAGER',7839,'02-Apr-81','2975','',20);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7654,'MARTIN','SALESMAN',7698,'28-Sep-81','1250','1400',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7698,'BLAKE','MANAGER','7839','01-May-81','2850','',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7782,'CLARK','MANAGER',7839,'09-Jun-81','2450','',10);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7788,'SCOTT','ANALYST',7566,'19-Apr-87','3000','',20);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7839,'KING','PRESIDENT','','17-Nov-81','5000','',10);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7844,'TURNER','SALESMAN',7698,'08-Sep-81','1500','0',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7876,'ADAMS','CLERK',7788,'23-May-87','1100','',20);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7900,'JAMES','CLERK',7698,'03-Dec-81','950','',30);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7902,'FORD','ANALYST',7566,'03-Dec-81','3000','',20);

INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(7934,'MILLER','CLERK',7782,'23-Jan-82','1300','',10);



create or replace view emp_20
as
select * from emp where deptno=20;



select * from emp_20


create materialized view emp_20_mv
as
select * from emp where deptno=20;



select * from emp_20_mv


delete from emp;





select * from emp;






select * from emp_20;

When we delete all the records from the orginal or the base table, view will reflect no records.



select * from emp_20_mv;

When we delete all the records from the emp table, materialized view is still reflecting the old records.So,we need to remove the old records by firing or executing the below mention refresh command.

exec DBMS_MVIEW.refresh('emp_20_mv');


select * from emp_20_mv















When we insert all the records in the orginal or the base table, view will reflect the required records.






When we insert all the records for the emp table, materialized view is still reflecting no records.So,we need to insert the records by firing or executing the below mention refresh command.







DIFFERENCE:












VIEW: 

Materialized View:

It is slower than the materialized view.

 

It is faster than the view.

 

 

When we will update the base table, there is no need refresh it.

When we will update the base table, there is need to manual refresh it.Refreshing technique is used to refresh the materialized view’s temporary table.

Syntax: Create view view_name AS SELECT columns FROM TABLES [WHERE CONDITIONS];

Syntax:

Create Materialized view_name AS

SELECT columns FROM TABLES

[WHERE conditions];

Here, materialized is the keyword.

 

Here, view is the keyword.

Here, materialized is the keyword.

 

It contains the select SQL statement.

It contains the data or output of the select statement.

It is not required the space for holding the data.

It is required the space for holding the data.

 

If the base table is dropped, then also the view will not be accessible.

If the base table is dropped, then also the view will be accessible.

 

DML operations can directly performed on the view.

DML operations cannot be directly performed on the materialized view.

 

 

 

It does not store the data, but it stores the SQL query.

It stores the data and query.

 

 

 

Base table dependency doesn't on the other tables, then it is preferred to use the view.

Base table dependency is on the other tables, then it is preferred to use the materialized view.

 

 

 

Refresh activity does not take place.

 

Refresh activity takes place.

 

It stores the data of the remote table which is called snapshot. Snapshot is the older form of materialized view which is used in the data warehouse.

 

 

View fetches the latest data from the base table.

Materialized view does not fetch the latest data from the base table, it fetches the data from the physical table.

 

 

 

Any change in the base table data will reflect in the view immediately.

Any change in the base table ‘s data will not reflect in the materialized view immediately.

 

It is called as the online data retrieved from base tables.

It is called as the offline data or delay data retrieved from base tables.

 

 

It retrieves the data from the base table.

 

It retrieves the data from the materialized table which need to be manually updated through refresh from the underlying base tables.

 

 

It is used in the simple queries.

It is used in the complex queries.

 

Indexing cannot be applied.

Indexing can be applied.

 

 

Performance is slow for select queries.

Performance is fast for select queries

Maintenance or updating cost don't occur.

Maintenance or updating cost don't occur.

 

When the table's data is updated frequently and its access infrequently.

When the table's data is updated infrequently and its access frequently.

 

It is dynamic because it will show the updated data.

 

It is static because it will show the updated data after last refresh.

 

Storage cost is present.

 

Storage cost is not present.

 

 

Execution of automatic action method or trigger to refresh is not required.

 

Execution of automatic action method or trigger to refresh.

 

 

Don't require the physical copy of the database.

 

It requires the physical copy of the database.

 

Execution of the query will provide result or output which is not stored in the disk or the database.

 

Execution of the query will provide result or output which is stored in the disk or the database.

 

It is designed with fixed architecture approach for defining with sql standard.  

 

It is designed with generic architecture approach for not defining with sql standard.  

 

Row id is similar with the original table.

 

Row id is entirely different with the original table.

 

 


 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       





 

 

 






















Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

"This is one of the most important questions that interviewer might be asking in an interview.Infyosis,Linked group,Kyndryl,Vodafone Idea,IBM and Mphasis etc" INTRODUCTION: It is said to be set operators which is used to combine, add or concatenate two or more result sets. It is used in the multiple select statements where to combine to produce the final desired results.In other words,multiple select statements of similar column name in same order and having same data type to produce the unified result.In MIS datalake,when you are adding the modules of GST reconciliation which is to be used in micros in Excel or VB script, union and union all set operator is preferred for eliminating duplicate records or keeping duplicate records as per business requirement. Column must be of the same datatype for these operations of one select statement with another select statements. Columns must be in the same order in this set operators in the multiple select statements. Same number of co

Difference between decode and case oracle sql plsql interview question and answer

" This is the most accepted question for PLSQL developer role.It has been asked in many interviews like Mphasis and Kyndryl. "  INTRODUCTION: Case was came after oracle database version 8.1.6 whereas decode was came before oracle database version 8.1.6 .Decode and case statements in an oracle work or acts like if then elsif conditional statements. if sal='2000000' then 'General Manager' elsif sal='3000000' then 'Senior Manager' elsif sal='1200000' then 'Manager' else 'Employee' end if; create table student_details(Student_RollNo Number,Student_Name Varchar2(20),student_marks Number,student_result varchar2(1)); insert into student_details values (100,'Rahul',90,'P'); insert into student_details values (101,'Rakesh',30,'F'); insert into student_details values (102,'Ram',60,'P'); insert into student_details values (103,'Rohan',10,'F'); insert into student_detai