-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1978 from souvikpramanikgit/add-sqlviews
Add SQL Views
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
id: sql-views | ||
sidebar_position: 33 | ||
title: "SQL Views" | ||
sidebar_label: "SQL Views" | ||
description: "SQL Views are virtual tables that represent a saved query in the database." | ||
tags: [sql, dbms, database, views, virtual tables] | ||
--- | ||
|
||
A **View** in SQL is a virtual table based on a result set of a `SELECT` query. Views can simplify complex queries, enhance security by limiting data access, and provide a more user-friendly way to access data. | ||
|
||
### Advantages of Views | ||
|
||
- **Simplicity**: Abstract complex queries, allowing easier access to data. | ||
- **Security**: Restrict access to specific data by allowing users to query a view instead of underlying tables. | ||
- **Data Consistency**: Ensure data consistency by allowing standardized queries to run on views instead of individual tables. | ||
|
||
### Syntax | ||
|
||
```sql | ||
CREATE VIEW view_name AS | ||
SELECT column1, column2, ... | ||
FROM table_name | ||
WHERE condition; | ||
``` | ||
|
||
### Example | ||
|
||
**Example 1: Creating a Simple View** | ||
|
||
Create a view named CustomerView that displays only selected columns from the Customers table. | ||
|
||
```sql | ||
CREATE VIEW CustomerView AS | ||
SELECT CustomerID, CustomerName, Country | ||
FROM Customers; | ||
``` | ||
|
||
To query data from this view: | ||
|
||
```sql | ||
SELECT * FROM CustomerView; | ||
``` | ||
|
||
**Example 2: View with Join** | ||
|
||
Create a view that combines data from multiple tables to show order details. | ||
|
||
```sql | ||
CREATE VIEW OrderDetailsView AS | ||
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Orders.Amount | ||
FROM Orders | ||
JOIN Customers ON Orders.CustomerID = Customers.CustomerID; | ||
``` | ||
|
||
**Example 3: Updating Data through a View** | ||
|
||
In some SQL databases, views can be updatable. This depends on database constraints and view complexity. Here’s an example of updating a view: | ||
|
||
```sql | ||
UPDATE CustomerView | ||
SET Country = 'USA' | ||
WHERE CustomerID = 1; | ||
``` | ||
|
||
Note: Not all views are updatable. Complex views (e.g., those with joins, group functions) may not support updates. | ||
|
||
### Managing Views | ||
|
||
* **Modify a View:** Use CREATE OR REPLACE VIEW to modify an existing view without dropping it. | ||
|
||
```sql | ||
CREATE OR REPLACE VIEW CustomerView AS | ||
SELECT CustomerID, CustomerName | ||
FROM Customers | ||
WHERE Country = 'USA'; | ||
``` | ||
|
||
* **Delete a View:** Use DROP VIEW to remove a view from the database. | ||
|
||
```sql | ||
DROP VIEW view_name; | ||
``` | ||
|
||
**Notes :** | ||
* Virtual Table: A view doesn’t store data; it only stores the SQL query that defines it. | ||
* Read-Only Views: Some views are read-only depending on complexity and database constraints. | ||
* Permissions: Views can provide restricted data access to specific users without exposing full tables. | ||
|
||
SQL Views are powerful tools that enhance database usability, security, and data management by abstracting and simplifying access to underlying tables. |