Skip to content

Commit

Permalink
Merge pull request #1978 from souvikpramanikgit/add-sqlviews
Browse files Browse the repository at this point in the history
Add SQL Views
  • Loading branch information
ajay-dhangar authored Nov 10, 2024
2 parents 75ec917 + 294e8b0 commit f78c7c6
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions docs/languages/SQL/sql-33.md
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.

0 comments on commit f78c7c6

Please sign in to comment.