Download presentation
Presentation is loading. Please wait.
Published byAmber Lambert Modified over 8 years ago
1
CSED421 Database Systems Lab View
2
Page 2 Virtual or logical table which is defined as SELECT query with joins Act as table but not real data, only query Simply, predefined query which frequently will be used In select query In ‘From’ clause subquery is not allowed In ‘Where’ clause subquery is allowed Other things are same as real table manipulation What is view?
3
Page 3 What is view?
4
Page 4 Why use view? Select ShipperName from Shipper s where 5< (select count(*) as total from Orders o, Customers c where s.ShipperID=o.ShipperID and o.CustomerID=c.CustomerID and Country=‘Germany’) Order by ShipperName Select * from v where v.total>5 Define this as view v
5
Page 5 Simplify complex queries Reuse or represent same queries(view) as different alias Limit data access to specific users Reduce the network traffic Why use view?
6
Page 6 Creating view Create View view_name (column name) As Select_statement Modifying view Create or Replace VIEW view_name (column name) As Select_statement Dropping view DROP VIEW view_name Show full tables Desc view_name Select * from view_name Syntax of view
7
Page 7 Find the number of order from German Create View german_customer As Select * from customers Where country=‘Germany’ Select sum(orderQuantity) from german_customer Select sum(orderQuantity) from (Select * from customers where country=‘Germany’) as german_customer Example of view
8
Page 8 It is possible to update data in the underlying table through the view One-to-one relation between the rows in view and underlying table SELECT statement must follow several rules Only refer to one database table Not use GROUP BY or HAVING clause Not use DISTINCT NOT contain any expression(aggregates, functions, computed columns ⋯ ) Update v set col1=0 where col1<0 Updatable view
9
Page 9 With Check Option clause can be given for an updatable view to prevent insert or update to rows except those for which the WHERE clause in the select_statement is true Example Create View emp80 As Select employee_id, last_name, department_id From employees Where department_id=80 With Check Option Update emp80 Set department_id=10 Where employee_id=145 → error occurred With Check Option Clause
10
Page 10 http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all 위 사이트에서 코드 실행 후 SQL 문 올려주세요. Practice Info
11
Page 11 1. 각 Order 에 대한 Customer 의 이름을 보여주는 view 를 생성 하시오. View name : orderName_vu Practice
12
Page 12 2. 각 order 의 판매 가격을 보여 주는 view 를 만드시오. viewname : orderPrice_vu 판매 가격 = product price * quantity Practice
13
Page 13 3. Order 의 판매 가격이 가장 높은 customer 의 이름을 찾으시오. Hint: practice 1,2 의 view 를 이용 Practice
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.