Lädt...


🔧 Views in SQL


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Views in SQL

A view in SQL is a virtual table that is created based on the result set of a query. It doesn't store data physically in the database like a table does. Instead, it dynamically retrieves data from one or more tables (or even other views) whenever it is queried.

Views simplify complex queries, abstract the database structure, and enhance security by exposing only specific data to the user.

Key Features of Views

1. Virtual Table:

A view behaves like a table when queried, but it doesn't actually store data. Instead, it fetches data from the base tables dynamically.

2. Simplification:

Views are used to simplify complex queries. You can encapsulate complex SELECT statements inside a view, making it easier for users to retrieve data without worrying about the underlying logic.

3. Security:

Views can be used to limit access to certain rows or columns in a table, ensuring that sensitive data is not exposed. For example, users can be granted permission to query a view without granting access to the underlying table.

4. Data Abstraction:

Views provide a layer of abstraction. If the structure of the underlying table changes, the view can be modified accordingly without impacting the applications or users that query the view.

5. Logical Data Independence:

Views provide a way to present data in a format that is independent of how it is stored. You can change the underlying table schema without affecting the view or its consumers.

Syntax to Create a View

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example

Suppose you have a table called employees:

CREATE TABLE employees (
emp_id NUMBER,
emp_name VARCHAR2(50),
department VARCHAR2(50),
salary NUMBER
);

INSERT INTO employees VALUES (1, 'John Doe', 'IT', 50000);
INSERT INTO employees VALUES (2, 'Jane Smith', 'HR', 45000);
INSERT INTO employees VALUES (3, 'James Brown', 'IT', 60000);
INSERT INTO employees VALUES (4, 'Mary Johnson', 'Finance', 70000);
COMMIT;

To create a view showing only employees in the IT department:

CREATE VIEW it_employees AS
SELECT emp_id, emp_name, salary
FROM employees
WHERE department = 'IT';

Now, you can query the view just like a table:

SELECT * FROM it_employees;

Output:

emp_id emp_name salary
1 John Doe 50000
3 James Brown 60000

Updating Data Through Views

If a view is based on a simple SELECT statement (single table, no joins or aggregations), it can also be used to insert, update, or delete data in the underlying table.

For example, updating the salary for employee John Doe using the view:

UPDATE it_employees
SET salary = 55000
WHERE emp_name = 'John Doe';

The employees table will now reflect the updated salary for John Doe.

However, complex views that use joins, aggregations, or derived columns may not allow such updates.

Benefits of Using Views

  1. Data Security: You can hide specific columns or rows from the base table, showing only the necessary information.

  2. Simplification of Queries: Views simplify complex queries by encapsulating them in a single object that can be queried like a table.

  3. Data Integrity: Views can present consistent data even if the underlying table structure changes.

  4. Reusability: Views can be reused in different parts of the database, avoiding the need to rewrite complex queries.

Types of Views

  1. Simple View: Based on a single table and does not contain complex logic (joins, aggregations, etc.).

Example:

CREATE VIEW simple_view AS
SELECT emp_name, salary FROM employees;

  1. Complex View: Based on multiple tables or contains joins, subqueries, or aggregations.

Example:

CREATE VIEW employee_summary AS
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

Limitations of Views

  1. No Physical Data Storage: Since views are virtual, they don't store data physically. As a result, querying a view can sometimes be slower than querying a materialized view because it needs to dynamically retrieve the data from the underlying tables.

  2. Performance: If a view contains complex logic or joins across large datasets, querying it can be slower because each time the view is queried, the underlying SELECT statement must be executed.

  3. Cannot Always Be Updated: Complex views with joins, aggregations, or derived columns cannot be directly updated. Only simple views based on a single table can allow updates, inserts, and deletes.

Difference Between Views and Materialized Views

Conclusion

Views are a powerful feature in SQL for simplifying complex queries, enhancing security by restricting access to sensitive data, and providing a layer of abstraction. They are essential for improving database design, usability, and security but may have performance limitations compared to materialized views in specific scenarios.

...

🔧 Views vs. Materialized Views: What’s the Difference?


📈 26.39 Punkte
🔧 Programmierung

🔧 Materialized Views in SQL | Best Explanation


📈 16.55 Punkte
🔧 Programmierung

🔧 Views in SQL


📈 16.55 Punkte
🔧 Programmierung

🔧 Views in SQL


📈 16.55 Punkte
🔧 Programmierung

🔧 Virtual tables in SQL - Views, Materialized View, Global Temporary Table and Inline View


📈 16.55 Punkte
🔧 Programmierung

🔧 Views and Materialized View in SQL


📈 16.55 Punkte
🔧 Programmierung

🔧 Mastering SQL Views: Simplify and Optimize Your Queries


📈 16.55 Punkte
🔧 Programmierung

🔧 Difference Between Views and Tables in SQL


📈 16.55 Punkte
🔧 Programmierung

🔧 How to avoid N+1 query using SQL views (materialized) in Rails application


📈 16.55 Punkte
🔧 Programmierung

🕵️ CVE-2024-3523 | Campcodes Online Event Management System 1.0 /views/index.php ID sql injection


📈 16.55 Punkte
🕵️ Sicherheitslücken

🔧 Getting Started With Database Views in SQL


📈 16.55 Punkte
🔧 Programmierung

🔧 How To Execute A Query In SQL: Using Queries And Data Views


📈 16.55 Punkte
🔧 Programmierung

🕵️ CVE-2015-10056 | 2071174A vinylmap recordstoreapp/views.py contact sql injection


📈 16.55 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2015-10060 | MNBikeways database Data/views.py id1/id2 sql injection


📈 16.55 Punkte
🕵️ Sicherheitslücken

🔧 Why do SQL Server views need refreshing?


📈 16.55 Punkte
🔧 Programmierung

💾 WordPress PT-Content-Views-Pro 2.1.2 SQL Injection


📈 16.55 Punkte
💾 IT Security Tools

💾 WordPress PT-Content-Views-Pro 2.1.2 SQL Injection


📈 16.55 Punkte
💾 IT Security Tools

🔧 On-Demand Refresh | Materialized Views (Mviews) in ORACLE SQL


📈 16.55 Punkte
🔧 Programmierung

🪟 Microsoft Teams will add support for multiple camera views for Teams Rooms


📈 13.19 Punkte
🪟 Windows Tipps

🔧 Advanced Strategies For Boosting Your Instagram Story Views


📈 13.19 Punkte
🔧 Programmierung

🕵️ CVE-2024-8412 | LinuxOSsk Shakal-NG up to 1.3.3 comments/views.py next redirect (Issue 202)


📈 13.19 Punkte
🕵️ Sicherheitslücken

🕵️ Vuln: Django 'django.views.static.serve()' Function Open Redirection Vulnerability


📈 13.19 Punkte
🕵️ Sicherheitslücken

🐧 This Week in GNOME: #49 New Views


📈 13.19 Punkte
🐧 Linux Tipps

📰 [20201107] - Core - Write ACL violation in multiple core views


📈 13.19 Punkte
📰 IT Security Nachrichten

📰 This is how Turkish websites create inorganic YouTube views


📈 13.19 Punkte
📰 IT Security Nachrichten

📰 2,6 Millionen Views in 20 Minuten: Dieses X-Video schockt das Netz


📈 13.19 Punkte
📰 IT Nachrichten

📰 Bill Barr’s Extreme Views on War Powers Mean Congress’s Window to Stop War with Iran is Now


📈 13.19 Punkte
📰 IT Security Nachrichten

📰 Which Roblox Game Has Most Views? Full Analysis!


📈 13.19 Punkte
Web Tipps

🎥 Live High-Definition Views from the International Space Station (Official NASA Stream)


📈 13.19 Punkte
🎥 Video | Youtube

🔧 How to Implement Deferrable Views in Angular 17 for Faster Load Times


📈 13.19 Punkte
🔧 Programmierung

🔧 Optimizing PostgreSQL Queries with Materialized Views


📈 13.19 Punkte
🔧 Programmierung

🕵️ Open Whisper Signal bis 1.10.0 js/views/message_view.js Cross Site Scripting


📈 13.19 Punkte
🕵️ Sicherheitslücken

matomo