VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
View (virtual table). View A VIEW is a virtual table A view contains rows and columns, just like a real table. The fields in a view are fields from one.
Database Languages Chapter 7. The Relational Algebra.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
Database Systems: Design, Implementation, and Management Tenth Edition
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Introduction to Structured Query Language (SQL)
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 5 Introduction to SQL.
Chapter 6: Integrity (and Security)
COP Introduction to Database Structures
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database Systems: Design, Implementation, and Management Tenth Edition
SQL Views CS542.
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
The Basics of Data Manipulation
SQL Views and Updates cs3431.
Chapter 2 Views.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
IST 318 Database Administration
Presentation transcript:

VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007

2 AGENDA: What are Views? Updating View Naming Columns Grouped Views Views and Joins View and Subqueries Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : Using Multiple Queries in Views What Views Can Not Do Dropping Views Updatable Views Read Only Views Checking the Values in Views

3 What are Views? Views are tables whose contents are taken or derived from other tables. Also knows as virtual tables, or viewed tables. Using the CREATE VIEW Statement: – CREATE VIEW viewname AS query;

4 What are Views? (cont’d) – CREATE VIEW Londonstaff AS SELECT snum, sname, city, comm FROM Salespeople WHERE city = ‘London’; – CREATE VIEW Salesown AS SELECT snum, sname, city FROM Salespeople;

5 Updating View We can modify the content of a view by using data manipulation statement, but the modifications do not affect the view itself. UPDATE Salesown SET city = ‘Palo Alto’ WHERE snum = 1004; Remember that some views can not be updated (will be discussed later).

6 Naming Columns So far, we have taken names of columns of our views directly from the names of the columns in the underlying tables. However, sometimes we need to provide new names for our columns in the views. On condition such as: – When some of the columns in the SELECT clause of the underlying query are expressions or derived values and therefore unnamed. – When the query in the view accesses multiple underlying tables, and two or more columns in those tables have the same name. To support this, we can use AS argument.

7 Naming Columns (cont’d) Example: CREATE VIEW Londonstaff AS SELECT snum AS Salesperson_Number, sname AS Last_name, city, comm AS Commission FROM Salespeople WHERE city = ‘London’;

8 Grouped Views Grouped Views are views that contain a GROUP BY clause or that are based on other grouped views. Example: CREATE VIEW Totalforday(Date, Custcount, Salescount, Ordercount, Averageamt, Totalorders) AS SELECT odate, COUNT(DISTNCT cnum), COUNT(DISTINCT snum), COUNT(onum), AVG(amt), SUM(amt) FROM Orders GROUP BY odate; We can access it by: SELECT * FROM Totalforday;

9 Views and Joins Views can be drawn from more than one tables. Example: CREATE VIEW Nameorders AS SELECT onum, amt, a.snum, sname, cname FROM Orders a, Customers b, Salespeople c WHERE a.cnum = b.cnum AND a.snum = c.snum; We can access it by: SELECT * FROM Nameorders WHERE snum = 1007;

10 View and Subqueries Views can also use subqueries, including correlated subqueries. Suppose we would like to track the bonus of the salesperson who has the customer with the highest order on any given date : – Example: CREATE VIEW Elitesalesforce AS SELECT b.odate, a.snum, a.sname FROM Salespeople a, Orders b WHERE a.snum = b.snum AND b.amt = (SELECT MAX(amt) FROM Orders C WHERE c.odate = b.odate);

11 View and Subqueries (cont’d) Suppose we would like to track the bonus of the salesperson who has the customer with the highest order at least ten times on any given date : – Example: CREATE VIEW Bonus AS SELECT DISTINCT snum, sname FROM Elitesalesforce a WHERE 10 <= (SELECT COUNT(*) FROM Elitesalesforce b WHERE a.snum = b.snum;

12 Using Multiple Queries in Views Views can be based on multiple queries combined with the UNION, EXCEPT, and INTERSECT operators. Meaning that you can treat material from several tables as though it were from one table. Example: CREATE VIEW Londonpeople AS SELECT snum, sname, ‘salesperson’ FROM Salespeople WHERE city = ‘London’ UNION SELECT cnum, cname, ‘ customer’ FROM Customers WHERE city = ‘London’;

13 What Views Can Not Do So far, types of views are read only. Meaning that you can query them, but you can not subject them to update statements (further details will be discussed later). We can not use ORDER BY in the definition of a view. It is unordered list output. However, queries on views may use ORDER BY. Do differentiate queries within a view and queries on the view.

14 Dropping Views The syntax to eliminate a view from the database is similar to that for removing tables. Syntax: DROP VIEW viewname; Example: DROP VIEW Bonus;

15 Changing Values Through Views Updating Views What happen when you update a view? The simple answer is that the changes are transferred to the base tables that underlie the view. Determining Whether a View is Updatable If you can perform update statements on a view, it is called updatable, otherwise it is read-only. Updating a view means that by executing any of the three DML statements (INSERT, UPDATE, and DELETE).

16 Updatable Views The criteria that determine whether a view is updatable are based on the content of the underlying query, which must satisfies rules as below: – It must be drawn on one and only one underlying table. – It must have no aggregate functions – It must not specify DISTINCT – It must not use GROUP BY or HAVING – It may be defined on another view, but that view must also be updatable – It must not use constants, strings, or value expressions among the selected output columns – For INSERT, it must include any columns of the underlying table that have the NOT NULL constraint, unless another default value has been specified. – Not all SQL version support subqueries as a standard. – It should include primary key of the underlying table.

17 Read Only Views Read-only views allow you to derive and reformat data extensively. They give you a library pf complex queries that you can execute and re-execute. Keeping your derived information strictly up to the minute. It also have the security appliactions.

18 Determining Which Views are Updatable CREATE VIEW Dateorders(odate, ocount) AS SELECT odate, COUNT(*) FROM Orders GROUP BY odate; – Is it updatable ? Why ? CREATE VIEW Londoncust AS SELECT * FROM Customers WHERE city = ‘London’ – Is it updatable ? Why ?

19 Checking the Values in Views You can ensure against modifications of the views by using WITH CHECK OPTION in the definition of the view. For example: CREATE VIEW Highratings AS SELECT cnum, rating FROM Customers WHERE rating = 300; And then we try to insert a data: INSERT INTO Highratings VALUES (2018, 200) – The values will be inserted and successed, but then disappear because the rating is below 300.

20 Checking the Values in Views (cont’d) Try to prevent that, by using WITH CHECK OPTION CREATE VIEW Highratings AS SELECT cnum, rating FROM Customers WHERE rating = 300 WITH CHECK OPTION; – The query to insert the new data will rejected and failed to insert.

21 End of V I E W S Thank you