Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Chapter 4B: More Advanced PL/SQL Programming
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Structured query language This is a presentation by JOSEPH ESTRada on the beauty of Structured Query Language.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Module 5: Data Access. Overview Introduce database components involved in data access Introduce concepts of Transact -SQL and Procedural SQL as tools.
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Agenda Journalling More Embedded SQL. Journalling.
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.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Programming in postgreSQL with PL/pgSQL ProceduralLanguageextension topostgreSQL.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Advanced SQL: Cursors & Stored Procedures
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
PL/SQL Procedural Language / Structured Query Language.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Technology Jing Shen.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Stored Procedure used in PosgreSQL.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.
Oracle 11g: SQL Chapter 7 User Creation and Management.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 7 User Creation and Management Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL Triggers, Functions & Stored Procedures Programming Operations.
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.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Programming in postgreSQL with PL/pgSQL
Programming in postgreSQL with PL/pgSQL
Trigger used in PosgreSQL
PL/pgSQL
The Basics of Data Manipulation
PostgreSQL Conference East 2009
ITEC 313 Database Programming
Views, Sequence, and Stored Procedure used in PosgreSQL
Stored Procedure used in PosgreSQL
Stored Procedure used in PosgreSQL
Stored Procedure used in PosgreSQL
The Basics of Data Manipulation
CS4222 Principles of Database System
Stored Procedure used in PosgreSQL
مقدمة في قواعد البيانات
Stored Procedure used in PosgreSQL
Database systems Lecture 3 – SQL + CRUD
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 8 Advanced SQL.
Information Management
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL

Types of SQL Statements Data Definition Language (DDL) Statements CREATE, ALTER, DROP Data Control Language Statements GRANT, REVOKE Data Manipulation Language (DML) Statements INSERT, DELETE, UPDATE Transaction Control Statements Session Control Statements

Views in PostgreSQL (1) What is view? A virtual table based on the result-set of an SQL statement It contains rows and columns The fields in a view are fields from one or more tables Syntax CREATE (OR REPLACE) (RECURSIVE) VIEW view_name AS SQL Statements ALTER VIEW view_name AS SQL Statements DROP VIEW view_name UPDATE view_name

Views in PostgreSQL (2) SELECT Continent, Region, Code, Code2, Name FROM country ORDER BY Continent, Region, Code CREATE VIEW CountryView AS SELECT Continent, Region, Code, Code2, Name FROM country ORDER BY Continent, Region, Code SELECT Continent, COUNT(*) FROM CountryView GROUP BY Continent SELECT Name, Language FROM CountryView as a, countrylanguage as b WHERE a.Code = b.CountryCode

Views in PostgreSQL (3) CREATE [OR REPLACE] VIEW view_name AS SQL Statement CREATE VIEW CountryMAXGNP AS SELECT Name, GNP FROM country WHERE (Region, GNP) IN ( SELECT Region, MAX(GNP) FROM country GROUP BY Region) CREATE VIEW CountryMAXGNP AS SELECT Code, Name, GNP FROM country WHERE (Region, GNP) IN ( SELECT Region, MAX(GNP) FROM country GROUP BY Region) CREATE OR REPLACE VIEW CountryMAXGNP AS SELECT Code, Name, GNP FROM country WHERE (Region, GNP) IN ( SELECT Region, MAX(GNP) FROM country GROUP BY Region)

Views in PostgreSQL (4) SELECT co.Name, ci.Name, co.Population, ci.Population, ROUND(ci.Population / co.Population, 2) Scale FROM country co, city ci WHERE co.Code = ci.CountryCode CREATE VIEW ScaleView AS SELECT co.Name, ci.Name, co.Population, ci.Population, ROUND(ci.Population / co.Population, 2) Scale FROM country co, city ci WHERE co.Code = ci.CountryCode CREATE VIEW ScaleView AS SELECT co.Name CountryName, ci.Name CityName, co.Population CountryPop, ci.Population CityPop, ROUND(ci.Population / co.Population, 2) Scale FROM country co, city ci WHERE co.Code = ci.CountryCode CREATE VIEW ScaleView (CountryName, CityName, CountryPop, CityPop, Scale) AS SELECT co.Name, ci.Name, co.Population, ci.Population, ROUND(ci.Population / co.Population, 2) Scale FROM country co, city ci WHERE co.Code = ci.CountryCode

Views in PostgreSQL (5) ALTER VIEW view_name AS SQL Statement ALTER VIEW CountryMAXGNP AS SELECT Continent, Code, Name, GNP FROM country WHERE (Region, GNP) IN ( SELECT Region, MAX(GNP) FROM country GROUP BY Region) CREATE OR REPLACE VIEW CountryMAXGNP AS SELECT Continent, Code, Name, GNP FROM country WHERE (Region, GNP) IN ( SELECT Region, MAX(GNP) FROM country GROUP BY Region)

Views in PostgreSQL (6) DROP VIEW view_name DROP VIEW CountryMAX DROP VIEW IF EXISTS CountryMAX

Views in PostgreSQL (7) CREATE VIEW comedies AS SELECT * FROM filems WHERE kind= ‘Comedy’; CREATE VIEW universal_comedies AS SELECT * FROM comedies WHERE classification= ‘U’ WITH LOCAL CHECK OPTION; CREATE VIEW pg_comedies AS SELECT * FROM comedies WHERE classification= ‘PG’ WITH CASCADE CHECK OPTION;

Views in PostgreSQL (8) Views can hide complexity Views can be used as a security mechanism Many different perspectives of the same table

Sequence in PostgreSQL (1) CREATE SEQUENCE seq_name INCREMENT increment MINVALUE minvalue MAXVALUE maxvalue START startvalue CREATE SEQUENCE product_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE START 1 CREATE TABLE products (product_id interger not null nextval(‘product_id_seq’), description text, );

Sequence in PostgreSQL (2) ALTER SEQUENCE seq_name ALTER SEQUENCE product_id START WITH 20 ALTER SEQUENCE product_id RESTART DROP SEQUENCE product_id

What are stored procedures A subroutine available to applications that access a relational database system. PL/pgSQL : A loadable procedural language. Creates functions and trigger procedures Adds control structures Performs complex computation Inherits all user-defined types, functions Can be defined to be trusted by the server Easy to use

Why do we need stored procedure One Query Wait, receive, process/compute Database Server Internet Reduce roundtrips across the network Can make security easier to manage Are precompiled

Structure of PL/pgSQL

Declarations (1) Declaring PL/pgSQL variable

Declarations (2) Declaring PL/pgSQL variable and assigning values

Declarations (3) Declaring Function Parameters (1) directly give a name to the parameter in the command (2) name ALIAS FOR $n;

Declarations (4) Directly using argument variables

Declarations (5) Attributes %TYPE attribute

Declarations (6) Attributes %ROWTYPE attribute

Comment syntax Single-line comments Block comments

Basic Statements (1) Assignment Executing a Command with NO RESULT – PERFORM

Basic Statements (2) Executing a Command with a Single-row result

Basic Statements (3) Example

Basic Statements (4)

Basic Statements (5) FOUND – Boolean variable

Control Structures(1) RETURN expression

Control Structures(2) IF statements IF … THEN IF … THEN … ELSE IF … THEN … ELSIF … THEN … ELSE

Control Structures(3) CASE statements CASE … WHEN … THEN … ELSE … END CASE CASE WHEN … THEN … ELSE … END CASE

Control Structures(4) LOOP EXIT

Control Structures(5) CONTINUE WHILE

Control Structures(6) FOR (Integer Variant)

Control Structures(7) FOR (Looping through query results)

Control Structures(8) Trapping Errors appendix.html#ERRCODES-TABLE appendix.html#ERRCODES-TABLE

Cursors (1) Declaring Cursor Variables OPEN FOR query

Cursors (2) Using Cursors FETCH MOVE NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count FORWARD BACKWORD

Cursors (3) Using Cursors CLOSE Returning Cursor

Cursors (4) Looping Through a Cursor’s Result

Errors and Messages RAISE Example

Reference PostgreSQL Manuals PostgreSQL Practical PostgreSQL

Stored Procedure in PgAdmin 1 2 3