Open In App

Difference Between View and Table

Last Updated : 02 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the world of database management systems (DBMS), views and tables are fundamental concepts that help in storing and managing data efficiently. While both terms are used frequently, they serve distinct purposes within a relational database.

Understanding the difference between a view and a table is crucial for anyone working with SQL and relational databases. In this article, we will explain the key differences between views and tables, their definitions, uses, and practical examples

Key Differences Between Views and Tables

Following are the differences between the view and table:

Basis

View

Table

Definition

A view is a virtual table that derives its data from one or more base tables through a SQL query.

A table is a physical object that stores data in the form of rows and columns.

Dependency

A view depends on underlying tables or other views for data retrieval.

A table is an independent data object that directly stores information.

Database space

Views do not occupy physical storage space. They only store the query structure.

Tables consume physical space to store data in a database.

Manipulate data

Data cannot be added, updated, or deleted directly from a view.

Data in tables can be added, updated, or deleted using SQL commands like INSERT, UPDATE, and DELETE.

Recreation

Views can be easily recreated or replaced using the CREATE OR REPLACE statement.

A table can only be created or dropped; data in the table can be manipulated.

Aggregation of data

Views can aggregate data from multiple tables or perform complex joins.

Tables store raw, unaggregated data and do not inherently support data aggregation.

table/view relationship

Views can combine multiple tables using joins, unions, etc.

Tables can have primary keys, foreign keys, and indexes to maintain relationships between different tables.

What is View in DBMS?

A view in a Database Management System (DBMS) is a virtual table that does not store any data itself but presents data from one or more tables in a specific manner. It is also called a derived table because it is derived from another table.

It is essentially a stored SQL query that is executed when the view is accessed, returning a result set. Views are useful for presenting a specific subset of data from a database and can simplify complex queries by encapsulating them into a single virtual table.

Syntax

Create or Replace view <view name> as 
select column_list
from <table_name>

Here,  <view name> is name of the view, and <table_name> is the base table from which data is fetched.

Example of View With SQL Query

Here's an example of how a view works: Suppose we have an "employees" table with attributes like employee_id, first_name, last_name, and department_id. We want to create a view that shows the full names of employees along with their department names.

Query

CREATE VIEW employee_details AS
SELECT e.employee_id, CONCAT(e.first_name,' ', e.last_name) AS full_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;

Explanation:

In this case, we create a view called "employee_details" that combines data from the "employees" and "departments" tables, displaying employees' full names along with their department names. This view can be queried like any regular table to retrieve the desired information.

What is Table in DBMS?

A table is a fundamental component of a relational database. It consists of rows and columns that store actual data. Each row in a table represents a unique record, and each column corresponds to a specific attribute of that record. Tables must have unique names, cannot use SQL reserved words, and are case-insensitive. They allow easy data creation and manipulation.

Syntax

CREATE TABLE <table_name> (
<column_name> <data_type>,
<column_name> <data_type>,
...
);

Here, <table_name> represents the name of the table, and <column_name> defines each column in the table along with its corresponding data type.

Example of Table With SQL Query

Creating a table in sql can be done as follows for instance : Consider we create a student table which would contain a student id, name, age and class.

Query:

CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade VARCHAR(2)
);

Explanation:

The above SQL code generates a table called "students" with columns consisting of the primary key associated with student_ids, student names, their ages and grades.

Conclusion

In conclusion, while both views and tables are crucial elements in a relational database, they serve different purposes. A view is a virtual table that allows us to present data from base tables in a specific format or structure, without storing any data itself. It provides a logical abstraction and simplifies complex queries. On the other hand, a table is a physical object in the database that stores actual data in rows and columns.


Next Article

Similar Reads