2014年7月30日 星期三

postgresql 9.3 筆記-2

Reserved Words
保留字集,建table欄位取名字之前都會先掃一下,以免造成困擾~
http://www.postgresql.org/docs/9.3/static/sql-keywords-appendix.html


Operators
其中比較特別的就是::,pg轉型的寫法
http://www.postgresql.org/docs/9.3/static/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS

Check Constraints
用於輸入值的檢查,在phppgadmin中,被放在"約束"的項目底下有CHECK
Check要通過(結果要true),或是該資料值是NULL才會過喔~
reference上使用CREATE TABLE的語法說明,是可以指定名稱或沒有名稱,不過透過UI介面,理論上就會有那個名稱了

CREATE TABLE products (
product_no integer,
name text,
price numeric
CHECK (price>0)
);

CREATE TABLE products (
product_no integer,
name text,
price numeric
CONSTRAINT positive_price CHECK (price>0)
);

So, to specify a named constraint, use the key word CONSTRAINT followed by an identifier followed by the constraint definition. (If you don’t specify a constraint name in this way, the system chooses a name for you.)
A check constraint can also refer to several columns. Say you store a regular price and a discounted price, and you want to ensure that the discounted price is lower than the regular price:

CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price>0),
discounted_price numeric CHECK (discounted_price>0),CHECK (price>discounted_price)
);




等同於
CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price>0),
discounted_price numeric, CHECK (discounted_price>0 AND price>discounted_price)
);

通過條件說明,null值也會通過,所以若要求非null且check通過的話,要額外設定非NULL的限制(Not-Null Constraints)
It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used.


Foreign Keys
外鍵,是在db裡面還蠻常用到的概念,除了基本的設定之後,外鍵的主要資料一定得先存在才能寫進去~意外發現pg裡面還有代入cascade的概念(第一次知道是從hibernate上得到的),可以自己連動處理資料還蠻方便的,但是rule得先讀懂,已免出現一些讓人傻眼的結果~

CREATE TABLE orders (
order_id integer PRIMARY KEY,
product_no integer REFERENCES products (product_no),
quantity integer
);

Now it is impossible to create orders with non-NULL product_no entries that do not appear in the products table.
We say that in this situation the orders table is the referencing table and the products table is the referenced table. Similarly, there are referencing and referenced columns.

CREATE TABLE order_items (
product_no integer REFERENCES products ON DELETE RESTRICT,
order_id integer REFERENCES orders ON DELETE CASCADE,
quantity integer,
PRIMARY KEY (product_no, order_id)
);

FK的用法
http://www.postgresql.org/docs/9.3/static/sql-createtable.html
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ]

其中action可以填入
NO ACTION
Produce an error indicating that the deletion or update would create a foreign key constraint violation. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. This is the default action.
RESTRICT
Produce an error indicating that the deletion or update would create a foreign key constraint violation. This is the same as NO ACTION except that the check is not deferrable.
CASCADE
Delete any rows referencing the deleted row, or update the values of the referencing column(s) to the new values of the referenced columns, respectively.
SET NULL
Set the referencing column(s) to null.
SET DEFAULT
Set the referencing column(s) to their default values. (There must be a row in the referenced table matching the default values, if they are not null, or the operation will fail.)

Privileges
因為pg內的權限定義到相當的細,所以比較習慣就指定專用的owner來處裡,記得是每個table的每一樣權限都得設定,如果降子就很累了,除非是有限制只開一兩個table使用。其語法主要關鍵字用GRANT/REVOKE。

There are different kinds of privileges:
SELECT,INSERT,UPDATE,DELETE,TRUNCATE,REFERENCES,TRIGGER,CREATE,CONNECT,TEMPORARY,EXECUTE, and USAGE.




Schemas


schema平常看起來是用不到,不過在有管理的需求上還是可以多加利用~

There are several reasons why one might want to use schemas:
•To allow many users to use one database without interfering with each other.
•To organize database objects into logical groups to make them more manageable.
•Third-party applications can be put into separate schemas so they do not collide with the names of other objects.


Partitioning
partition通常是在大量資料的查詢使用上會比較有效果,pg現在也有支援,不過看起來相當的複雜...裡頭寫了一個很大篇幅的文章,等以後真的不幸碰到再來試試吧@@


Foreign Data
這也是一個比較新的名詞,概念是聽來的,大概就是可以外掛一些像csv, 有format txt那樣的檔案資料, 透過下sql來做操作,因為不是真的將資料寫進DB裡,所以聽說可以應用在像log分析之類的東西上~


postgresql 9.3 筆記-1

Having
可用於groupby後再做條件的過濾~

SELECT city, max(temp_lo)
FROM weather
GROUP BY city;

city | max
---------------+-----
Hayward | 37
San Francisco | 46
(2 rows)

which gives us one output row per city. Each aggregate result is computed over the table rows match-ing that city. We can filter these grouped rows using HAVING:
SELECT city, max(temp_lo)
FROM weather
GROUP BY city
HAVING max(temp_lo)<40;

city | max
---------+-----
Hayward | 37
(1 row)


Transactions
這個寫程式就不太會用到~因為程式大多有做transaction,jdbc的autocommit/commit/rollback就是囉~
不過註記一下如果在sql內下的話是~

BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = ’Alice’;
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = ’Bob’;
-- oops ... forget that and use Wally’s account
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = ’Wally’;
COMMIT;

Inheritance
table繼承,以往得到的概念做法,都還是建兩個table,在程式實做時同時更新或異動,不過發現這裡有一個現成的做法
reference上寫了一個很棒的說明

Inheritance is a concept from object-oriented databases. It opens up interesting new possibilities of database design.
Let’s create two tables: A table cities and a table capitals. Naturally, capitals are also cities, so you want some way to show the capitals implicitly when you list all cities. If you’re really clever you might invent some scheme like this:

CREATE TABLE capitals (
name text,
population real,
altitude int, -- (in ft)
state char(2)
);
CREATE TABLE non_capitals (
name text,
population real,
altitude int -- (in ft)
);
CREATE VIEW cities AS
SELECT name, population, altitude FROM capitals
UNION
SELECT name, population, altitude FROM non_capitals;

This works OK as far as querying goes, but it gets ugly when you need to update several rows, for one thing.
A better solution is this:

CREATE TABLE cities (
name text,
population real,
altitude int -- (in ft)
);
CREATE TABLE capitals (
state char(2)
) INHERITS (cities);

In this case, a row of capitals inherits all columns (name,population, and altitude) from its parent, cities. The type of the column name is text, a native PostgreSQL type for variable length character strings. State capitals have an extra column,state, that shows their state.

In PostgreSQL,a table can inherit from zero or more other tables


postgresql 9.3 筆記-0

這幾年open source開始出現大量的商業化~
偶猜一部份原因也許是被某些公司買走~準備開始進行綁商品賣的方式~

看看走經很多的Java, 從7之後快要變成甲骨文專用sdk了~
好樣的spring綁自家產品還有縮減對其他陣營的支援(spring4+hibernate要漂亮~我看是要榜第三方開發連節方式了~hibernate記得是JBoss那邊的~),現在spring的lib下載點超難找的~

另外也許是因為景氣問題~這些open source沒有金援也要活不下去了...
總之~偶不太喜歡這些走經的open source,但是要求人家做慈善事業是也有點過份~唉唉~

偶愛用低postgresql,其實之前就有企業版的了~叫EDB,不過以前也不會廣告~最近再來看~廣告就大喇喇的擺在旁邊,文件也是用EDB的名稱~就只好說這些是有名open source的命運吧~那天從.org轉成.com,偶想也不該意外了~

最近9.4版出來了~不過可能還在白老鼠~看了版本功能差異
http://www.postgresql.org/about/featurematrix/
後來就先下了9.3來~
(之前最讚賞9.1的是Extension package installation,很好用,不像以前要跑好幾個sql檔,還要懷疑跑失敗的那幾行有沒有關係~囧a~)
主要是看上JSON的處裡,這是個很有前途的部份,偶個人覺得啦~
還有Foreign Tables, Foreign Data處理的部份,雖然以前也沒用過,但聽起來很厲害的樣子~
剛好有點時間,就來讀一下~

windows安裝保持以往快樂安裝包的好習慣~
點兩下裝完後,會問還有沒有要其他的額外工具~
基本的pgAdmin III原本就會裝,但是好用的phppgadmin也千萬別忘掉(因為會順便裝apache server很方便)

身為PostGIS的支持者~有Extension 和win快樂安裝包真是個方便~

至於筆記的來源主要是
http://www.postgresql.org/docs/manuals/

只挑個人覺得要記錄的部份來寫,有些很熟或是覺得沒必要的就跳過~

application server和solaris一些小東西

 備註一下...
這次用到的環境是solaris 10+sun application server

solaris 雖然是liunx, 但是用起來還是有差...

cd , ls 差不多,習慣用-l都還好~
不過在檔案後面會出現*...好像表示非owner的檔案意思或是那是root的檔案~
用root來看就不會有*

在pietty上看起來的差別是~不曉得是不是建的人有設定~還是他就是那樣子?!
root的指令下法好像不能用左右改字~會變特殊符號?
所以只能一個字一個字敲~還好pietty的貼上功能還可以用QQ~
另外root看不到所在路徑(>左邊通常會顯示路徑)~但是會跟su前的那個路徑一樣~
為了確保安全~在執行前都先ls一下~
非root帳號~指令列就會標明完整的路徑~

常用的 tail -n 100 在solaris上要改成
tail -100 server.log

tar上~有些引數不能用~所以後來都用這組(-cf)
tar -cf abc.0728.tar abc/

chmod上改用文字描述(user+write權限)
chmod u+w aaa.txt

hosts位置也有點不同~
/etc/hosts還是有東西~但那是捷徑... ls -l後就可以看到實體位置

sun application server
偶只能說評價差~不過當年同期的TOMCAT可能還在3~5,BUG很慘烈的年代~也許是沒那麼差~不過用過TOMCAT6以後的穩定度~心中有很多os...

 先說中文化的介面真是造成認知上的困擾...

正統布署是用war,在application server的admin GUI上上傳,再指定在那幾個instance(實例),雖然偶覺得有指定了,但是他好像跟本就不鳥,不知道是中文翻議錯誤還是怎樣~
到server下去看source日期有時後跟本沒變~ (可是有時後又有~搭配七月特效,這真的只是古早的靈異問題嗎一.一a)

所以最保險的做法還是布署後,再去各server的實體路徑下確認檔案更換了沒,測試修bug就乾脆去換server下的單檔再重新啟動server就好了~因為佈署非常的慢~

某次悲慘的情境就是同時deploy兩個server...結果不知道布太久斷掉還怎樣,總之在gui上就出現unknow(狀態不明)的情況,此時用gui的任何按鈕就會送你一堆莫名其妙的Exception...
過一天還是一樣~google上沒找到解法,因為真的很難google...名字取太差了,難怪之後改玻離魚還比較好辨識

總之解法就是,在看他的目錄結構的時後,看到server下有個bin,裡面還寫得很好懂的start, stop,鼓起勇氣給它stopserv下去,反正原本也就爛了,再爛也沒差了...stopserv後果然有些不好看的訊息,再去gui上看,狀態終於歸回未啟動!再啟動就正常了^^~

猜測可能是由agent控制server時,可能timeout(因為當時真的等到想關網頁了),或是某些檔案內容鎖檔io問題或是balabala的,變成agent狀態不明無法判別再處裡。因為偶看到的是EOFException,這個通常是搶檔案或是檔案沒寫全的錯誤。