Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Online Chapter Example is the school of mankind,
SQL Constraints and Triggers
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query-by-Example (QBE) Chapter 6 Example is the school of mankind, and they will learn at no.
SQL (2).
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
Introduction to Structured Query Language (SQL)
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
1 Views. 2 Views A view is a "virtual table" defined using a query You can use a view as if it were a table, even though it doesn't contain data The view.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Systems More SQL Database Design -- More SQL1.
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.
The Relational Model These slides are based on the slides of your text book.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
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.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
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 Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CMPT 258 Database Systems SQL Queries (Chapter 5).
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
CMPT 258 Database Systems Final Exam Review.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
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.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
COP Introduction to Database Structures
Chapter 2 Views.
SQL: Structured Query Language
Chapter 2 Views.
CSC 453 Database Systems Lecture
CSC 453 Database Systems Lecture
Presentation transcript:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our examples.  If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ?

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke3 View A database view is a virtual table or logical table which is defined as a SQL Select Query. A database view is dynamic because it is not related to the physical schema. When the data of the tables changes, the view reflects that changes as well. Most database management systems, including MySQL, allow you to update data in the underlying tables through the database view with some prerequisites.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke4 Advantage of Views Simplify complex queries and hide the complexity of underlying tables to the end-users and external applications. Limit data access to specific users - expose only non- sensitive data. Provides extra security layer for a database management system - read-only view. Enable computed columns. Enables backward compatibility - create database views with the same schema as the legacy tables.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke5 Disadvantage Performance: querying data from a database view can be slow especially if the view is created based on other views. Tables dependency: you create view based on underlying tables of the a database. Whenever you change the structure of those tables that view associates with, you have to change the view as well.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke6 Create View Statement Within a database, a view and a table cannot have the same name CREATE VIEW [database_name].[view_name] AS [ SELECT statement] CREATE VIEW ReservationPerSailor AS SELECT S.sid, S.sname, COUNT( *) as RCount FROM Reservation R, Sailor S WHERE R.sid = S.sid GROUP by S.sid ORDER BY S.sid DESC ;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke7 Create View with Subquery The SELECT statement can contain a subquery in a WHERE clause, but not in FROM clause. CREATE VIEW aboveAvgSailor AS SELECT S2.sid, S2.sname, S2.rating FROM Sailor S2 WHERE S2.rating > ( SELECT AVG(S1.rating) FROM Sailor S1) ORDER BY S2.rating DESC ;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke8 Query Against View You can query a view as if it is a table. You can create a view based on other views. Queries against the views are executed in two ways: A temporary table is created based on the view definition statement and executes the incoming query on this temporary table. The incoming query with the query defined the view are combined into one query and executes the combined query.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke9 Show View Definition Create view AboveAvgStudents as Select S2.sid, concat (S2.fname,' ', S2.lname) as StudentName, S2.points/S2.credits as GPA From Students S2 Where S2.points/S2.credits > (select AVG(S1.points/S1.credits) from Students S1) order by S2.sid; Show create view AboveAvgStudents;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke10 Modify an Existing View ALTER VIEW [database_name]. [view_name] AS [ SELECT statement] Alter view AboveAvgStudents as Select S2.sid, concat (S2.fname,' ', S2.lname) as StudentName, TimeStampDiff(YEAR, S2.DOB, CurDate()) as Age, S2.points/S2.credits as GPA From Students S2 Where S2.points/S2.credits > (select AVG(S1.points/S1.credits) from Students S1) order by S2.sid;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke11 Modified View

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke12 Create or Replace View If a view already exists, MySQL simply modifies the view. If the view does not exist, MySQL create a new view. CREATE OR REPLACE VIEW [database_name]. [view_name] AS [ SELECT statement]

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke13 Drop a View IF EXISTS clause allows you to check whether the view exists or not. DROP VIEW [ IF EXISTS ] [database_name].[view_name]

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke14 Create an Updatable View Views are not only query-able but also updatable. Using INSERT or UPDATE statement to insert or update rows of the base table through the updatable view. Using DELETE statement to remove rows of the underlying table through the view. To be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke15 Create view Sub_Students as Select fname as FirstName, lname as LastName, credits as Credits, points as Points From Students;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke16 Update Sub_Students SET Credits = Credits + 1 WHERE Points > 340;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke17 Insertable View An updatable view is insertable if it also satisfies additional requirements No duplicate view column names; The view must contain all columns in the underlying table that do not have a default value. The view columns must be simple column references. They must not be expressions or composite expression.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke18 INSERT INTO Sub_Students values ('Lisa', 'Li', 100, 300);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke19 Non-Updatable View A view is not updatable if it contains any of the following Aggregate functions such as MIN, MAX, SUM, AVG, COUNT, etc. DISTINCT, GROUP BY, HAVING clause. UNION or UNION ALL clause. Subquery in the SELECT clause or in the WHERE clause that refers to the table appeared in the FROM clause. Reference to non-updatable view in the FROM clause. Reference only to literal values. Multiple references to any column of the base table.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke20 Update Mutli-table View Only one single underlying table can be updated in a multi-table view definition. SET Clause must name only columns from one of the tables in the view. Create View StudentsEnrollment As Select S1.fname as FirstName, S1.lname as LastName, S2.course_title as CourseTitle, E.grade as Grade From Students S1, Sessions S2, Enrolled E Where S1.sid = E.sid AND S2.call_num = E.call_num;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke21 Update StudentsEnrollment SET LastName = concat(LastName,’A’) Where Grade = ‘A’;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke22

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke23 Removing Rows Through View DELETE from Sub_Students WHERE Credits = 104;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke24

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke25 WITH CHECK OPTION When updatable view shows only partial data of the underlying table, it is possible to update data which is not visible through the view. To ensure data consistency of the view, WITH CHECK OPTION clause is used when you create the view. CREATE OR REPLACE VIEW view_name AS select_statement WITH CHECK OPTION ;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke26 CREATE OR REPLACE VIEW vps AS SELECT employeeNumber, lastname, firstname, jobtitle, extension, , officeCode, reportsTo FROM employees WHERE jobTitle LIKE '%VP%';

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke27 Select employeeNumber, lastname, firstname, jobtitle From vps; INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension, ,officeCode,reports To) values (1703,'Lily','Bush','IT

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke28 SELECT * FROM employees ORDER BY employeeNumber DESC ; This is not what we want to do through the view of “vps’

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke29 CREATE OR REPLACE VIEW vps AS SELECT employeeNumber, lastname, firstname, jobtitle, extension, , officeCode, reportsTo FROM employees WHERE jobTitle LIKE '%VP%' WITH CHECK OPTION ;

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke30 INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension, ,officeCode,reports To) values (1703,'Lily','Bush','IT 1Error Code: CHECK OPTION failed 'classicmodels.vps' INSERT INTO vps(employeeNumber,firstname,lastname,jobtitle,extension, ,officeCode,reports To) VALUES (1704,'John','Smith','SVP It works as expected.