PostgreSQL (2) by Aswin
-
Upload
agate-studio -
Category
Education
-
view
116 -
download
2
Transcript of PostgreSQL (2) by Aswin
@agatestudio
PostGreSQL
Aswin Juari
Knight
Agate Studio
@agatestudio @agatestudio @agatestudio
Overview
• Introduction
• Differences with MySQL
• Installation
• Unique Features
• Notes
@agatestudio @agatestudio @agatestudio
Introduction
• Advanced Open Source SQL
– More features
• Unique Features:
– Table Inherintance
– Create Customized Function
– Etc
@agatestudio @agatestudio @agatestudio
Differences With MySQL
PostgreSQL MySQL
Access defined by file configuration: (Edit postgresql.conf to define who can access database and from where)
Access is created by MySQL (create user ‘something’@’some_ip’)
User defined by UNIX/Server User is created with MySQL
Port: 5432 Port: 3306
Can Store Array Cannot Store Array
@agatestudio @agatestudio @agatestudio
Installation
• What you need (Case : PHP – Postgresql)
– Postgresql
– Postgresql-server
– Php-postgresql
– Php-pdo
• Client Tools
– pgadmin
@agatestudio @agatestudio @agatestudio
Installation (2)
• Setting:
– Service postgresql initdb
– /var/lib/pgsql9/conf/pgsql.conf
• Listen address : *
– /var/lib/pgsql9/conf/pg_hba.conf
• Set which host/user that can access postgresql
• Create db
– su – postgres
– Create database something;
– Create user some_user with password ‘some_password’;
– Grant all privileges on database “something” to some_user;
@agatestudio @agatestudio @agatestudio
Unique Feature
• PL/PGSQL
– Scripting in database level
– Advantage: • source code in application level looks more simpler.
• Extra round trips between client and server are eliminated
• Intermediate result that client doesn’t need do not have to be transferred between client and server
• Multiple round of query parsing can be avoided.
@agatestudio @agatestudio @agatestudio
Basic Expression PLPGSL
[ <<label>> ]
[ DECLARE declarations ]
BEGIN
statements
END
[ label ];
@agatestudio @agatestudio @agatestudio
Sample PLSQL (Fibonacci) CREATE OR REPLACE FUNCTION fib(
fib_for integer
) RETURNS integer AS $$
DECLARE
some_var integer := 1;
BEGIN
IF fib_for < 2 THEN
RETURN fib_for;
END IF;
ret := fib(fib_for - 2) + fib(fib_for - 1);
RETURN ret;
END;
$$ LANGUAGE plpgsql;
@agatestudio @agatestudio @agatestudio
• Capital
City_id Name State
1 Bandung Jawa Barat
2 Surabaya Jawa Timur
3 Manado Sulawesi Utara
@agatestudio @agatestudio @agatestudio
Sample PLPSQL (2)
CREATE OR REPLACE FUNCTION test4
(state_name character varying)
RETURNS SETOF character varying AS
$BODY$
BEGIN
return query SELECT name from capital where
"state"=state_name;
END;
$BODY$
LANGUAGE plpgsql;
Select test4(‘Jawa Barat’);
@agatestudio @agatestudio @agatestudio
Sample PGSQL (3) CREATE TYPE my_type (f1 varchar(10), f2 varchar(10), ... );
CREATE OR REPLACE FUNCTION get_obj (name text)
RETURNS my_type
AS $$
DECLARE result_record my_type;
BEGIN
SELECT f1, f2 INTO result_record.f1, result_record.f2
FROM table1 WHERE pk_col = 42;
SELECT f3 INTO result_record.f3 FROM table2
WHERE pk_col = 24;
return result_record;
END
$$ language plpgsql;
@agatestudio @agatestudio @agatestudio
Unique Feature (2)
• Table Inheritance – Like Class Inheritance and FOREIGN KEY
– Child Table akan mewarisi atribut dari parent table
– Table Entry (Row) in child table will be inserted to parent table
– Can be multiple inheritance.
• Advantage: – The number of rows that processed can be lower
@agatestudio @agatestudio @agatestudio
Example Table inheritance
CREATE TABLE cities (
name text,
population float,
);
CREATE TABLE capitals (
state char(2) )
INHERITS (cities);
@agatestudio @agatestudio @agatestudio
Table Inheritance
• DELETE/UPDATE
– Delete/Update will remove/update rows from original table and child or parent table.
@agatestudio @agatestudio @agatestudio
Notes
• Utilize Firewall (iptables) to Restrict Access
• Be careful with 8.x. (This version has no ‘create table A if not exists’
• Quote is different from double quote
• If using data binary, use ‘false’ not false, and ‘true’ not true (In PHP).
@agatestudio @agatestudio @agatestudio
Source
• http://www.postgresql.org/docs/9.2/
• http://www.codeproject.com/Articles/33734/How-to-write-PL-pgSQL-functions-for-PostgreSQL