Download presentation
Presentation is loading. Please wait.
Published byTerence Small Modified over 9 years ago
1
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL
2
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
3
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
4
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
5
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)
6
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
7
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)
8
Views in PostgreSQL (6) DROP VIEW view_name DROP VIEW CountryMAX DROP VIEW IF EXISTS CountryMAX
9
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;
10
Views in PostgreSQL (8) Views can hide complexity Views can be used as a security mechanism Many different perspectives of the same table
11
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 100000 START 1 CREATE TABLE products (product_id interger not null nextval(‘product_id_seq’), description text, );
12
Sequence in PostgreSQL (2) ALTER SEQUENCE seq_name ALTER SEQUENCE product_id START WITH 20 ALTER SEQUENCE product_id RESTART DROP SEQUENCE product_id
13
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
14
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
15
Structure of PL/pgSQL
16
Declarations (1) Declaring PL/pgSQL variable
17
Declarations (2) Declaring PL/pgSQL variable and assigning values
18
Declarations (3) Declaring Function Parameters (1) directly give a name to the parameter in the command (2) name ALIAS FOR $n;
19
Declarations (4) Directly using argument variables
20
Declarations (5) Attributes %TYPE attribute
21
Declarations (6) Attributes %ROWTYPE attribute
22
Comment syntax Single-line comments Block comments
23
Basic Statements (1) Assignment Executing a Command with NO RESULT – PERFORM
24
Basic Statements (2) Executing a Command with a Single-row result
25
Basic Statements (3) Example
26
Basic Statements (4)
27
Basic Statements (5) FOUND – Boolean variable
28
Control Structures(1) RETURN expression
29
Control Structures(2) IF statements IF … THEN IF … THEN … ELSE IF … THEN … ELSIF … THEN … ELSE
30
Control Structures(3) CASE statements CASE … WHEN … THEN … ELSE … END CASE CASE WHEN … THEN … ELSE … END CASE
31
Control Structures(4) LOOP EXIT
32
Control Structures(5) CONTINUE WHILE
33
Control Structures(6) FOR (Integer Variant)
34
Control Structures(7) FOR (Looping through query results)
35
Control Structures(8) Trapping Errors http://www.postgresql.org/docs/9.1/static/errcodes- appendix.html#ERRCODES-TABLE http://www.postgresql.org/docs/9.1/static/errcodes- appendix.html#ERRCODES-TABLE
36
Cursors (1) Declaring Cursor Variables OPEN FOR query
37
Cursors (2) Using Cursors FETCH MOVE NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count FORWARD BACKWORD
38
Cursors (3) Using Cursors CLOSE Returning Cursor
39
Cursors (4) Looping Through a Cursor’s Result
40
Errors and Messages RAISE Example
41
Reference PostgreSQL Manuals PostgreSQL 9.1 http://www.postgresql.org/docs/9.1/static/index.html Practical PostgreSQL http://www.faqs.org/docs/ppbook/c19610.htm
42
Stored Procedure in PgAdmin 1 2 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.