VIRTUAL
VIRTUAL in SQL is a keyword used in the context of creating virtual tables or virtual columns. A virtual column is not stored in the database but is computed in real-time when a query is made. Use of VIRTUAL columns can assist in simplifying queries, contribute to space conservation, and aid with performance optimization under certain conditions.
Example
Output
Explanation
The above example demonstrates the usage of VIRTUAL in MySQL. It creates a VIRTUAL column full_name
in the employee
table. This column is generated by concatenating first_name
and last_name
. When a new record is inserted into the table, MySQL automatically calculates and stores the value for the full_name
column.
Example
Output
Explanation
In the above SQL code, a table called Students is created with columns for StudentID, FirstName, LastName, Age, GradeLevel, and GPA. In addition, there is a VIRTUAL column, TotalScore, which is calculated on the fly and doesn’t store any data permanently. It’s computed as GPA multiplied by GradeLevel each time data is read out of the table.