Skip to main content

Pseudo Column in Oracle #Diksha Oracle Training Centre

 

Pseudo Column in Oracle.

Hello Everyone,

Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle.

Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In  Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table.

Below are the Pseudo Column commonly used in Oracle Database:

ROWID

ROWNUM

LEVEL

CURVAL

NEXTVAL

 

ROWID: When you create table and insert the data in a table , For Each row in the database , the ROWID column return’s a row address. The ROWID contains three information about row address i.e. File no , Data Block No  and Record No.

ROWID can returns values by using select statement in any table. For each row there is unique ID generated by Oracle even for Duplicate values. Using any query ROWID is very fast as it’s now where the value for each row in a table.

Example :

Select Max(ROWID) , emp_id

from emp

group by emp_id;

Result you will see in Hexadecimal for each row you will see different ROWID that’s why it’s easy to know the ROWID for duplicate rows as well.

Here in table emp we have duplicates records so query for removing duplicate rows from a table is as below:

Delete from emp where rowid not in

 (select max(rowid)

from emp group by emp_id);

Here we use subquery where inner query will return only 1 value for each dept_no as we use function and group by clause and in main query we mention not in so it will delete all the duplicate records apart from inner query which returns the value. We can also use MIN function to eliminate duplicate records.

Output of both Query is as below.

 

Note : Removing Duplicate records by using self join or other will perform slow as compared to ROWID as  as it’s know the position and value for each row.

ROWNUM : ROWNUM is a Pseudo column where  Oracle engine maintains the number of each record inserted by users in table. With the help of ROWNUM we can see the data according to the record inserted in the table.

Example:

 

SELECT * FROM EMP WHERE ROWNUM <= 4; 

SELECT * FROM EMP WHERE ROWNUM <=7;

Here 2nd query will give the first 7 rows of the table and 1st query give result against the condition mentioned in where condition.

Output of both the query is as below:

 


 

LEVEL: Level is a Pseudo column and it’s for each row returned by a hierarchical query, the LEVEL of Pseudo column returns 1 for a root node, 2 for a child of a root, and so on depends how you use.

Example :

SELECT Level AS Sequence

FROM Dual

CONNECT BY Level <= 7

Explanation:

Above query will execute Level having initial value 1 from dual, which is dummy table. In query, condition will be checked and relationship created using Connect By between Level having value 1 and the specified condition.

Note : Level can be helpful to start a sequence of number from any particular initial value.

Output of the above query is as below:

 


 

CURVAL and NEXTVAL

Before Describing Pseudo Column we need to know about Sequence. Sequence is an object which is stored in database that can generate unique sequential values , Sequence basically used in primary or unique Key . We can use Pseudo Column CURVAL and NEXTVAL in SQL statements. 

Syntax for creating Sequence:

 

CREATE SEQUENCE emp_seq

 START WITH  1

 INCREMENT BY   1

 NOCACHE

 NOCYCLE;

 

Here we created sequence emp_seq which can be used on any tables to select or insert the value in table by using pseudo column i.e CURVAL and NEXTVAL

CURVAL : CURVAL is a Pseudo Column which mainly used in sequence to find the current value of particular column . It’s return the current value of Sequence.

Example

 SELECT emp_seq.curval FROM DUAL;

Output of above query is as below:

 


NEXTVAL : NEXTVAL is a Psuedo Column which used to insert next vale in table .

 

It simply increments the sequence and returns the next value.

Example : I created a temporary table test where for ID column I use sequence emp_seq for next value.

INSERT INTO test VALUES (emp_seq.nextval, 'DHEERAJ', 'MDB', 'BIHAR');

INSERT INTO test VALUES (emp_seq.nextval, 'Sakshi', 'Ghaziabad', 'UP');

INSERT INTO test VALUES (emp_seq.nextval, 'Diksha', 'Delhi', 'NEW DELHI');

INSERT INTO test VALUES (emp_seq.nextval, 'Abhishek', 'PATNA', 'BIHAR');

COMMIT;

Select ID , Name from test where rownum <=5;

Output of above statement is as below:

 


NOTE : if we want to see the current value of sequence by using select statement it will give you the last value which we inserted in the table . Sequence is important to insert the increment value directly to the tables and we can see the exact value of sequence by using sequence.CURRVAL .

Hope you are able to understand the importance of Pseudo Column in Oracle. Pseudo column in Oracle database is very frequently used and in interview they can ask you about Pseudo column questions  and also they can ask how it works and it's benefit. As in Interview I saw lot’s of candidate only talk about ROWID . So please go through all the Pseudo column which I mentioned and try to use in query and see the performance and also get the benefits of using sequence by using Pseudo column CURVAL and NEXTVAL in any tables.

This Blog is important for Candidate who is preparing for interview and also good for candidate who is learning RDBMS. Please read this blog carefully and let me know if you have any questions.

Thanks.

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li