0% found this document useful (0 votes)
12 views

Views in SQL Server

A view in SQL Server is a compiled SQL query acting as a virtual table. It does not store data physically by default but retrieves data from underlying database tables.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views

Views in SQL Server

A view in SQL Server is a compiled SQL query acting as a virtual table. It does not store data physically by default but retrieves data from underlying database tables.

Uploaded by

Md Asif Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
6 Summary of Views in SQL Server with Examples What is a View? Aview in SQL Server is a compiled SQL query acting as a virtual table. It does not store data physically by default but retrieves data from underlying database tables. Differences Between a Table and a View: ‘+ Tables are physical objects that store data, while views are logical or virtual representations. ‘+ Tables are independent objects, whereas views depend on the underlying tables. ‘+ Changes to a table reflect in dependent views and vice versa. Examples to Understand Views: Department Table: ‘CREATE TABLE Department ( ID INT PRIMARY KEY, Name VARCHAR(50) ) co INSERT INTO Department VALUES(1, ‘IT'), (2, ‘HR'), (3, ‘Sales’ Employee Table: CREATE TABLE Employee ( ID INT PRIMARY KEY, Name VARCHAR(SO), Gender VARCHAR(56), DOB DATETIME, , eetrD Ta 60 INSERT INTO Employee VALUES (1, ‘Pranaya’, ‘Male’ , ‘1996-02-29 10:53:27,060", 1), (2) ‘Priyanka’, ‘Fenaie', '1995-05-25 10:53:27,060", 2), (3) ‘Anurag’, ‘Mate', "1995-64-19 1:53:27.060', 2), (4) ‘Preety', *Fenalé', '1996-03-17 10:53:27.868", 3), (5, 'Sambit'; ‘Mate’, '1997-01-15 16:53:27.068", i), (6, ‘Hina, ‘Fenate’; °1995-67-12 16:53:27,068", 2); Types of Views: 35 1, Simple Views (Updatable Views): © Created from a single table. © Allow all DML operations. Examples: CREATE VIEW vwALLEnployeesi AS SELECT * FROM Employee; CREATE VIEW vwALLEnployees2 AS SELECT ID, Name, Gender, DOB, DeptID FROM Employee; 2, Complex Views: © Created from multiple tables or with complex queries (e.g., using joins, aggregates). ‘© May not allow DML operations or update base tables correctly. Examples: CREATE VIEW VwALLEnpLoyees3 AS SELECT emp.1D, enp.Name, emp-Gender, emp.00B, dep.Nane as DepartmentNane FROM Employee” emp INNER JOIN Department dep ON emp.DeptID = dep.ID; CREATE VIEW vwALLEnployees4 AS SELECT Gender, COUNT(*) as TotalEnployee FROM Employee’ GROUP BY Gender; DML Operations on Simple Views: * Select: SELECT * FROM wALLEmpLoyeest; * Insert: INSERT INTO vwALLEnployees} (ID, Name, Gender, DOB, DeptID) VALUES (7, ‘Rohit’, Male’, ‘1995-64-19 10:53:27.060", 3); * Update: UPDATE wwALLEmployees1 SET Nane = 'Rohit Kunar’, DOB = '1996-02-29 1053:27. ans beptIo = 1 WHERE ID = 7; * Delete: DELETE FROM vwALLEnployees} WHERE ID = 7; Additional Points: ‘+ Dropping Tables with Dependent Views: You can drop tables with dependent views; the views become inactive but remain in the database. ‘+ Creating Views Based on Other Views: It is possible to create views based on other views. ‘+ Updating Views: Views can be updated, but updates on views based on multiple tables might not update base tables correctly, Use INSTEAD OF triggers for proper updates. Is this conversation helpful so far? (ChatGPT can make mistakes, Check important info 515

You might also like