Your SlideShare is downloading. ×
PostgreSQL (2) by Aswin
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PostgreSQL (2) by Aswin

107

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
107
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. @agatestudio PostGreSQL Aswin Juari Knight Agate Studio
  • 2. @agatestudio@agatestudio@agatestudio Overview • Introduction • Differences with MySQL • Installation • Unique Features • Notes
  • 3. @agatestudio@agatestudio@agatestudio Introduction • Advanced Open Source SQL – More features • Unique Features: – Table Inherintance – Create Customized Function – Etc
  • 4. @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
  • 5. @agatestudio@agatestudio@agatestudio Installation • What you need (Case : PHP – Postgresql) – Postgresql – Postgresql-server – Php-postgresql – Php-pdo • Client Tools – pgadmin
  • 6. @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;
  • 7. @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.
  • 8. @agatestudio@agatestudio@agatestudio Basic Expression PLPGSL [ <<label>> ] [ DECLARE declarations ] BEGIN statements END [ label ];
  • 9. @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;
  • 10. @agatestudio@agatestudio@agatestudio • Capital City_id Name State 1 Bandung Jawa Barat 2 Surabaya Jawa Timur 3 Manado Sulawesi Utara
  • 11. @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’);
  • 12. @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;
  • 13. @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
  • 14. @agatestudio@agatestudio@agatestudio Example Table inheritance CREATE TABLE cities ( name text, population float, ); CREATE TABLE capitals ( state char(2) ) INHERITS (cities);
  • 15. @agatestudio@agatestudio@agatestudio Table Inheritance • DELETE/UPDATE – Delete/Update will remove/update rows from original table and child or parent table.
  • 16. @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).
  • 17. @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

×