Views, Triggers and Recursive Queries

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt.
PL/SQL.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Database Systems SQL Queries, PL/SQL, Triggers Gergely Czuczy Cause Light Wounds: I call upon chaos to cause unbalanced parentheses.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Winter 2002Arthur Keller – CS 18015–1 Schedule Today: Feb. 28 (TH) u Datalog and SQL Recursion, ODL. u Read Sections , Project Part 6.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
Click Here to Begin the Game CHOICE 1CHOICE 2CHOICE 3 CHOICE CHOICE
Database Systems Lecture 5 Natasha Alechina
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.
1 MT258 Computer Programming and Problem Solving Unit 7.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
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.
Programming in postgreSQL with PL/pgSQL ProceduralLanguageextension topostgreSQL.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
Advanced SQL: Cursors & Stored Procedures
Example Consider the following class specification for a class that stores a bunch of characters. /* class invariant *this bunch contains one or more char.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Stored Procedure used in PosgreSQL.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Distributed Database Systems INF413. Joins MySQL 5 Certification Study Guide 2 Writing Inner Joins with the Comma Operator mysql> SELECT Name, Language.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL.
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
Security and User Authorization in SQL. Lu Chaojun, SJTU 2 Security Two aspects: –Users only see the data they’re supposed to; –Guard against malicious.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
Assignment 6 - Solution Problem 1 Suppose a transaction sets an intention-write lock on a file and later sets a write lock on a record of the file. Is.
Programming in postgreSQL with PL/pgSQL ProceduralLanguageextension topostgreSQL 1.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Database Refactoring By: Chris Hoover. 2 Agenda 1. Introduction 2. The Problem 3. The Solution 4. Demo 5. Q&A.
Programming in postgreSQL with PL/pgSQL
Programming in postgreSQL with PL/pgSQL
Trigger used in PosgreSQL
Views, Triggers and Recursive Queries
PL/pgSQL
PostgreSQL Conference East 2009
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Recursion in SQL Basic recursive WITH statement.
CS122B: Projects in Databases and Web Applications Spring 2017
CS122B: Projects in Databases and Web Applications Winter 2017
PL/SQL Programing : Triggers
Stored Procedure used in PosgreSQL
CS4222 Principles of Database System
Instructor: Mohamed Eltabakh
Database systems Lecture 3 – SQL + CRUD
CS122B: Projects in Databases and Web Applications Winter 2018
Chapter 7 Using SQL in Applications

Chapter 7 Using SQL in Applications
CS122B: Projects in Databases and Web Applications Spring 2018
Recursion in SQL See notes for the question SELECT WHERE
DUAL TABLE The DUAL table has one row called "X" and one column called "DUMMY." The DUAL table is used to create SELECT statements and execute commands.
1,000, , , Year Old Topic 1 10 Year Old Topic 2 175, ,000 9 Year Old Topic 3 9 Year Old Topic 4 50,000 8 Year Old Topic 5 8 Year.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Views, Triggers and Recursive Queries

Views and Recursive Queries Assume we have a relation Par(child, parent). Write a view that contains all pairs of siblings. Write a view that contains all pairs of direct cousins. Write a query that computes all (direct or indirect) cousins.

Solution (Part 1) CREATE VIEW Siblings AS SELECT P1.child as sib1, P2.child as sib2 FROM Par P1, Par P2 WHERE P1.parent = P2.parent and P1.child<>P2.child

Solution (Part 2) CREATE VIEW DCousins AS SELECT P1.child as cous1, P2.child as cous2 FROM Par P1, Par P2, Siblings S WHERE P1.parent = sib1 and P2.parent = sib2

Solution (Part 3) WITH Recursive Cousins(C1, C2) AS (SELECT * FROM DCousins) UNION (SELECT P1.child, P2.child FROM Par P1, Par P2, Cousins C WHERE P1.parent = c1 and P2.parent = c2) SELECT * FROM Cousins

Views create table a(col1 integer, col2 integer); create view viewA as select col1 from a where col1>col2; insert into a values(1,2); insert into a values(13,7); insert into a values(16,19); select * from viewA; update viewA set col1 = col1 + 2 where col1<15 select * from a; select * from viewA; update a set col1 = col1 + 2 where col1<15 10.select * from a; select * from viewA; What will be returned on line 6? On line 8? On line 10?

Solution Line 6: 13 Line 8: Line 10: Select * from a: (1,2), (15,7), (16,19) Select * from viewA: 15 Line 10: Select * from a: (3,2), (15,7), (16,19) Select * from viewA: 15, 3

Triggers create or replace function mystery() returns trigger as $$ begin new.a := (new.a + old.a)/2; new.b := (new.b + old.b)/2; return new; end $$ language plpgsql; create trigger mysterytrig before update on nums for each row execute procedure mystery(); update nums set a = a+2 where b>4; select * from nums; Solution: (2,5), (3,6), (6,3) nums a b 1 5 2 6 3

Triggers create or replace function mystery() returns trigger as $$ begin new.a := (new.a + old.a)/2; new.b := (new.b + old.b)/2; return new; end $$ language plpgsql; create trigger mysterytrig after update on nums for each row execute procedure mystery(); update nums set a = a+2 where b>4; select * from nums; Solution: (3,5), (4,6), (6,3) nums a b 1 5 2 6 3

Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return null; end $$ language plpgsql; create trigger enigmatrig after insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; Solution: First:10, 21, 5, 2 Second: 27, 4 first a 10 21 second b

Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return null; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; Solution: First:10, 21 Second: 27, 4 first a 10 21 second b

Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return new; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; Solution: First:10, 21, 5, 2 Second: 27, 4 first a 10 21 second b

Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(new.A * new.A); update second set b=b+1 where b>12; return old; end $$ language plpgsql; create trigger enigmatrig before insert on first for each row execute procedure enigma(); insert into first values(5); insert into first values(2); select * from first; select * from second; Solution: First:10, 21 Second: 27, 4 first a 10 21 second b

Triggers create or replace function enigma() returns trigger as $$ begin insert into second values(old.A * new.A); update second set b=b+1; return old; end $$ language plpgsql; create trigger enigmatrig after update on first for each row execute procedure enigma(); update first set a = a+3; select * from first; select * from second; Solution: The answer is order dependent. First will have 4,5. We will either get: Second with 6, 11 or Second with 5, 12 first a 1 2 second b

Triggers create or replace function what() returns trigger as $$ declare r nums%ROWTYPE; begin select max(a) into r from nums; IF r.a<new.a THEN insert into nums values(new.a-1); END IF; RETURN new; end $$ language plpgsql; create trigger whattrig before insert on nums for each row execute procedure what(); insert into nums values(4); select * from nums; insert into nums values(0); select * from nums; Solution: The first select will return 1,1,2,3,4 The second select will return 1,1,2,3,4,0 nums a 1

Triggers create or replace function whatr() returns trigger as $$ begin insert into S values(1); RETURN null; end $$ language plpgsql; create or replace function whats() returns trigger as $$ insert into T values(1); R a S a T a

Triggers (cont) create trigger whatstrig before insert on S for each statement execute procedure whats(); create trigger whatrtrig before insert on R execute procedure whatr(); insert into R values(0); select * from R; select * from S; select * from T; insert into S values(0); Solution: After first insert R: 0, S: 1, T: 1 After second insert R: 0 , S: 0, 1, T: 1, 1 R a S a T a