CSED421 Database Systems Lab View
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?
Page 3 What is view?
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
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?
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
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
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
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
Page 10 위 사이트에서 코드 실행 후 SQL 문 올려주세요. Practice Info
Page 11 1. 각 Order 에 대한 Customer 의 이름을 보여주는 view 를 생성 하시오. View name : orderName_vu Practice
Page 12 2. 각 order 의 판매 가격을 보여 주는 view 를 만드시오. viewname : orderPrice_vu 판매 가격 = product price * quantity Practice
Page 13 3. Order 의 판매 가격이 가장 높은 customer 의 이름을 찾으시오. Hint: practice 1,2 의 view 를 이용 Practice