Views, Triggers and Recursive Queries

Slides:



Advertisements
Similar presentations
Day 9. SELECT INSERT UPDATE DELETE » The standard UPDATE statement. UPDATE table SET field1=val1, field2=val2 WHERE condition » Multiple table UPDATE.
Advertisements

DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
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.
1 Constraints, Triggers and Active Databases Chapter 9.
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.
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
SQL/Server Stephen Cunningham Thema Davis. Problem Domain Designed for retrieval and management of data Defines the structures and operations of a data.
Click Here to Begin the Game CHOICE 1CHOICE 2CHOICE 3 CHOICE CHOICE
Database Systems Lecture 5 Natasha Alechina
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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.
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.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
Advanced SQL: Cursors & Stored Procedures
Хранимые процедуры PL/plPgSQL. Общий вид [ DECLARE declarations ] BEGIN statements END.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
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.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
Computer Science 101 For Statement. For-Statement The For-Statement is a loop statement that is especially convenient for loops that are to be executed.
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.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
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.
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.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Programming in postgreSQL with PL/pgSQL
Programming in postgreSQL with PL/pgSQL
Trigger used in PosgreSQL
PL/pgSQL
DataBase Logic in Business Applications
Insert, Update and the rest…
PostgreSQL Conference East 2009
Views, Triggers and Recursive Queries
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
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
JOINs JOINs can be used to combine tables A CROSS JOIN B
CS4222 Principles of Database System
Instructor: Mohamed Eltabakh
CS122B: Projects in Databases and Web Applications Winter 2018
Chapter 7 Using SQL in Applications
Information Management
CS122B: Projects in Databases and Web Applications Spring 2018
Recursion in SQL See notes for the question SELECT WHERE
Triggers.
Triggers 7/11/2019 See scm-intranet.
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.

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 What will be returned on line 6? On line 8? On line 10?

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; 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; 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; 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; 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; 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; 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; 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; 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); R a S a T a