Anda di halaman 1dari 5

1.

POSTGRE
########### ALTER SEQUENCE #################
ALTER SEQUENCE seq RESTART WITH 1;
############################################
su postgres
psql
\list
\i [filename]
\dt or \dt+
\df or \df+
\df+ [funcname]

->
->
->
->

-> login into postgres


-> using psql
-> show list database
execute file sql
show list table
show list function
show specific function

local : psql -h 192.168.7.101 -p 5432 -U postgres -w


live : psql postgres postgres
show database list : \list
connect to database : \c [nama database] [Owner_DB]
show table_list : \dt atau \dt+
show function : \df atau \df+
buat output result jadi terstrukutur kebawah : \x
balikin hasil query : raise notice '%', str_query;
restart service : /etc/init.d/postgresql-9.0 start/stop
update function :
bikin file di linux, edit komentar drop function (harus di drop dulu)
masuk ke postgres db nya, ketik \i [nama_file]
FILE BERUPA DUMP .SQL
psql -U <username> -d <dbname> -1 -f <filename>.sql
example : psql -U postgres -d and_tsel_me_db -1 -f <filename>.sql
FILE BERUPA BACKUP
pg_restore -i -h localhost -p 5432 -U postgres -d <dbname> -v /home/<dbname>.bac
kup
DUMP DB
pg_dump -U <user> <db_source> -f <nama_file_sql>
Command Description
\d
This command is in general the shortcut for describe . In other database sy
stems the desc keyword is used instead.
\dt
Displays all your user defined tables.
\dT
Displays all your user defined data types.
\df
Displays all your user defined functions.
\di
Displays all your user defined indexes.
\dv
Displays all your user defined views.
\dn
Displays all namespaces.
\du
Displays all users.
\l
Displays all databases.
\d [tablename] Describes the table [tablename] with all fields and indexes.
===============================
CREATE FUNCTION: return types
===============================
CREATE FUNCTION function_name (argument_name argument_type, argument_name argume
nt_type, ...) RETURNS return_type AS $$
function_body_query;
$$ language language_name;

In this post we'll add some additional options and features related to the funct
ion's output.
==== VOID FUNCTIONS ====
Functions can return nothing. To do that we declare them as "RETURNS void". This
is appropriate for non-SELECT commands such as INSERT:
CREATE FUNCTION addsalesdata(name varchar, month varchar, value decimal) returns
void as $$
insert into salesfigures values($1,$2,$3);
$$ language 'sql';
For these functions one can call SELECT function_name(...) or SELECT * FROM func
tion_name(...).
==== FUNCTIONS THAT RETURN A SINGLE VALUE ====
Functions can return one value (one int, one decimal, one text etc.). For these
functions one can call SELECT function_name(...) or SELECT * FROM function_name(
...).
CREATE FUNCTION totalsalesformonth(month varchar) returns decimal as $$
select sum(sales) from salesfigures where month=$1;
$$ language 'sql';
Another option to define a function's output is using OUT parameters. Here we do
not use RETURNS but we list the order, names and types of output alongside the
input arguments preceding each field with "OUT".
CREATE FUNCTION salesforpersonmonth4(month varchar, OUT name varchar, OUT value
decimal) as $$
select name,sales from salesfigures where month=$1;
$$ language 'sql';
aggs=# select * from salesforpersonmonth4('jan');
name | value
------+------joe |
34
(1 row)
Notice that this is returning just one record when in fact the query would produ
ce multiple rows of results.
==== FUNCTIONS THAT RETURN A SETOF VALUES ====
Functions can also return a set of values of the same type. That is, imagine it
returning a column of decimals or a column of text. To specify this we use the S
ETOF modifier. Here is a function that returns the sales figures for each salesp
erson for January.
CREATE FUNCTION salesformonth(month varchar) returns SETOF decimal as $$
select sales from salesfigures where month=$1;
$$ language 'sql';
aggs=# select * from salesformonth('jan');
salesformonth
--------------34
18
(2 rows)
==== FUNCTIONS THAT RETURN A SETOF ROWS ====

There are 4 options to return a set of rows.


1) RETURNS table
PostgreSQL functions (version 8.4 and later) can also return a table. That is, w
e define the fields of the SETOF rows that the function will return. Here is tha
t last function but with the person's name as well as their sales:
CREATE FUNCTION salesforpersonformonth(month varchar) returns table(name text, s
ales decimal) as $$
select name, sales from salesfigures where month=$1;
$$ language 'sql';
aggs=# select * from salesforpersonformonth('jan');
name | sales
------+------joe |
34
bob |
18
(2 rows)
The types that functions return can be standard PostgreSQL types but they can al
so be columns or tables that exist or they can be a type that the user defines.
2) Existing table
Here we have a table with a name and sales value:
aggs=# create table personsales(name varchar, sales decimal);
CREATE TABLE
A function can then return that type:
CREATE FUNCTION salesforpersonmonth(month varchar) returns SETOF personsales as
$$
select name,sales from salesfigures where month=$1;
$$ language 'sql';
aggs=# select * from salesforpersonmonth('jan');
name | sales
------+------joe |
34
bob |
18
(2 rows)
3) Column of existing table
Here is a function that returns a SETOF values of a type defined by the column o
f an existing table:
CREATE FUNCTION salesforpersonmonth2(month varchar) returns SETOF personsales.sa
les%TYPE as $$
select sales from salesfigures where month=$1;
$$ language 'sql';
aggs=# select * from salesforpersonmonth2('jan');
salesforpersonmonth2
---------------------34
18
(2 rows)
4) Defined type
We can create a new type by using CREATE TYPE. CREATE TYPE has a lot of options
but the basic use is

CREATE TYPE typename AS (fieldname1 type1, fieldname2 type2,...);


For instance,
CREATE TYPE personsales3 AS (name varchar, sales decimal);
Then, one can define
CREATE FUNCTION salesforpersonmonth3(month varchar) returns SETOF personsales3 a
s $$
select name,sales from salesfigures where month=$1;
$$ language 'sql';
========================================================
2. CRONTAB
#create crontab#
EDITOR='nano -w' crontab -e
1.
2.
3.
4.
5.

(*
(*
(*
(*
(*

pertama) : menit (isian : 0


59)
kedua) : jam (isian : 0 23)
ketiga) : hari dari bulan / tanggal (isian : 1 31)
keempat) : bulan (isian : 1
12)
kelima) : hari dari minggu (isian : 0
6) > 0 = minggu

========================================================
3. ajax
function checkShippingCost(){
$objCityId = document.getElementById("list_city_id").value ;
$objDealId = "<?php echo $varTemplate['deal_id'] ?>";
$(document).ready(function(){
$.ajax({
url:"<?php echo site_url("deal/getShippingCost");?>",
type: 'POST',
data: "city_id="+$objCityId+"&deal_id="+$objDealId,
dataType: "json",
success:function(result){
if(result['status'] == "sukses"){
document.getElementById('shippingCost').
value = result['price_value'];
}else{
alert('error');
}
}
});
});
}
========================================================
4. sphinx
indexer --rotate --all //rotate semua index
searchd --stop // stop searchd
searchd //start searchd
=========================================================
5. MYSQL

connect lewat terminal : mysql -u <userdb> -h <servername> -p <pass>


show database; -> melihat list database.
use <databasename>; -> konek ke database
show tables; -> meliaht list table dalam sebuah database.
menyimpan hasil query kedalam textfile
-> SELECT order_id,product_name,qty FROM orders INTO OUTFILE '/tmp/order
s.txt' (TEXTFILE)
-> SELECT order_id,product_name,qty FROM orders INTO OUTFILE '/tmp/order
s.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (C
SV FILE)
- dump data mysql
-> mysqldump -u... -p... mydb t1 t2 t3 > mydb_tables.sql

Anda mungkin juga menyukai