CS240A: Databases and Knowledge Bases TSQL2 Carlo Zaniolo Department of Computer Science University of California, Los Angeles Notes From Chapter 6 of.

Slides:



Advertisements
Similar presentations
TSQL 2 : QUERY LANGUAGE FOR TEMPORAL DATA CS 224 : Advanced Topics in Data Management.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 Constraints, Triggers and Active Databases Chapter 9.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Advanced Databases Temporal Databases Dr Theodoros Manavis
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
CS240A: Databases and Knowledge Bases Time Ontology and Representations Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Modification Statements in TSQL2 INSERT INTO Prescription %Executed on VALUES ('Melanie','Dr. Beren','Proventil','30 mg', INTERVAL '8:00' MINUTE)
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS240A: Databases and Knowledge Bases A Taxonomy of Temporal DBs Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS240A: Databases and Knowledge Bases Applications of Active Database Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
CS240A: Databases and Knowledge Bases Time Ontology and Representations Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
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.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
CS240A: Databases and Knowledge Bases Recursive Queries in SQL 1999 Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Fushen Wang, XinZhou, Carlo Zaniolo Using XML to Build Efficient Transaction- Time Temporal Database Systems on Relational Databases In Time Center, 2005.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu
資工所 在職碩一 P 莊浚銘 Temporal Database Paper Reading Report.
CS240A: Databases and Knowledge Bases Time Ontology and Representations Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Data Models and Query Languages of Spatio-Temporal Information Cindy Xinmin Chen Computer Science Department UCLA February 28, 2001.
CS240A: Databases and Knowledge Bases Temporal Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Temporal Data Modeling
Where does time go ?. Applications abound Temporal database systems provide built-in support for recording and querying time-varying information Application.
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.
1 The T4SQL Temporal Query Language Presented by 黃泰豐 2007/12/26.
CPT-S Advanced Databases 11 Yinghui Wu EME 49.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS580 Advanced Database Topics
COP Introduction to Database Structures
Chapter 3 Introduction to SQL(3)
CS580 Advanced Database Topics
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database systems Lecture 3 – SQL + CRUD
Structured Query Language – The Fundamentals
Contents Preface I Introduction Lesson Objectives I-2
CS240A: Databases and Knowledge Bases TSQL2
CS240A: Databases and Knowledge Bases A Taxonomy of Temporal DBs
Presentation transcript:

CS240A: Databases and Knowledge Bases TSQL2 Carlo Zaniolo Department of Computer Science University of California, Los Angeles Notes From Chapter 6 of Advanced Database Systems by Zaniolo, Ceri, Faloutsos, Snodgrass, Subrahmanian and Zicari. Morgan Kaufmann, 1997

TSQL2 Language Constructs  Schema Specification  Snapshot Queries  The FROM Clause---Restructuring  Valid­Time Selection  Valid­Time Projection  Modification Statements  Event Tables  Transaction Time  Aggregates  The NOW construct Many other constructs: Temporal Indeterminacy, Granularity,Schema Versioning, etc.

Schema Specification  Scenario: Patient records include information on the drugs prescribed to each patient.  The valid time specifies the period(s) during which the drug was prescribed.  The valid time has a granularity of day (transaction time granularity is system defined): CREATE TABLE Prescription (Name, Physician, Drug, Dosage, Frequency INTERVAL MINUTE) AS VALID DAY AND TRANSACTION

Six Different Kinds of Tables  Snapshot table: nothing after the attributes  Valid­time state table: AS VALID [ STATE ]  Valid­time event table: AS VALID EVENT  Transaction­time table: AS TRANSACTION  Bitemporal state table: AS VALID [ STATE ] AND TRANSACTION  Bitemporal event table: AS VALID EVENT AND TRANSACTION  The type of a table can be changed at any time, using the ALTER statement.

Snapshot Queries  Who has been prescribed drugs? SELECT SNAPSOT Name FROM Prescription  Result is a list of names of those with current or past prescriptions.  Who is or was taking the drug Proventi l ? SELECT SNAPSHOT Name FROM Prescription WHERE Drug = 'Proventil‘  Result is a list of names.

Valid Time History Queries  Can request history, rather than just current state.  Who has been prescribed drugs, and when? SELECT Name FROM Prescription  Result is a list of names, each associated with one or more maximal periods.  Since we have projection: coalescing is used to compute those maximal periods.

Temporal Joins  What drugs have been prescribed with Proventil? SELECT P1.Name, P2.Drug FROM Prescription AS P1, Prescription AS P2 WHERE P1.Drug = 'Proventil‘ AND P2.Drug <> `Proventil‘ AND P1.Name = P2.Name  Result is a list of patient names and drugs, along with their associated maximal period(s).

Temporal Projection  Who has been on a drug for more than a total of six months? SELECT Name, Drug FROM Prescription(Name, Drug) AS P WHERE CAST(VALID(P) AS INTERVAL MONTH) > INTERVAL '6' MONTH  Result will contain the maximal interval(s) when the patient has been on the drug---provided that the sum of all those intervals exceeds 6 months.

Restructuring  Nested SELECT projects on the specified attributes, then automatically coalesces the result. FROM R(B, C) AS MyR is equivalent to FROM (SELECT B, C FROM R) AS MyR Other attributes in R are not accessible via MyR, and A is not accessible in the enclosing SELECT.

Restructuring: coupling of correlation names  Who has been on Proventil throughout their drug regime? SELECT SNAPSHOT P1.Name FROM Prescription(Name) AS P1, P1(Drug) AS P2 WHERE P2.Drug = 'Proventil’ AND VALID(P2) = VALID(P1)  P1 contains all the times that any drug has been prescribed to a patient.  P2 is coalesced on the Name and Drug columns. Also, the values of the Name column for both P1 and P2 are identical.

Restructuring: very useful syntactic sugar SELECT... FROM A(B,C,D) AS A2, A2(E,F) AS A3 WHERE...  is equivalent to:SELECT... FROM (SELECT B,C,D FROM A) AS A2, (SELECT B,C,D,E,F FROM A) AS A3 WHERE... AND A2.B = A3.B AND A2.C=A3.C AND A2.D = A3.D AND VALID(A2) OVERLAPS VALID(A3)  Intuitively, A2 is timestamped with a temporal element when attributes B, C, and D remained constant, and A3 ranges over different values of D and E, with the timestamp of A3 being a subset of that of A2.

Partitioning  Who has been on the same drug for more than six consecutive months? SELECT Name, Drug FROM Prescription(Name, Drug) AS (Period) P WHERE CAST(VALID(P) AS INTERVAL MONTH) > INTERVAL '6' MONTH  P ranges over Name­Drug pairs associated with individual maximal periods.  The result may contain several rows with the same Name and Drug values.

Partitioning, cont.  Partitioning is more than syntactic sugar.  It violates the data model, in that it produces value­ equivalent tuples that the correlation name ranges over.  Note, however, that underlying tables and the result table are always coalesced so that the violation is temporary and internal to a query.  Useful for queries specifying “consecutive time” or “continuous periods”.

Valid­Time Projection  What drugs was Melanie prescribed during 1994? SELECT Drug VALID INTERSECT(VALID(Prescription), PERIOD '[1994]' DAY) FROM Prescription WHERE Name = 'Melanie‘  The result is a list of drugs, each associated with a set of the periods during 1994 that they were prescribed to Melanie.  A new clause, the VALID clause, specifies the timestamp of the resulting tuple.

Modification Statements  The valid times can be specified for rows that are being inserted.  Inserted values will be coalesced with existing value­ equivalent rows.  Default valid clause: INSERT INTO Prescription VALUES ('Sally','Dr. Beren','Proventil','100 mg', INTERVAL '8:00' MINUTE) VALID PERIOD '[1993/01/01 ­ 1993/06/30]‘ VALID PERIOD(CURRENT_TIMESTAMP, NOBIND(TIMESTAMP 'now'))  An INSERT statement with a subquery is handled in the same manner.

Modification Statements, cont.  The delete statement removes period(s) from the temporal element of the qualifying row(s).  The columns of a row can be changed, as in SQL­92. DELETE FROM Prescription WHERE Name = 'Melanie‘ VALID PERIOD '[1993­03­01 ­ 1993­05­31]‘ VALID PERIOD '[1994­03­01 ­ 1994­04­30]‘  If the temporal element becomes empty, the row itself is deleted (no valid time specified means ‘from now’) UPDATE Prescription SET Dosage TO '50 mg‘ WHERE Name = 'Melanie‘ AND Drug = 'Proventil'

Modification statements, cont.  The valid time of a row can also be changed: UPDATE Prescription SET Dosage TO '50 mg‘ VALID PERIOD '[1993­03­01 ­ 1993­07­30]'WHERE Name = 'Melanie‘ AND Drug = 'Proventil'

Modification Statements (Con’t) now Original DELETE yields UPDATE yields UPDATE with valid time yields

Event Tables  Event tables are timestamped with instant sets.  Each row identifies a particular kind of (instantaneous) event, with the timestamp of that row specifying the instant(s) when that event occurred.  Event tables may also be associated with transaction time: CREATE TABLE LabTest (Name, Physician, TestID) AS VALID EVENT HOUR AND TRANSACTION

Event Tables, cont.  Were any patients the sole receivers of tests ordered by a physician? SELECT L1.Name, L2.Physician FROM LabTest(Name) AS L1, L1(Physician) AS L2, LabTest(Physician) AS L3 WHERE VALID(L1) = VALID(L2) AND L2.Physician = L3.Physician AND VALID(L1) = VALID(L3)  VALID(L1) is an event set containing all tests done to a particular patient  VALID(L2) contains the times of all tests done to a particular patient and ordered by a particular physician  VALID(L3) is an event set containing all tests ordered by a particular physician.

Transaction Time  If transaction time is not mentioned in the query, the results include only the information currently believed to be true. E.g.: What is Melanie's prescription history? SELECT Drug FROM Prescription WHERE Name = 'Melanie'  Can also rollback the database. E.g.: What did the physician believe on June 1, 1994 was Melanie's prescription history? SELECT Drug FROM Prescription AS P WHERE Name = 'Melanie‘ AND TRANSACTION(P) OVERLAPS DATE '1994­06­01‘

Transaction Time, cont.  Auditing can be done on previously corrected data.  When was Melanie's data valid on January 1, 1993 last corrected? SELECT SNAPSHOT BEGIN(TRANSACTION(P2)) FROM Prescription AS P P2 WHERE P1.Name = 'Melanie' AND P1.Name = P2.Name AND VALID(P1) OVERLAPS DATE '1993­01­01‘ AND VALID(P2) OVERLAPS DATE '1993­01­01‘ AND TRANSACTION(P1) MEETS TRANSACTION(P2)

Transaction Time, cont.  The transaction timestamp is specified with TRANSACTION(name), and evaluates to a period.  What was recorded as current on March 1 about Melanie?  The default is SELECT SNAPSHOT Drug FROM Prescription AS P WHERE Name = 'Melanie‘ AND TRANSACTION(P) OVERLAPS DATE '1995­03­01‘ AND VALID(P) OVERLAPS DATE '1995­03­01‘ AND TRANSACTION(...) OVERLAPS CURRENT_TIMESTAMP

Aggregates  SQL­92 supports the aggregates MIN, MAX, COUNT, SUM, and AVG, plus a new temporal aggregate called RISING.  Snapshot reducibility implies that these aggregates return time­varying results when applied to temporal tables.  The aggregate is applied to each snapshot in turn.  Evaluation can be optimized, as many snapshots will contain the same rows, and hence evaluate to the same result.

Aggregates, cont.  How many drugs is/was Melanie taking? SELECT COUNT(*) FROM Prescription WHERE Name = 'Melanie‘ AND Drug = 'Proventil’  How many people are taking each drug? SELECT Drug, COUNT(*) FROM Prescription GROUP BY Drug.  Again, the result is a temporal table.

Discussion  A critique of TSQL2