Views and Virtual Tables

Similar documents
CSC 453 Database Technologies. Tanu Malik DePaul University

Relational Database Languages

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Database Management Systems. Chapter 5

Database Management Systems. Chapter 5

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

CSC 261/461 Database Systems Lecture 6. Fall 2017

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL: Structured Query Language Nested Queries

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Principles of Data Management

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Chapter 4: Intermediate SQL

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

Topics. CSCI 403 Database Management DISTINCT. JOIN Clause 2/4/2019 DISTINCT JOINS. 12 Miscellaneous Topics

Database Management Systems. Chapter 3 Part 1

CMPT 354: Database System I. Lecture 2. Relational Model

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Chapter 4: Intermediate SQL

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

The Relational Model 2. Week 3

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

EECS 647: Introduction to Database Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

The Relational Model. Chapter 3

Relational Algebra and SQL

More SQL: Complex Queries, Triggers, Views, and Schema Modification

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

SQL. Chapter 5 FROM WHERE

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Data Model. Data Models. Relational vs Semistructured. Structure Operations - Constraints

Query Processing and Query Optimization. Prof Monika Shah

CSEN 501 CSEN501 - Databases I

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

Database Applications (15-415)

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Introduction to Database Systems CSE 344

CS352 Lecture: Integrity and Security Constraints revised 9/8/06

A hypothetical M:M student schedule example

Introduction to Database Systems CSE 344

Querying Data with Transact SQL

Outer Join, More on SQL Constraints

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Homework 1: RA, SQL and B+-Trees (due Feb 7, 2017, 9:30am, in class hard-copy please)

SQL. SQL Queries. CISC437/637, Lecture #8 Ben Cartere9e. Basic form of a SQL query: 3/17/10

Triggers- View-Sequence

Introduction to Database Systems CSE 344

Database Systems CSE 414

The Relational Data Model. Data Model

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

Lecture 17: Classes (Chapter 15)

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

20761B: QUERYING DATA WITH TRANSACT-SQL

CMPT 354: Database System I. Lecture 3. SQL Basics

COMP 430 Intro. to Database Systems

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Database Systems ( 資料庫系統 )

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Querying Data with Transact-SQL

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #4: SQL---Part 2

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Querying Data with Transact-SQL

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Systems Relational Model. A.R. Hurson 323 CS Building

King Fahd University of Petroleum and Minerals

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

The Relational Model

Creating Other Schema Objects

COMP 244 DATABASE CONCEPTS & APPLICATIONS

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Database Systems CSE 414

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL Aggregation and Grouping. Outline

SQL Aggregation and Grouping. Davood Rafiei

20761 Querying Data with Transact SQL

CS145 Final Examination

CMPT 354: Database System I. Lecture 4. SQL Advanced

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Sql Server Syllabus. Overview

SQL. Dean Williamson, Ph.D. Assistant Vice President Institutional Research, Effectiveness, Analysis & Accreditation Prairie View A&M University

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Advanced SQL Tribal Data Workshop Joe Nowinski

The Relational Model

SQL Interview Questions

Implementing a Logical-to-Physical Mapping, Part II

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Course Outline. Querying Data with Transact-SQL Course 20761B: 5 days Instructor Led

Oracle 9i Application Development and Tuning

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

Querying Data with Transact-SQL

Relational model and basic SQL

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Transcription:

Views and Virtual Tables

VIEWS

Views CREATE OR REPLACE VIEW CSstudents AS SELECT * FROM student WHERE Program = 'COMP-SCI'; SELECT * FROM CSstudents; base tables (CREATE TABLE) stored in database views (CREATE VIEW) dependent on base tables or other views, may or may not be stored (virtual vs materialized) temporary tables (subquery, etc.) limited lifetime

Point of Views CREATE VIEW studentview AS SELECT LastName, FirstName, SID, Career, Program FROM student; Hide information (grant access to relevant info) SELECT name FROM studentgroup WHERE name NOT IN (SELECT groupname FROM CSstudents, memberof WHERE StudentID = SID); Simplify queries (improve readability) -not necessarily a good reason to create a view in general, if temporary table is sufficient

Point of Views CREATE VIEW enrollment(sid, LName, CID, CNR, Dpt) AS SELECT SID, LastName, CID, CourseNr, Department FROM student, enrolled, course WHERE SID = studentid AND CourseID = CID; SELECT count(*) FROM enrollment WHERE CNR = 440 AND Dpt = 'CSC'; speed up querying

Modifying Views DROP VIEW Csstudents; What about other objects that depend on it (e.g other views)? How is/are the underlying base table(s) affected? INSERT INTO CSstudents(LastName, FirstName, SID) VALUES ('Crackenden', 'Gloria', 123); What do INSERT, DELETE, UPDATE mean for a view? Examples: CSstudents, Enrollment

Updatable Views An updatable view is one you can use to insert, update, or delete base table rows. http://download.oracle.com/docs/cd/b28359_01/server.111/b28286/statements_8004.htm Roughly: FROM contains only a single relation no DISTINCT, aggregation, set, calculated value WHERE clause may not contain a sub-query involving the relation the view is based on Statement can still fail (e.g. if primary key is missing in INSERT)

Or, you use Triggers CREATE VIEW enrollment(sid, LName, CID, CNR, Dpt) AS SELECT SID, LastName, CID, CourseNr, Department FROM student, enrolled, course WHERE SID = studentid AND CourseID = CID; CREATE TRIGGER enrollmentinsert INSTEAD OF INSERT ON enrollment FOR EACH ROW BEGIN INSERT INTO enrolled(studentid, CourseID) VALUES (:new.sid, :new.cid); END; Trigger can fail for f.k violations: good

Updatable Views: Examples Create a trigger that implements INSERTs into studentview Create a trigger that implements INSERTs into Csstudents Create a trigger that implements DELETEs on enrollment Create triggers that implement UPDATEs on enrollment

WITH CHECK OPTION CREATE OR REPLACE VIEW CSstudents AS SELECT * FROM student WHERE Program = 'COMP-SCI' WITH CHECK OPTION; SELECT * FROM CSstudents; what happens if we try inserting non-cs student?

CHECK OPTION for Assertions CREATE OR REPLACE VIEW v_memberof AS SELECT StudentID, GroupID, Joined FROM memberof WHERE joined >= (SELECT started FROM student WHERE SID = StudentID) WITH CHECK OPTION; if we use v_memberof in place of memberof what does this enforce? downside: nesting views deeply is bad, so not always good replacement for base tables

CHECK OPTION Examples ensure that undergraduate students do not enroll in graduate courses ensure that graduate students do not enroll in more than 3 courses a quarter limit the number of courses to at most 100 limit the number of students each year to at most 50

VIRTUAL TABLES

Temporary Tables create global temporary table gradstudent( LASTNAME VARCHAR2(40), SID NUMBER(5,0), PROGRAM VARCHAR2(10), primary key(sid) ) on commit delete rows; insert into gradstudent select lastname, sid, program from student where career = 'GRD'; or on commit preserve rows lifetime of temporary data is limited to session table exists beyond session

Common Table Expressions (CTE) temporary table, exists only for lifetime of query, cannot be used in other queries can create multiple such tables WITH GradStudents AS (SELECT SID, LastName, SSN FROM student WHERE Career = 'GRD') SELECT * FROM enrolled WHERE StudentID NOT IN (SELECT SID FROM GradStudents);

CTE Example WITH StudentEnrollment(SID, Quarter, Year, crs_nbr) AS (SELECT StudentID, Quarter, Year, count(courseid) FROM enrolled GROUP BY StudentID, Quarter, Year), StudentMax(SID, maxcrs) AS (SELECT SID, max(crs_nbr) FROM StudentEnrollment GROUP BY SID) SELECT * FROM student S, StudentMax SM WHERE S.SID = SM.SID; temporary table can refer to previous temporary table mutual recursion not allowed (in Oracle)

CTE Examples List departments in which the average enrollment in courses is below 2 For each program compute the number of Chicago students in the program but only include programs that have at least three students.