2 PostgreSQL 常è§é®é¢ï¼FAQï¼
4 æè¿æ´æ°ï¼2007 å¹´ 1 æ 30 æ¥ ææäº 17:37:01 EST
5 ä¸æçæè¿æ´æ°ï¼2007 å¹´ 2 æ 1 æ¥ ææå 12:55:04 CST
7 ç®åç»´æ¤äººåï¼Bruce Momjian (pgman@candle.pha.pa.us)
8 ä¸æçç»´æ¤äººåï¼Daojing.Zhouï¼doudou586@gmail.comï¼
11 http://www.postgresql.org/files/documentation/faqs/FAQ.htmlæ¥çã
13 ä¸æä½ç³»ç»å¹³å°ç¸å³çé®é¢å¯å¨http://www.postgresql.org/docs/faq/éæ¾å°ç
15 _________________________________________________________________
19 1.1)PostgreSQL æ¯ä»ä¹ï¼è¯¥æä¹åé³ï¼
20 1.2)è°æ§å¶å管çPostgreSQL ï¼
21 1.3)PostgreSQLççææ¯ä»ä¹ï¼
22 1.4)PostgreSQLå¯ä»¥è¿è¡å¨åªäºæä½ç³»ç»å¹³å°ä¸ï¼
23 1.5)æä»åªéè½å¾å°PostgreSQLï¼
24 1.6)ææ°ççPostgreSQL æ¯ä»ä¹ï¼
25 1.7)æä»åªéè½å¾å°å¯¹PostgreSQL çæ¯æï¼
26 1.8)æå¦ä½æ交ä¸ä¸ªBUGæ¥åï¼
27 1.9)æå¦ä½äºè§£å·²ç¥ç BUG ææ缺çåè½ï¼
28 1.10)è½å¤è·åçææ°ææ¡£æåªäºï¼
29 1.11)æåºè¯¥ææ ·å¦ä¹ SQL ï¼
30 1.12)å¦ä½æ交补ä¸ææ¯å å¥å¼åéä¼ï¼
31 1.13)PostgreSQL åå¶ä»æ°æ®åºç³»ç»æ¯èµ·æ¥å¦ä½ï¼
32 1.14)PostgreSQL å¯ä»¥å¤çæè¿å个å½å®¶å¤æ¶å¶çåååï¼
36 2.1)æ们å¯ä»¥ç¨ä»ä¹è¯è¨åPostgreSQL æ交éï¼
37 2.2)æä»ä¹å·¥å·å¯ä»¥æPostgreSQL ç¨äº Web 页é¢ï¼
38 2.3)PostgreSQL æ¥æå¾å½¢ç¨æ·çé¢åï¼
42 3.1)æææ ·æè½æPostgreSQL è£å¨ /usr/local/pgsql 以å¤çå°æ¹ï¼
43 3.2)æå¦ä½æ§å¶æ¥èªå¶ä»çµèçè¿æ¥ï¼
44 3.3)æææ ·è°æ´æ°æ®åºæå¡å¨ä»¥è·å¾æ´å¥½çæ§è½ï¼
45 3.4)PostgreSQL éå¯ä»¥è·å¾ä»ä¹æ ·çè°è¯ç¹æ§ï¼
46 3.5)为ä»ä¹å¨è¯å¾è¿æ¥ç»å½æ¶æ¶å°âSorry, too many clientsâ æ¶æ¯ï¼
47 3.6)PostgreSQLçå级è¿ç¨æåªäºå容ï¼
48 3.7)(使ç¨PostgreSQL)æéè¦ä½¿ç¨ä»ä¹è®¡ç®æºç¡¬ä»¶ ï¼
52 4.1) å¦ä½åªéæ©ä¸ä¸ªæ¥è¯¢ç»æç头å è¡ï¼ææ¯éæºçä¸è¡ï¼
54 å¦ä½æ¥ç表ãç´¢å¼ãæ°æ®åºä»¥åç¨æ·çå®ä¹ï¼å¦ä½æ¥çpsqléç¨å°çæ¥è¯¢æ令并æ¾ç
56 4.3) å¦ä½æ´æ¹ä¸ä¸ªå段çæ°æ®ç±»åï¼
57 4.4) åæ¡è®°å½ï¼å个表ï¼ä¸ä¸ªæ°æ®åºçæ大éå¶æ¯å¤å°ï¼
58 4.5) åå¨ä¸ä¸ªå¸åçææ¬æ件éçæ°æ®éè¦å¤å°ç£ç空é´ï¼
59 4.6) 为ä»ä¹æçæ¥è¯¢å¾æ¢ï¼ä¸ºä»ä¹è¿äºæ¥è¯¢æ²¡æå©ç¨ç´¢å¼ï¼
60 4.7) æå¦ä½æè½çå°æ¥è¯¢ä¼åå¨æ¯ææ ·è¯ä¼°å¤çæçæ¥è¯¢çï¼
61 4.8) æææ ·åæ£å表达å¼æç´¢å大å°åæ å³çæ
62 £å表达å¼æ¥æ¾ï¼ææ ·å©ç¨ç´¢å¼è¿è¡å¤§å°åæ å³æ¥æ¾ï¼
63 4.9) å¨ä¸ä¸ªæ¥è¯¢éï¼æææ ·æ£æµä¸ä¸ªå段æ¯å¦ä¸º
64 NULLï¼æå¦ä½æè½åç¡®æåºèä¸è®ºæå段æ¯å¦å«NULLå¼ï¼
65 4.10) åç§å符类åä¹é´æä»ä¹ä¸åï¼
66 4.11.1) æææ ·å建ä¸ä¸ªåºåå·åææ¯èªå¨éå¢çå段ï¼
67 4.11.2) æå¦ä½è·å¾ä¸ä¸ªæå¥çåºåå·çå¼ï¼
68 4.11.3) åæ¶ä½¿ç¨ currval() ä¼å¯¼è´åå¶ä»ç¨æ·çå²çªæåµåï¼
69 4.11.4) 为ä»ä¹ä¸å¨äºå¡å¼å¸¸ä¸æ¢åéç¨åºåå·å¢ï¼ä¸ºä»ä¹å¨åºåå·å段çåå¼ä¸
71 4.12) ä»ä¹æ¯ OIDï¼ä»ä¹æ¯ CTID ï¼
72 4.13) 为ä»ä¹ææ¶å°é误信æ¯âERROR: Memory exhausted in
74 4.14) æå¦ä½æè½ç¥éæè¿è¡ç PostgreSQL ççæ¬ï¼
75 4.15) æå¦ä½å建ä¸ä¸ªç¼ºçå¼æ¯å½åæ¶é´çå段ï¼
76 4.16) å¦ä½æ§è¡å¤è¿æ¥ï¼outer joinï¼æ¥è¯¢ï¼
77 4.17) å¦ä½æ§è¡æ¶åå¤ä¸ªæ°æ®åºçæ¥è¯¢ï¼
78 4.18) å¦ä½è®©å½æ°è¿åå¤è¡æå¤åæ°æ®ï¼
79 4.19) 为ä»ä¹æå¨ä½¿ç¨PL/PgSQLå½æ°åå临æ¶è¡¨æ¶ä¼æ¶å°é误信æ¯ârelation
80 with OID ##### does not existâï¼
81 4.20) ç®åæåªäºæ°æ®å¤å¶æ¹æ¡å¯ç¨ï¼
82 4.21) 为ä½æ¥è¯¢ç»ææ¾ç¤ºç表åæååä¸æçæ¥è¯¢è¯å¥ä¸
83 çä¸åï¼ä¸ºä½å¤§åç¶æä¸è½ä¿çï¼
84 _________________________________________________________________
88 1.1)PostgreSQL æ¯ä»ä¹ï¼è¯¥æä¹åé³ï¼
90 PostgreSQL è¯»ä½ Post-Gres-Q-Lï¼ææ¶åä¹ç®ç§°ä¸ºPostgres
91 ãæ³å¬ä¸ä¸å¶åé³ç人åå¯ä»è¿éä¸è½½å£°é³æä»¶ï¼ MP3 æ ¼å¼ ã
94 æ¯é¢åç®æ çå³ç³»æ°æ®åºç³»ç»ï¼å®å·æä¼ ç»åä¸æ°æ®åºç³»ç»çææåè½ï¼åæ¶åå«æå°å
95 ¨ä¸ä¸ä»£ DBMS ç³»ç»ç使ç¨çå¢å¼ºç¹æ§ãPostgreSQL
96 æ¯èªç±åè´¹çï¼å¹¶ä¸æææºä»£ç é½å¯ä»¥è·å¾ã
99 çå¼åéä¼ä¸»è¦ä¸ºå¿æ¿èï¼ä»ä»¬éå¸ä¸çåå°å¹¶éè¿äºèç½è¿è¡èç³»ï¼è¿æ¯ä¸ä¸ªç¤¾å
100 ºå¼å项ç®ï¼å®ä¸è¢«ä»»ä½å¬å¸æ§å¶ã
101 å¦æ³å å¥å¼åéä¼ï¼è¯·åè§å¼å人å常è§é®é¢ï¼FAQï¼
102 http://www.postgresql.org/files/documentation/faqs/FAQ_DEV.html
104 1.2) è°æ§å¶PostgreSQL ï¼
106 å¦æä½ å¨å¯»æ¾PostgreSQLçæé¨äººï¼ææ¯ä»ä¹ä¸
107 央å§åä¼ï¼ææ¯ä»ä¹æå±å¬å¸ï¼ä½ åªè½æ¾å¼äº---å 为ä¸ä¸ªä¹ä¸å
109 æ ¸å¿å§åä¼åCVS管çç»ï¼ä½è¿äºå·¥ä½ç»ç设ç«ä¸»è¦æ¯ä¸ºäºè¿è¡ç®¡çå·¥ä½èä¸æ
110 ¯å¯¹PostgreSQLè¿è¡ç¬å å¼æ§å¶ï¼PostgreSQL项ç®æ¯ç±ä»»ä½äººå
111 å¯åå çå¼å人å社åºåææç¨æ·æ§å¶çï¼ä½ æéè¦åçå°±æ¯è®¢éé®ä»¶å表ï¼åä¸è®¨è®º
112 å³å¯ï¼è¦åä¸PostgreSQLçå¼åè¯¦è§ å¼å人å常é®é¢ (Developer's FAQ)
115 1.3)PostgreSQLççææ¯ä»ä¹?
117 PostgreSQLçåå¸éµä»ç»å¸çBSDçæãå®å许ç¨æ·ä¸éç®çå°ä½¿ç¨PostgreSQLï¼çè³ä½
118 å¯ä»¥éå®PostgreSQLèä¸å«æºä»£ç ä¹å¯ä»¥ï¼å¯ä¸çéå¶å°±æ¯ä½ ä¸è½å 软件èªè
119 º«é®é¢èåæ们追è¯æ³å¾è´£ä»»ï¼å¦å¤å°±æ¯è¦æ±ææç软件æ·è´ä¸
120 é¡»åæ¬ä»¥ä¸çæ声æãä¸é¢å°±æ¯æ们æ使ç¨çBSDçæ声æå容ï¼
122 PostgreSQLæ°æ®åºç®¡çç³»ç»
124 é¨åçæï¼cï¼1996-2005ï¼PostgreSQL å¨çå¼åå°ç»ï¼é¨åçæï¼cï¼1994-1996
127 ï¼Portions copyright (c) 1996-2005,PostgreSQL Global Development Group
128 Portions Copyright (c) 1994-6 Regents of the University of
131 å许为任ä½ç®ç使ç¨ï¼æ·è´ï¼ä¿®æ¹åååè¿ä¸ªè½¯ä»¶åå®çææ¡£èä¸æ¶åä»»ä½è´¹ç
132 ¨ï¼ 并ä¸æ é¡»ç¾ç½²å æ
133 ¤è产ççè¯æï¼åææ¯ä¸é¢ççæ声æåæ¬æ®µä»¥åä¸é¢ä¸¤æ®µæååºç°å¨æææ·è´ä¸ã
135 ï¼Permission to use, copy, modify, and distribute this software and
136 its documentation for any purpose, without fee, and without a written
137 agreement is hereby granted, provided that the above copyright notice
138 and this paragraph and the following two paragraphs appear in all
141 å¨ä»»ä½æåµä¸ï¼å å·å¤§å¦é½ä¸æ¿æå 使ç¨æ
142 ¤è½¯ä»¶åå¶ææ¡£è导è´ç对任ä½å½äºäººçç´æ¥çï¼
143 é´æ¥çï¼ç¹æ®çï¼éå çæèç¸ä¼´èççæåï¼åæ¬å©çæ失ç责任ï¼å³ä½¿å å·å¤§å
144 ¦å·²ç»å»ºè®®äºè¿äºæ失çå¯è½æ§æ¶ä¹æ¯å¦æ¤ã
146 ï¼IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
147 PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
148 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
149 SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA
150 HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.ï¼
152 å å·å¤§å¦æç¡®æ¾å¼ä»»ä½ä¿è¯ï¼åæ¬ä½ä¸å±éäºæä¸ç¹å®ç¨éçåä¸åå©ççéå«ä¿è¯ã
153 è¿éæä¾çè¿ä»½è½¯ä»¶æ¯åºäºâå½ä½æ¯âçåºç¡çï¼å èå å·å¤§å
154 ¦æ²¡æ责任æä¾ç»´æ¤ï¼æ¯æï¼æ´æ°ï¼å¢å¼ºæèä¿®æ¹çæå¡ã
156 ï¼THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
157 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
158 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
159 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
160 CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
161 UPDATES, ENHANCEMENTS, OR MODIFICATIONS.ï¼
163 1.4)PostgreSQL å¯ä»¥è¿è¡å¨åªäºæä½ç³»ç»å¹³å°ä¸ï¼
165 ä¸è¬è¯´æ¥ï¼ä»»ä½ç°å¨å¯¹ UNIX å¼å®¹çæä½ç³»ç»ä¹ä¸é½è½è¿è¡PostgreSQL
166 ãå¨å®è£æåéååºäºåå¸æ¶ç»è¿æç¡®æµè¯çå¹³å°ã
168 PostgreSQlä¹å¯ä»¥ç´æ¥è¿è¡å¨åºäºå¾®è½¯Windows-NTçæä½ç³»ç»ï¼å¦Win2000
169 SP4ï¼WinXP å Win2003ï¼å·²å¶ä½å®æçå®è£åå¯ä»
170 http://pgfoundry.org/projects/pginstallerä¸è½½ï¼åºäºMSDOSçWindowsæä½ç³
171 »ç» ï¼Win95ï¼Win98ï¼WinMeï¼éè¦éè¿Cygwin模æç¯å¢è¿è¡PostgreSQLã
173 åæ¶ä¹æä¸ä¸ªä¸ºNovell Netware 6å¼åççæ¬å¯ä»
174 http://forge.novell.comè·åï¼ä¸ºOS/2(eComStation)å¼åççæ¬å¯ä»
175 http://hobbes.nmsu.edu/cgi-bin/h-search?sh=1&button=Search&key=postgre
176 SQL&stype=all&sort=type&dir=%2F ä¸è½½ã
178 1.5) æä»åªéè½å¾å°PostgreSQLï¼
180 éè¿æµè§å¨å¯ä»http://www.postgresql.org/ftp/ä¸è½½ï¼ä¹å¯éè¿FTPï¼ä»
181 ftp://ftp.PostgreSQL.org/pub/ç«ç¹ä¸è½½ã
183 1.6) ææ°ççPostgreSQLæ¯ä»ä¹ï¼
185 PostgreSQL ææ°ççæ¬æ¯çæ¬ 8.2.1 ã
187 æ们计åæ¯å¹´åå¸ä¸ä¸ªä¸»è¦å级çæ¬ï¼æ¯å 个æåå¸ä¸ä¸ªå°çæ¬ã
189 1.7) æä»åªéè½å¾å°å¯¹PostgreSQLçæ¯æï¼
191 PostgreSQL社åºéè¿é®ä»¶å表为å¶å¤§å¤æ°ç¨æ·æä¾å¸®å©ï¼è®¢éé®ä»¶å表ç主
193 http://www.postgresql.org/community/lists/ï¼ä¸è¬æåµä¸ï¼åå å¥General æ
194 Bugé®ä»¶å表æ¯ä¸ä¸ªè¾å¥½çå¼å§ã
196 主è¦çIRCé¢éæ¯å¨FreeNode(irc.freenode.net)ç#postgresqlï¼ä¸ºäºè¿ä¸æ
197 ¤é¢éï¼å¯ä»¥ä½¿ç¨UNIXç¨åºircï¼å¶æä»¤æ ¼å¼ï¼ irc -c '#postgresql'
198 "$USER" irc.freenode.net ï¼æè使ç¨å¶ä»IRC客æ·ç«¯ç¨åºãå¨æ¤ç½ç»ä¸è¿å
199 å¨ä¸ä¸ªPostgreSQLç西ççé¢é(#postgersql-es)åæ³è¯é¢é
200 (#postgresql-fr)ãåæ ·å°ï¼å¨EFNETä¸ä¹æä¸ä¸ªPostgreSQLç交æµé¢éã
202 å¯æä¾åä¸æ¯æçå¬å¸å表å¯å¨http://techdocs.postgresql.org/companies.phpæµ
205 1.8) æå¦ä½æ交ä¸ä¸ªBUGæ¥åï¼
208 http://www.postgresql.org/support/submitbugï¼å¡«åBugä¸æ¥è¡¨æ ¼å³å¯ï¼åæ
209 ·ä¹å¯è®¿é®ftpç«ç¹ftp://ftp.PostgreSQL.org/pub/
210 æ£æ¥ææ æ´æ°çPostgreSQLçæ¬æè¡¥ä¸ã
212 éè¿ä½¿ç¨Bugæäº¤è¡¨æ ¼ææ¯åå¾PostgreSQLé®ä»¶å表çBugé常ä¼æ以ä¸ä¹ä¸åå¤ï
214 * ææ交å容ä¸æ¯ä¸ä¸ªBugåå¶ä¸æ¯Bugçåå ã
215 * ææ交å容æ¯ä¸ä¸ªå·²ç¥çBug并ä¸å·²ç»å å¥TODOå¾å¤çä»»å¡å表ã
216 * ææ交çBugå·²å¨å½åçæ¬ä¸è¢«ä¿®æ£ã
217 * ææ交çBug已修æ£ä½å°æªå°è¡¥ä¸å å¥ç°å¨çåå¸è½¯ä»¶åã
218 * 请æ±æ交èæä¾æ´è¯¦ç»çä¿¡æ¯ï¼
224 * ææ交å容æ¯ä¸ä¸ªæ°Bugï¼å°æ§è¡ä»¥ä¸å·¥ä½ï¼
225 + å建ä¸ä¸ªæ°è¡¥ä¸å¹¶å°å¶å å¥ä¸ä¸ä¸ªä¸»è¦çæ¬ææ¯å°çæ¹è¿çæ¬ä¸ã
226 + æ¤Bugææ¶ä¸è½ä¿®æ£ï¼å°è¢«å è³TODOå¾å¤çä»»å¡å表ã
228 1.9) æå¦ä½äºè§£å·²ç¥ç BUG ææ缺çåè½ï¼
230 PostgreSQL æ¯æä¸ä¸ªæ©å±ç SQL:2003 çåéãåéæ们çTODO
231 å表ï¼äºè§£å·²ç¥Bugå表ãæ缺çåè½åå°æ¥çå¼å计åã
233 è¦æ±å¢å æ°åè½çç³è¯·é常ä¼æ¶å°ä»¥ä¸ä¹ä¸çåå¤ï¼
234 * 该åè½å·²å å¥TODOå¾å¤çä»»å¡å表ã
235 * 该åè½ä¸æ¯å¿é¡»çï¼å 为ï¼
236 + å®æ¯ç°æçä¸ç¬¦åSQLæ åçæåè½çéå¤ã
237 + 该åè½æ§ä¼å¤§å¤§å¢å 代ç çå¤æç¨åºï¼è带æ¥ç好å¤æ¯å¾®ä¸è¶³éçã
238 + 该åè½æ¯ä¸å®å¨ææ¯ä¸å¯é çã
239 * 该åè½å°è¢«å å¥TODOå¾å¤çä»»å¡å表ã
241 PostgreSQLä¸ä½¿ç¨Bugè·è¸ªç³»ç»ï¼å 为æ们åç°å¨é®ä»¶å表ä¸
242 ç´æ¥åå¤ä»¥åä¿è¯TODOä»»å¡å表æ»æ¯å¤äºææ°ç¶æçæ¹å¼å·¥ä½æçä¼æ´é«ä¸äºãäºå®ä
243 ¸ï¼Bugä¸ä¼å¨æ们ç软件ä¸åå¨å¾é¿æ¶é´ï¼
244 对影åå¾å¤ç¨æ·çBugä¹æ»æ¯å¾å¿«ä¼è¢«ä¿®æ£ãå¯ä¸è½æ¾å°æææ¹è¿ãæé«åä¿®æ
245 £çå°æ¹æ¯CVSçæ¥å¿ä¿¡æ¯ï¼å³ä½¿æ¯å¨è½¯ä»¶æ°çæ¬çåå¸ä¿¡æ¯ä¸
246 ä¹ä¸ä¼ååºæ¯ä¸å¤ç软件æ´æ°ã
248 1.10) è½å¤è·åçææ°ææ¡£æåªäºï¼
250 PostgreSQLåå«å¤§éçææ¡£ï¼ä¸»è¦æ详ç»çåèæåï¼æå页åä¸äºçæµè¯ä¾åãåè§ /doc
251 ç®å½ï¼è¯æ³¨ï¼åºä¸º $PGHOME/docï¼ã
252 ä½ è¿å¯ä»¥å¨çº¿æµè§PostgreSQLçæåï¼å¶ç½åæ¯ï¼http://www.PostgreSQL.org/d
255 æ两æ¬å³äºPostgreSQLç书å¨çº¿æä¾ï¼å¨
256 http://www.PostgreSQL.org/docs/awbook.html å
257 http://www.commandprompt.com/ppbook/ ã ä¹æ大éçPostgreSQL书ç±å¯ä¾è´
258 ä¹°ï¼å¶ä¸æ为æµè¡çä¸æ¬æ¯ç±Korry Douglasç¼åçãå¨
259 http://techdocs.PostgreSQL.org/techdocs/bookreviews.phpä¸
260 ä¸æ大éæå³PostgreSQL书ç±çç®ä»ã å¨
261 http://techdocs.PostgreSQL.org/ä¸æ¶éäºæå³PostgreSQLç大éææ¯æç« ã
263 客æ·ç«¯çå½ä»¤è¡ç¨åºpsqlæä¸äºä»¥ \d
264 å¼å¤´çå½ä»¤ï¼å¯æ¾ç¤ºå³äºç±»åï¼æä½ç¬¦ï¼å½æ°ï¼èåçä¿¡æ¯ï¼ä½¿ç¨ \?
265 å¯ä»¥æ¾ç¤ºææå¯ç¨çå½ä»¤ã
267 æ们ç web ç«ç¹åå«æ´å¤çææ¡£ã
269 1.11) æåºè¯¥ææ ·å¦ä¹ SQL ï¼
271 é¦åèèä¸è¿°æå°çä¸PostgreSQLç¸å³ç书ç±ï¼å¦å¤ä¸æ¬æ¯Teach Yourself SQL in
272 21 Days, Second Editionï¼å¶è¯¦ç»ä»ç»çç½åæ¯
273 http://members.tripod.com/er4ebus/sql/index.htmï¼
274 æ们ç许å¤ç¨æ·å欢The Practical SQL Handbookï¼ Bowman, Judith S.
275 ç¼åï¼Addison-Wesleyå¬å¸åºçï¼å¶ä»çåå欢 The Complete Reference SQL,
276 Groff ç¼åï¼McGraw-Hillå¬å¸åºçã
278 å¨ä¸åç½åä¸ä¹æå¾å¥½çæç¨ï¼ä»ä»¬æ¯
279 * http://www.intermedia.net/support/sql/sqltut.shtm
280 * http://sqlcourse.com.
281 * http://www.w3schools.com/sql/default.asp
282 * http://mysite.verizon.net/Graeme_Birchall/id1.html
284 1.12)å¦ä½æ交补ä¸ææ¯å å¥å¼åéä¼ï¼
286 è¯¦è§ å¼å人å常è§é®é¢ (Developer's FAQ) ã
288 1.13)PostgreSQLåå¶ä»æ°æ®åºç³»ç»æ¯èµ·æ¥å¦ä½ï¼
290 è¯ä»·è½¯ä»¶æ好å ç§æ¹æ³ï¼åè½ï¼æ§è½ï¼å¯é æ§ï¼æ¯æåä»·æ ¼ã
293 PostgreSQL æ¥æ大ååç¨æ°æ®åºæå¤çåè½ï¼ä¾å¦ï¼äºå¡ï¼å
294 æ¥è¯¢ï¼è§¦åå¨ï¼è§å¾ï¼å¤é®åèå®æ´æ§åå¤æçéå®çã
295 æ们è¿æä¸äºå®ä»¬æ²¡æçç¹æ§ï¼å¦ç¨æ·å®ä¹ç±»åï¼ç»§æ¿ï¼è§ååå¤çæ¬å¹¶è
299 PostgreSQLåå¶ä»åç¨åå¼æºçæ°æ®åºå·æ类似çæ§è½ã对æäºå¤çå®æ¯è¾å¿«
300 ï¼å¯¹å¶ä»ä¸äºå¤çå®æ¯è¾æ¢ã ä¸å¶ä»æ°æ®åºç¸æ¯ï¼æ们çæ§è½ä¼å£é常å¨
304 æ们é½ç¥éæ°æ®åºå¿é¡»æ¯å¯é çï¼å¦åå®å°±ä¸ç¹ç¨é½æ²¡æãæ们åªååå°åå¸
305 ç»è¿è®¤çæµè¯çï¼ç¼ºé·æå°ç稳å®ä»£ç ãæ¯ä¸ªçæ¬è³å°æä¸ä¸ªæç beta
306 æµè¯æ¶é´ï¼å¹¶ä¸æ们çåå¸åå²æ¾ç¤ºæ们å¯ä»¥æä¾ç¨³å®çï¼ç¢åºçï¼å¯ç¨ä
307 ºç产使ç¨ççæ¬ãæ们ç¸ä¿¡å¨è¿æ¹é¢æ们ä¸å¶ä»çæ°æ®åºè½¯ä»¶æ¯ç¸å½çã
310 æ们çé®ä»¶å表æä¾ä¸ä¸ªé常大çå¼å人ååç¨æ·çç»ä»¥å¸®å©è§£å³æ碰å
311 °çä»»ä½é®é¢ãæ们ä¸è½ä¿è¯æ»æ¯è½è§£å³é®é¢ï¼ç¸æ¯ä¹ä¸ï¼åç¨æ°æ®åºè½¯
312 件ä¹å¹¶ä¸æ¯æ»è½å¤æä¾è§£å³æ¹æ³ã
313 ç´æ¥ä¸å¼å人åï¼ç¨æ·ç¾¤ï¼æååæºç¨åºæ¥è§¦ä½¿PostgreSQLçæ¯ææ¯å¶ä»æ°
314 æ®åºè¿è¦å¥½ãè¿æä¸äºåä¸æ§çå¨é¢ææ¯æ¯æï¼å¯ä»¥ç»æä¾ç»é£äºéè¦ç人ãï¼
318 æ们对任ä½ç¨éé½åè´¹ï¼åæ¬åç¨åéåç¨ç®çã
319 ä½ å¯ä»¥ä¸å éå¶å°åä½ ç产åéå å¥æ们ç代ç ï¼é¤äºé£äºæ们å¨ä¸é¢çç
320 æ声æé声æç BSDçæä¹å¤çå容ã
322 1.14) PostgreSQLå¯ä»¥å¤çæè¿å个å½å®¶å¤æ¶å¶çååå?
324 PostgreSQL 8.0ä¹åççæ¬æ¯ä½¿ç¨æä½ç³»ç»ä¸
325 çæ¶åºæ°æ®åºæ¥å¤çå¤æ¶å¶çä¿¡æ¯ï¼èª8.0çå以åççæ¬PostgreSQLä¼èªèº«å«æææ°çæ
327 _________________________________________________________________
331 2.1) æ们å¯ä»¥ç¨ä»ä¹è¯è¨åPostgreSQLæ交éï¼
333 PostgreSQL(缺çæåµ)åªå®è£æCåååµå¼Cçæ¥å£ï¼å¶ä»çæ¥å£é½æ¯ç¬ç«ç项ç®ï¼è½å¤
334 åå«ä¸è½½ï¼è¿äºæ¥å£é¡¹ç®ç¬ç«ç好å¤
335 æ¯ä»ä»¬å¯ä»¥æåèªçåå¸è®¡åååèªç¬ç«çå¼åç»ã
338 è¨å¦PHPé½æ访é®PostgreSQLçæ¥å£ï¼PerlãTCLãPython以åå¾å¤å¶ä»è¯è¨çæ¥å£å¨
339 http://gborg.postgresql.orgç½ç«ä¸çDrivers/Interfaceså°èå¯æ¾å°ï¼
340 并ä¸éè¿Internetå¾å®¹ææç´¢å°ã
342 2.2) æä»ä¹å·¥å·å¯ä»¥æPostgreSQLç¨äº Web 页é¢ï¼
344 ä¸ä¸ªä»ç»ä»¥æ°æ®åºä¸ºåå°çæºä¸éçç«ç¹æ¯ï¼http://www.webreview.comã
346 å¯¹äº Web éæï¼PHP æ¯ä¸ä¸ªæ好çæ¥å£ãå®å¨http://www.php.net/ã
348 对äºå¤æçä»»å¡ï¼å¾å¤äººéç¨ Perl æ¥å£å 使ç¨CGI.pmçDBD::Pg æ mod_perl ã
350 2.3)PostgreSQLæ¥æå¾å½¢ç¨æ·çé¢åï¼
352 åä¸ç¨æ·ææ¯å¼æºå¼å人åè½æ¾å°å¾å¤çæå³PostgreSQLçGUIå¾å½¢å·¥å·è½¯ä»¶ï¼å¨
353 PostgreSQL社åºææ¡£æä¸ä¸ªè¯¦ç»çå表ã
354 _________________________________________________________________
358 3.1)æææ ·è½æPostgreSQLè£å¨ /usr/local/pgsql 以å¤çå°æ¹ï¼
360 å¨è¿è¡ configure æ¶å ä¸ --prefix é项ã
362 3.2) æå¦ä½æ§å¶æ¥èªå¶ä»çµèçè¿æ¥ï¼
364 缺çæåµä¸ï¼PostgreSQLåªå许æ¥èªæ¬æºä¸éè¿ unix åå¥æ¥åæTCP/IPæ¹å¼çè¿æ¥ã
365 ä½ åªæå¨ä¿®æ¹äºéç½®æ件postgresql.confä¸
366 çlisten_addressesï¼ä¸ä¹å¨éç½®æ件$PGDATA/pg_hba.confä¸æå¼äº
367 åºäºè¿ç¨çµèï¼ host-based
368 ï¼ç身份认è¯ï¼å¹¶éæ°å¯å¨PostgreSQLï¼å¦åå¶ä»çµèæ¯ä¸è½ä¸ä½ çPostgreSQL
371 3.3) æææ ·è°æ´æ°æ®åºå¼æ以è·å¾æ´å¥½çæ§è½ï¼
373 æä¸ä¸ªä¸»è¦æ¹é¢å¯ä»¥æåPostgreSQLçæ½è½ã
376 è¿ä¸»è¦æ¶åä¿®æ¹æ¥è¯¢æ¹å¼ä»¥è·åæ´å¥½çæ§è½:
378 + å建索å¼ï¼åæ¬è¡¨è¾¾å¼åé¨åç´¢å¼ï¼
379 + 使ç¨COPYè¯å¥ä»£æ¿å¤ä¸ªInsertè¯å¥ï¼
380 + å°å¤ä¸ªSQLè¯å¥ç»æä¸ä¸ªäºå¡ä»¥åå°æ交äºå¡çå¼éï¼
381 + ä»ä¸ä¸ªç´¢å¼ä¸æåå¤æ¡è®°å½æ¶ä½¿ç¨CLUSTERï¼
382 + ä»ä¸ä¸ªæ¥è¯¢ç»æä¸ååºé¨åè®°å½æ¶ä½¿ç¨LIMITï¼
383 + 使ç¨é¢ç¼è¯å¼æ¥è¯¢ï¼Prepared Query)ï¼
384 + 使ç¨ANALYZE以ä¿æ精确çä¼åç»è®¡ï¼
385 + å®æä½¿ç¨ VACUUM æ pg_autovacuum
386 + è¿è¡å¤§éæ°æ®æ´æ¹æ¶åå é¤ç´¢å¼ï¼ç¶åé建索å¼ï¼
389 éç½®æ件postgres.confä¸
390 çå¾å¤è®¾ç½®é½ä¼å½±åæ§è½ï¼ææåæ°çå表å¯è§ï¼
391 管çåæå/æ°æ®åºæå¡å¨è¿è¡ç¯å¢/æ°æ®åºæå¡å¨è¿è¡éç½®ï¼
392 æå³åæ°ç解éå¯è§ï¼http://www.varlena.com/varlena/GeneralBits/Tid
393 bits/annotated_conf_e.html å
394 http://www.varlena.com/varlena/GeneralBits/Tidbits/perf.htmlã
397 计ç®æºç¡¬ä»¶å¯¹æ§è½çå½±åå¯æµè§
398 http://candle.pha.pa.us/main/writings/pgsql/hw_performance/inde
399 x.html å http://www.powerpostgresql.com/PerfList/ã
401 3.4)PostgreSQLéå¯ä»¥è·å¾ä»ä¹æ ·çè°è¯ç¹æ§ï¼
403 PostgreSQL æå¾å¤ç±»ä¼¼ log_*
404 çæå¡å¨éç½®åéå¯ç¨äºæ¥è¯¢çæå°åè¿ç¨ç»è®¡ï¼èè¿äºå·¥ä½å¯¹è°è¯åæ§è½æµè¯å¾æå¸
407 3.5) 为ä»ä¹å¨è¯å¾è¿æ¥æ¶æ¶å°âSorry, too many
408 clientsï¼å·²æ太å¤ç¨æ·è¿æ¥ï¼âæ¶æ¯ï¼
410 è¿è¡¨ç¤ºä½ 已达å°ç¼ºç100个并ååå°è¿ç¨æ°çéå¶ï¼ä½ éè¦éè¿ä¿®æ¹postgres
411 ql.confæ件ä¸çmax_connectionså¼æ¥
412 å¢å postmasterçåå°å¹¶åå¤çæ°ï¼ä¿®æ¹åééæ°å¯å¨postmasterã
414 3.6)PostgreSQLçå级è¿ç¨æåªäºå容 ï¼
416 PostgreSQL å¼åç»å¯¹æ¯æ¬¡å°çæ¬çå级主è¦åªåäºä¸äºBugä¿®æ£å·¥ä½ï¼å æ¤ä»
417 7.4.8 åçº§å° 7.4.9 ä¸éè¦ dump å restoreï¼ä»éè¦åæ
418 ¢æ°æ®åºæå¡å¨ï¼å®è£æ´æ°åç软件åï¼ç¶åéå¯æå¡å¨å³å¯ã
420 ææPostgreSQLçç¨æ·åºè¯¥å¨ææ¥è¿ï¼ä½ æ使ç¨ç主çæ¬ï¼çå°æ¹è¿çæ¬åå¸å°½å¿«åç
421 º§ã尽管æ¯æ¬¡å级å¯è½é½æä¸ç¹é£é©ï¼PostgreSQLçå°æ¹è¿çä»ä»æ¯è®¾è®¡ç¨æ¥ä
423 £ä¸äºBugçï¼ä»£ç æ¹å¨è¾å°ï¼æ以é£é©è¿æ¯å¾å°çãPostgreSQL社åºè®¤ä¸ºä¸è¬æ
424 åµä¸ä¸å级çé£é©è¿æ¯å¤äºå级çã
426 主çæ¬çå级ï¼ä¾å¦ä» 7.3 å° 7.4ï¼é常ä¼ä¿®æ¹ç³»ç»è¡¨åæ°æ®è¡¨çåé¨æ ¼å¼ã
427 è¿äºæ¹åä¸è¬æ¯è¾å¤æï¼å æ¤æ们ä¸ç»´ææ°æ®æ件çååå¼å®¹æ§ãå æ¤ä»èçæ¬ä¸
428 è¿è¡æ°æ®å¯¼åºï¼dumpï¼/ç¶åå¨æ°çæ¬ä¸
429 è¿è¡æ°æ®å¯¼å¥ï¼reloadï¼å¯¹ä¸»çæ¬çå级æ¯å¿é¡»çã
431 3.7)(使ç¨PostgreSQL)æéè¦ä½¿ç¨ä»ä¹è®¡ç®æºç¡¬ä»¶ ï¼
433 ç±äºè®¡ç®æºç¡¬ä»¶å¤§å¤æ°æ¯å¼å®¹çï¼äººä»¬æ»æ¯å¾åäºç¸ä¿¡ææ计ç®æºç¡¬ä»¶è
434 ´¨éä¹æ¯ç¸åçãäºå®ä¸ä¸æ¯ï¼ ECC RAMï¼å¸¦å¥å¶æ ¡éªçååï¼ï¼SCSI
435 ï¼ç¡¬çï¼åä¼è´¨ç主æ¿æ¯ä¸äºä¾¿å®è´§è¦æ´å å¯é ä¸å·ææ´å¥½çæ§è½ãPostgreSQL
436 å ä¹å¯ä»¥è¿è¡å¨ä»»ä½ç¡¬ä»¶ä¸ï¼
437 ä½å¦æå¯é æ§åæ§è½å¯¹ä½ çç³»ç»å¾éè¦ï¼ä½ å°±éè¦å¨é¢çç 究ä¸ä¸ä½ ç硬件éç
438 ½®äºãå¨æ们çé®ä»¶å表ä¸ä¹æå³äº 硬件éç½®åæ§ä»·æ¯ç讨论ã
439 _________________________________________________________________
443 4.1) å¦ä½åªéæ©ä¸ä¸ªæ¥è¯¢ç»æç头å è¡ï¼ææ¯éæºçä¸è¡ï¼
445 å¦æä½ åªæ¯è¦æåå è¡æ°æ®ï¼å¹¶ä¸ä½ å¨æ§è¡æ¥è¯¢ä¸
446 ç¥éç¡®åçè¡æ°ï¼ä½ å¯ä»¥ä½¿ç¨LIMITåè½ã å¦ææä¸ä¸ªç´¢å¼ä¸ ORDER BYä¸
447 çæ¡ä»¶å¹éï¼PostgreSQL å¯è½å°±åªå¤çè¦æ±ç头å æ¡è®°å½ï¼
448 ï¼å¦åå°å¯¹æ´ä¸ªæ¥è¯¢è¿è¡å¤çç´å°çæéè¦çè¡ï¼ãå¦æå¨æ§è¡æ¥è¯¢åè½æ¶ä¸ç¥éç¡®å
449 çè®°å½æ°ï¼ å¯ä½¿ç¨æ¸¸æ (cursor)åFETCHåè½ã
451 å¯ä½¿ç¨ä»¥ä¸æ¹æ³æåä¸è¡éæºè®°å½çï¼
458 å¦ä½æ¥ç表ãç´¢å¼ãæ°æ®åºä»¥åç¨æ·çå®ä¹ï¼å¦ä½æ¥çpsqléç¨å°çæ¥è¯¢æ令并æ¾ç¤ºå®ä»¬
461 å¨psqlä¸ä½¿ç¨ \dt å½ä»¤æ¥æ¾ç¤ºæ°æ®è¡¨çå®ä¹ï¼è¦äºè§£psqlä¸
462 çå®æ´å½ä»¤å表å¯ä½¿ç¨\? ï¼å¦å¤ï¼ä½ ä¹å¯ä»¥é读 psql çæºä»£ç
463 æ件pgsql/src/bin/psql/describe.cï¼å®åæ¬ä¸ºçæpsqlåææ å½ä»¤çè¾åºçææ SQL
464 å½ä»¤ãä½ è¿å¯ä»¥å¸¦ -E é项å¯å¨ psqlï¼ è¿æ ·å®å°æå°åºä½ å¨psqlä¸
465 æç»åºçå½ä»¤æ§è¡æ¶çåé¨å®é使ç¨çSQLæ¥è¯¢è¯
466 å¥ãPostgreSQLä¹æä¾äºä¸ä¸ªå¼å®¹SQLçINFORMATION SCHEMAæ¥å£ï¼
467 ä½ å¯ä»¥ä»è¿éè·åå³äºæ°æ®åºçä¿¡æ¯ã
469 å¨ç³»ç»ä¸ä¹æä¸äºä»¥pg_ æ头çç³»ç»è¡¨ä¹æè¿°äºè¡¨çå®ä¹ã
471 ä½¿ç¨ psql -l æ令å¯ä»¥ååºææçæ°æ®åºã
474 pgsql/src/tutorial/syscat.sourceæ件ï¼å®å举äºå¾å¤å¯ä»æ°æ®åºç³»ç»è¡¨ä¸
477 4.3) å¦ä½æ´æ¹ä¸ä¸ªå段çæ°æ®ç±»åï¼
479 å¨8.0çæ¬éæ´æ¹ä¸ä¸ªå段çæ°æ®ç±»åå¾å®¹æï¼å¯ä½¿ç¨ ALTER TABLE ALTER
482 å¨ä»¥åççæ¬ä¸ï¼å¯ä»¥è¿æ ·åï¼
484 ALTER TABLE tab ADD COLUMN new_col new_data_type;
485 UPDATE tab SET new_col = CAST(old_col AS new_data_type);
486 ALTER TABLE tab DROP COLUMN old_col;
489 ä½ ç¶åå¯ä»¥ä½¿ç¨VACUUM FULL tab æ令æ¥ä½¿ç³»ç»æ¶åæ ææ°æ®æå ç¨ç空é´ã
491 4.4) åæ¡è®°å½ï¼å个表ï¼å个æ°æ®åºçæ大éå¶æ¯å¤å°ï¼
495 å个æ°æ®åºæå¤§å°ºå¯¸ï¼ æ éå¶ï¼å·²åå¨æ 32TB çæ°æ®åºï¼
496 å个表çæå¤§å°ºå¯¸ï¼ 32 TB
497 ä¸è¡è®°å½çæå¤§å°ºå¯¸ï¼ 1.6 TB
498 ä¸ä¸ªå段çæ大尺寸? 1 GB
499 ä¸ä¸ªè¡¨éæ大è¡æ°ï¼ æ éå¶
500 ä¸ä¸ªè¡¨éæ大åæ°ï¼ 250-1600 ï¼ä¸åç±»åæå³ï¼
501 ä¸ä¸ªè¡¨éçæ大索å¼æ°éï¼ æ éå¶
503 å½ç¶ï¼å®éä¸æ²¡æçæ£çæ éå¶ï¼è¿æ¯è¦åå¯ç¨ç£ç空é´ãå¯ç¨åå/交æ¢åºçå¶çº¦ã
504 äºå®ä¸ï¼å½ä¸è¿°è¿äºæ°å¼åå¾å¼å¸¸å°å¤§æ¶ï¼ç³»ç»æ§è½ä¹ä¼åå¾å¤§å½±åã
507 ä¸éè¦æä½ç³»ç»å¯¹å个æ件ä¹éè¿ä¹å¤§çæ¯æã大表ç¨å¤ä¸ª 1 GB çæ件å
508 å¨ï¼å æ¤æ件系ç»å¤§å°çéå¶æ¯ä¸éè¦çã
510 å¦æ缺ççå大å°å¢é¿å° 32K ï¼æ大çå表大å°åæ大åæ°è¿å¯ä»¥å¢å å°ååã
512 æä¸ä¸ªéå¶å°±æ¯ä¸è½å¯¹å¤§å°å¤äº2000å
513 èçåå建索å¼ã幸è¿å°æ¯è¿æ ·çç´¢å¼å¾å°ç¨å°ãéè¿å¯¹å¤å
514 èåçå容è¿è¡MD5åç¨è¿ç®ç»æè¿è¡å½æ°ç´¢å¼å¯å¯¹åçå¯ä¸æ§å¾å°ä¿è¯ï¼
515 并ä¸å¨ææ£ç´¢å许对åä¸çåè¯è¿è¡æç´¢ã
517 4.5) åå¨ä¸ä¸ªå¸åçææ¬æ件éçæ°æ®éè¦å¤å°ç£ç空é´ï¼
519 ä¸ä¸ª Postgres æ°æ®åºï¼å
520 å¨ä¸ä¸ªææ¬æ件ï¼æå ç¨ç空é´æå¤å¯è½éè¦ç¸å½äºè¿ä¸ªææ¬æ件èªèº«å¤§å°5åçç£
523 ä¾å¦ï¼å设æä¸ä¸ª 100,000 è¡çæ件ï¼æ¯è¡æä¸ä¸ªæ´æ°åä¸ä¸ªææ¬æè¿°ã
524 å设ææ¬ä¸²çå¹³åé¿åº¦ä¸º20åèãææ¬æ件å ç¨ 2.8 MBãå
525 æ¾è¿äºæ°æ®çPostgreSQLæ°æ®åºæä»¶å¤§çº¦æ¯ 6.4 MB:
526 28 åè: æ¯è¡ç头ï¼å¤§çº¦å¼ï¼
527 24 åè: ä¸ä¸ªæ´æ°åå段åä¸ä¸ªææ¬åå段
528 + 4 åè: 页é¢åæååç»çæé
529 ----------------------------------------
532 PostgreSQL æ°æ®é¡µç大å°æ¯ 8192 åè (8 KB)ï¼åï¼
535 ------------------- = 146 è¡/æ°æ®é¡µï¼åä¸åæ´ï¼
539 -------------------- = 685 æ°æ®é¡µï¼åä¸åæ´ï¼
542 685 æ°æ®é¡µ * 8192 åè/页 = 5,611,520 åèï¼5.6 MBï¼
544 ç´¢å¼ä¸éè¦è¿ä¹å¤çé¢å¤æ¶èï¼ä½ä¹ç¡®å®åæ¬è¢«ç´¢å¼çæ°æ®ï¼å æ
547 空å¼NULLåæ¾å¨ä½å¾ä¸ï¼å æ¤å ç¨å¾å°ç空é´ã
549 4.6) 为ä»ä¹æçæ¥è¯¢å¾æ¢ï¼ä¸ºä»ä¹è¿äºæ¥è¯¢æ²¡æå©ç¨ç´¢å¼ï¼
551 并éæ¯ä¸ªæ¥è¯¢é½ä¼èªå¨ä½¿ç¨ç´¢å¼ãåªæå¨è¡¨ç大å°è¶è¿ä¸ä¸ªæå°å¼ï¼å¹¶ä¸æ¥
552 询åªä¼éä¸è¡¨ä¸è¾å°æ¯ä¾çè®°å½æ¶æä¼éç¨ç´¢å¼ã
553 è¿æ¯å 为索å¼æ«æå¼èµ·çéå³ç£çååå¯è½æ¯ç´æ¥å°è¯»å表ï¼é¡ºåºæ«æï¼æ´æ¢ã
556 æ¯å¦ä½¿ç¨ç´¢å¼ï¼PostgreSQLå¿é¡»è·å¾æå³è¡¨çç»è®¡å¼ãè¿äºç»è®¡å¼å¯ä»¥ä½¿ç
557 ¨ VACUUM ANALYZEï¼æ ANALYZE è·å¾ã 使ç¨ç»è®¡å¼ï¼ä¼åå¨ç¥é表ä¸
558 æå¤å°è¡ï¼å°±è½å¤æ´å¥½å°å¤ææ¯å¦å©ç¨ç´¢å¼ã
559 ç»è®¡å¼å¯¹ç¡®å®ä¼åçè¿æ¥é¡ºåºåè¿æ¥æ¹æ³ä¹å¾æç¨ãå¨è¡¨çå容åçååæ¶ï¼åºå®æè¿
562 ç´¢å¼é常ä¸ç¨äº ORDER BY
563 ææ§è¡è¿æ¥ã对ä¸ä¸ªå¤§è¡¨çä¸æ¬¡é¡ºåºæ«æååä¸æ¬¡æåºé常æ¯ç´¢å¼æ«æè¦å¿«ãç¶
564 èï¼å¦æå° LIMIT å ORDER BY
565 ç»åå¨ä¸èµ·ä½¿ç¨çè¯ï¼é常å°ä¼ä½¿ç¨ç´¢å¼ï¼å 为è¿æ¶ä»è¿å表ä¸
568 å¦æä½ ç¡®ä¿¡PostgreSQLçä¼åå¨ä½¿ç¨é¡ºåºæ«ææ¯ä¸æ£ç¡®çï¼ä½ å¯ä»¥ä½¿ç¨SET
569 enable_seqscan TO 'off'æ令æ¥å³é顺åºæ«æï¼
570 ç¶åå次è¿è¡æ¥è¯¢ï¼ä½ å°±å¯ä»¥çåºä½¿ç¨ä¸ä¸ªç´¢å¼æ«ææ¯å¦ç¡®å®è¦å¿«ä¸äºã
572 å½ä½¿ç¨éé符æä½ï¼ä¾å¦ LIKE æ ~ æ¶ï¼ç´¢å¼åªè½å¨ç¹å®çæåµä¸ä½¿ç¨ï¼
573 * å符串çå¼å§é¨åå¿é¡»æ¯æ®éå符串ï¼ä¹å°±æ¯è¯´ï¼
574 + LIKE 模å¼ä¸è½ä»¥ % æ头ã
575 + ~ ï¼æ£å表达å¼ï¼æ¨¡å¼å¿é¡»ä»¥ ^ æ头ã
576 * å符串ä¸è½ä»¥å¹éå¤ä¸ªå符ç模å¼ç±»æ头ï¼ä¾å¦ [a-e]ã
577 * 大å°åæ å³çæ¥æ¾ï¼å¦ ILIKE å ~* çä¸ä½¿ç¨ç´¢å¼ï¼ä½å¯ä»¥ç¨ 4.8
579 * å¨å initdb æ¶å¿é¡»éç¨ç¼ºççæ¬å°è®¾ç½® C
580 localeï¼å 为系ç»ä¸å¯è½ç¥éå¨éC localeæåµæ¶ä¸ä¸ä¸ªæ大å符æ¯ä»ä¹ã
581 å¨è¿ç§æåµä¸ï¼ä½ å¯ä»¥å建ä¸ä¸ªç¹æ®çtext_pattern_opsç´¢å¼æ¥ç¨äºLIKE
585 ï¼é¤éè¦æ¥è¯¢çæ°æ®ç±»ååç´¢å¼çæ°æ®ç±»åç¸å¹éï¼å¦åç´¢å¼ç»å¸¸æ¯æªè¢«ç¨å°ï¼ç
586 ¹å«æ¯å¯¹int2,int8åæ°å¼åçç´¢å¼ã
588 4.7) æå¦ä½æè½çå°æ¥è¯¢ä¼åå¨æ¯ææ ·è¯ä¼°å¤çæçæ¥è¯¢ï¼
592 4.8) æææ ·åæ£å表达å¼æç´¢å大å°åæ å³çæ
593 £å表达å¼æ¥æ¾ï¼ææ ·å©ç¨ç´¢å¼è¿è¡å¤§å°åæ å³æ¥æ¾ï¼
595 æä½ç¬¦ ~ å¤çæ£å表达å¼å¹éï¼è ~* å¤ç大å°åæ å³çæ
596 £å表达å¼å¹éã大å°åæ å³ç LIKE åç§æ为 ILIKEã
598 大å°åæ å³ççå¼æ¯è¾é常ååï¼
601 WHERE lower(col) = 'abc';
603 è¿æ ·å°ä¸ä¼ä½¿ç¨æ åçç´¢å¼ãä½æ¯å¯ä»¥å建ä¸ä¸ªå¨è¿ç§æåµä¸ä½¿ç¨ç表达å¼ç
605 CREATE INDEX tabindex ON tab (lower(col));
607 å¦æä¸è¿°ç´¢å¼å¨å建æ¶å å¥UNIQUE约æï¼è½ç¶ç´¢å¼å段èªèº«å容å¯ä»¥å
608 å¨å¤§å°åä¸éçå容ï¼ä½å¦ææUNIQUE约æåï¼è¿äºå容ä¸è½ä»ä»æ¯å¤§å°åä¸åï¼å¦åä
609 ¼é æå²çªï¼ã为äºä¿è¯ä¸åçè¿ç§æåµï¼å¯ä»¥ä½¿ç¨CHECK约ææ¡ä»¶ææ¯è§¦åå¨å¨å½
612 4.9) å¨ä¸ä¸ªæ¥è¯¢éï¼æææ ·æ£æµä¸ä¸ªå段æ¯å¦ä¸º NULL ï¼æå¦ä½æè½åç¡®æåºèä¸è®ºæå
615 ç¨ IS NULL å IS NOT NULL æµè¯è¿ä¸ªå段ï¼å·ä½æ¹æ³å¦ä¸ï¼
620 为äºè½å¯¹å« NULLå段æåºï¼å¯å¨ ORDER BY æ¡ä»¶ä¸ä½¿ç¨ IS NULLå IS NOT
621 NULL 修饰符ï¼æ¡ä»¶ä¸ºç true å°æ¯æ¡ä»¶ä¸ºåfalse æå¨åé¢ï¼ä¸é¢çä¾å
622 å°±ä¼å°å« NULL çè®°å½æå¨ç»æçä¸é¢é¨åï¼
625 ORDER BY (col IS NOT NULL)
627 4.10) åç§å符类åä¹é´æä»ä¹ä¸åï¼
630 VARCHAR(n) varchar æå®äºæ大é¿åº¦ï¼åé¿å
631 符串ï¼ä¸è¶³å®ä¹é¿åº¦çé¨åä¸è¡¥é½
632 CHAR(n) bpchar å®é¿å符串ï¼å®éæ°æ®ä¸è¶³å®ä¹é¿åº¦æ¶ï¼ä»¥ç©ºæ ¼è¡¥é½
633 TEXT text 没æç¹å«çä¸ééå¶ï¼ä»åè¡çæ大é¿åº¦éå¶ï¼
634 BYTEA bytea åé¿åèåºåï¼ä½¿ç¨NULLå符ä¹æ¯å许çï¼
637 å¨ç³»ç»è¡¨åå¨ä¸äºé误信æ¯éä½ å°çå°åé¨å称ã
639 ä¸é¢æåçååç§ç±»åæ¯"varlena"ï¼åé¿ï¼ç±»åï¼ä¹å°±æ¯è¯´ï¼å¼å¤´çå个å
640 èæ¯é¿åº¦ï¼åé¢ææ¯æ°æ®ï¼ã äºæ¯å®éå ç¨ç空é´æ¯å£°æç大å°è¦å¤ä¸äºã
641 ç¶èè¿äºç±»åå¦å®ä¹å¾é¿æ¶é½å¯ä»¥è¢«å缩åå¨ï¼å æ
642 ¤ç£ç空é´ä¹å¯è½æ¯é¢æ³çè¦å°ã
644 VARCHAR(n) å¨åå¨éå¶äºæ大é¿åº¦çåé¿å符串æ¯æ好çã TEXT éç¨äºå
645 å¨æ大å¯è¾¾ 1Gå·¦å³ä½æªå®ä¹éå¶é¿åº¦çå符串ã
647 CHAR(n) æéåäºåå¨é¿åº¦ç¸åçå符串ã CHAR(n)ä¼æ ¹æ®æç»å®çå
648 段é¿åº¦ä»¥ç©ºæ ¼è¡¥è¶³ï¼ä¸è¶³çå段å容ï¼ï¼ è VARCHAR(n) åªå
649 å¨æç»å®çæ°æ®å容ã BYTEA ç¨äºåå¨äºè¿å¶æ°æ®ï¼å°¤å¶æ¯åå« NULL å
650 èçå¼ãè¿äºç±»åå·æå·®ä¸å¤çæ§è½ã
652 4.11.1) æææ ·å建ä¸ä¸ªåºåå·ææ¯èªå¨éå¢çå段ï¼
654 PostgreSQL æ¯æ SERIAL æ°æ®ç±»åãï¼å
655 段å®ä¹ä¸ºSERIALåï¼å°èªå¨å建ä¸ä¸ªåºåçæå¨ï¼ä¾å¦ï¼
656 CREATE TABLE person (
661 ä¼èªå¨è½¬æ¢ä¸ºä»¥ä¸SQLè¯å¥ï¼
662 CREATE SEQUENCE person_id_seq;
663 CREATE TABLE person (
664 id INT4 NOT NULL DEFAULT nextval('person_id_seq'),
668 åè create_sequence æå页è·åå³äºåºåçæå¨çæ´å¤ä¿¡æ¯ã
670 4.11.2) æå¦ä½è·å¾ä¸ä¸ªæå¥çåºåå·çå¼ï¼
672 ä¸ç§æ¹æ³æ¯å¨æå¥ä¹ååç¨å½æ° nextval() ä»åºå对象éæ£ç´¢åºä¸ä¸ä¸ª SERIAL
673 å¼ï¼ç¶ååç¨æ¤å¼ç²¾ç¡®å°æå¥ãä½¿ç¨ 4.11.1 éçä¾è¡¨ï¼å¯ç¨ä¼ªç è¿æ ·æè¿°ï¼
674 new_id = execute("SELECT nextval('person_id_seq')");
675 execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
677 è¿æ ·è¿è½å¨å¶ä»æ¥è¯¢ä¸ä½¿ç¨åæ¾å¨ new_id éçæ°å¼ï¼ä¾å¦ï¼ä½ä¸ºåç§ person
678 表çå¤é®ï¼ã 注æèªå¨å建ç SEQUENCE 对象çå称å°ä¼æ¯
679 <table>_<serialcolumn>_seqï¼ è¿é table å serialcolumn
680 åå«æ¯ä½ ç表çå称åä½ ç SERIAL å段çå称ã
682 类似çï¼å¨ SERIAL 对象缺çæå¥åä½ å¯ä»¥ç¨å½æ° currval() æ£ç´¢åèµå¼ç
684 execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
685 new_id = execute("SELECT currval('person_id_seq')");
687 4.11.3) åæ¶ä½¿ç¨ currval() ä¼å¯¼è´åå¶ä»ç¨æ·çå²çªæåµåï¼
689 ä¸ä¼ãcurrval() è¿åçæ¯ä½ æ¬æ¬¡ä¼è¯è¿ç¨æèµçå¼èä¸æ¯ææç¨æ·çå½åå¼ã
691 4.11.4) 为ä»ä¹ä¸å¨äºå¡å¼å¸¸ä¸æ¢åéç¨åºåå·å¢ï¼ä¸ºä»ä¹å¨åºåå·å段çåå¼ä¸åå¨é´æ
694 为äºæé«å¹¶åæ§ï¼åºåå·å¨éè¦çæ¶åèµäºæ
695 £å¨è¿è¡çäºå¡ï¼å¹¶ä¸å¨äºå¡ç»æä¹åä¸è¿è¡éå®ï¼ è¿å°±ä¼å¯¼è´å¼å¸¸ä¸
696 æ¢çäºå¡åï¼åºåå·ä¼åºç°é´éã
698 4.12) ä»ä¹æ¯ OID ï¼ä»ä¹æ¯ CTID ï¼
701 éå建çæ¯ä¸è¡è®°å½é½ä¼è·å¾ä¸ä¸ªå¯ä¸çOIDï¼é¤éå¨å建表æ¶ä½¿ç¨WITHOUT
702 OIDSé项ã OIDå建æ¶ä¼èªå¨çæä¸ä¸ª4åèçæ´æ°ï¼ææ OID
703 å¨ç¸åºPostgreSQLæå¡å¨ä¸åæ¯å¯ä¸çã ç¶èï¼å®å¨è¶è¿40亿æ¶å°æº¢åºï¼ OIDæ
704 ¤åä¼åºç°éå¤ãPostgreSQL å¨å®çåé¨ç³»ç»è¡¨éä½¿ç¨ OID å¨è¡¨ä¹é´å»ºç«èç³»ã
706 å¨ç¨æ·çæ°æ®è¡¨ä¸ï¼æ好æ¯ä½¿ç¨SERIAlæ¥ä»£æ¿OID
707 å 为SERIALåªè¦ä¿è¯å¨å个表ä¸
708 çæ°å¼æ¯å¯ä¸çå°±å¯ä»¥äºï¼è¿æ ·å®æº¢åºçå¯è½æ§å°±é常å°äºï¼
709 SERIAL8å¯ç¨æ¥ä¿å8åèçåºåæ°å¼ã
711 CTID ç¨äºæ è¯å¸¦çæ°æ®åï¼å°åï¼åï¼ååï¼å移çç¹å®çç©çè¡ã CTID
712 å¨è®°å½è¢«æ´æ¹æéè½½ååçæ¹åãç´¢å¼æ°æ®ä½¿ç¨å®ä»¬æåç©çè¡ã
714 4.13) 为ä»ä¹ææ¶å°é误信æ¯âERROR: Memory exhausted in AllocSetAlloc()âï¼
716 è¿å¾å¯è½æ¯ç³»ç»çèæååç¨åäºï¼æèåæ ¸å¯¹æäºèµæºæè¾ä½çéå¶å¼ãå¨å¯å¨
717 postmaster ä¹åè¯è¯ä¸é¢çå½ä»¤ï¼
722 shellï¼ä¸é¢å½ä»¤åªæä¸æ¡è½æåï¼ä½æ¯å®å°æä½ çè¿ç¨æ°æ®æ®µéå¶è®¾å¾æ¯è¾é«ï¼
723 å èä¹è®¸è½è®©æ¥è¯¢å®æãè¿æ¡å½ä»¤åºç¨äºå½åè¿ç¨ï¼ä»¥åææå¨è¿æ¡å½ä»¤è¿è¡ååå
725 å¦æä½ æ¯å¨è¿è¡SQL客æ·ç«¯æ¶å 为åå°è¿åäºå¤ªå¤çæ°æ®èåºç°é®é¢ï¼è¯·å¨è¿è¡
726 客æ·ç«¯ä¹åæ§è¡ä¸è¿°å½ä»¤ã
728 4.14) æå¦ä½æè½ç¥éæè¿è¡çPostgreSQLççæ¬ï¼
730 ä» psql éï¼è¾å¥ SELECT version();æ令ã
732 4.15) æå¦ä½å建ä¸ä¸ªç¼ºçå¼æ¯å½åæ¶é´çå段ï¼
734 ä½¿ç¨ CURRENT_TIMESTAMPï¼
735 CREATE TABLE test (x int, modtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
737 4.16) æææ ·è¿è¡ outer join ï¼å¤è¿æ¥ï¼ï¼
739 PostgreSQL éç¨æ åç SQL è¯æ³æ¯æå¤è¿æ¥ãè¿éæ¯ä¸¤ä¸ªä¾åï¼
741 FROM t1 LEFT OUTER JOIN t2 ON (t1.col = t2.col);
745 FROM t1 LEFT OUTER JOIN t2 USING (col);
747 è¿ä¸¤ä¸ªçä»·çæ¥è¯¢å¨ t1.col å t2.col ä¸åè¿æ¥ï¼å¹¶ä¸è¿å t1 ä¸
748 æææªè¿æ¥çè¡ï¼é£äºå¨ t2 ä¸æ²¡æå¹éçè¡ï¼ã å³[å¤]è¿æ¥ï¼RIGHT OUTER
749 JOINï¼å°è¿å t2 ä¸æªè¿æ¥çè¡ã å®å¨å¤è¿æ¥ï¼FULL OUTER JOINï¼å°è¿å t1 å t2
750 ä¸æªè¿æ¥çè¡ã å³é®å OUTER å¨å·¦[å¤]è¿æ¥ãå³[å¤]è¿æ¥åå®å¨[å¤]è¿æ¥ä¸
751 æ¯å¯éçï¼æ®éè¿æ¥è¢«ç§°ä¸ºåè¿æ¥ï¼INNER JOINï¼ã
753 4.17) å¦ä½ä½¿ç¨æ¶åå¤ä¸ªæ°æ®åºçæ¥è¯¢ï¼
755 没æåæ³æ¥è¯¢å½åæ°æ®åºä¹å¤çæ°æ®åºã
756 å 为PostgreSQLè¦å è½½ä¸æ°æ®åºç¸å³çç³»ç»ç®å½ï¼ç³»ç»è¡¨ï¼ï¼å æ
757 ¤è·¨æ°æ®åºçæ¥è¯¢å¦ä½æ§è¡æ¯ä¸å®çã
759 éå å¢å¼æ¨¡åcontrib/dblinkå许éç¨å½æ°è°ç¨å®ç°è·¨åºæ¥è¯¢ãå½ç¶ç¨æ·ä¹å¯ä»¥
760 åæ¶è¿æ¥å°ä¸åçæ°æ®åºæ§è¡æ¥è¯¢ç¶åå¨å®¢æ·ç«¯å并ç»æã
762 4.18) å¦ä½è®©å½æ°è¿åå¤è¡æå¤åæ°æ®ï¼
764 å¨å½æ°ä¸è¿åæ°æ®è®°å½éçåè½æ¯å¾å®¹æ使ç¨çï¼è¯¦æåè§ï¼
765 http://techdocs.postgresql.org/guides/SetReturningFunctions
767 4.19) 为ä»ä¹æå¨ä½¿ç¨PL/PgSQLå½æ°åå临æ¶è¡¨æ¶ä¼æ¶å°é误信æ¯ârelation with
768 OID ##### does not existâï¼
770 PL/PgSQLä¼ç¼åå½æ°çèæ¬å容ï¼ç±æ¤å¸¦æ¥çä¸ä¸ªä¸å¥½çå¯ä½ç¨æ¯è¥ä¸ä¸ª
772 å½æ°è®¿é®äºä¸ä¸ªä¸´æ¶è¡¨ï¼ç¶å该表被å é¤å¹¶é建äºï¼åå次è°ç¨è¯¥å½æ°å
773 °å¤±è´¥ï¼ å 为ç¼åçå½æ°å容ä»ç¶æåæ§ç临æ¶è¡¨ã解å³çæ¹æ³æ¯å¨ PL/PgSQL
775 对临æ¶è¡¨è¿è¡è®¿é®ãè¿æ ·ä¼ä¿è¯æ¥è¯¢å¨æ§è¡åæ»ä¼è¢«éæ°è§£æã
777 4.20) ç®åæåªäºæ°æ®å¤å¶æ¹æ¡å¯ç¨ï¼
779 âå¤å¶âåªæ¯ä¸ä¸ªæ¯è¯ï¼æ好å ç§å¤å¶ææ¯å¯ç¨ï¼æ¯ç§é½æä¼ç¹å缺ç¹ï¼
781 主/ä»å¤å¶æ¹å¼æ¯å许ä¸ä¸ªä¸»æå¡å¨æ¥å读/åçç³è¯·ï¼èå¤ä¸ªä»æå¡å¨åªè½æ¥åè
782 ¯»/SELECTæ¥è¯¢çç³è¯·ï¼ ç®åææµè¡ä¸åè´¹ç主/ä»PostgreSQLå¤å¶æ¹æ¡æ¯
785 å¤ä¸ªä¸»æå¡å¨çå¤å¶æ¹å¼å许å°è¯»/åçç³è¯·åéç»å¤å°ç计ç®æºï¼è¿ç§æ¹å¼ç±äºé
786 è¦å¨å¤å°æå¡å¨ä¹é´åæ¥æ°æ®åå¨
787 å¯è½ä¼å¸¦æ¥è¾ä¸¥éçæ§è½æ失ï¼Pgclusteræ¯ç®åè¿ç§æ¹æ¡ ä¸
788 æ好çï¼èä¸è¿å¯ä»¥åè´¹ä¸è½½ã
790 ä¹æä¸äºåä¸éä»è´¹ååºäºç¡¬ä»¶çæ°æ®å¤å¶æ¹æ¡ï¼æ¯æä¸è¿°åç§å¤å¶æ¨¡åã
792 4.21) 为ä½æ¥è¯¢ç»ææ¾ç¤ºç表åæååä¸æçæ¥è¯¢è¯å¥ä¸çä¸åï¼ä¸ºä½å¤§åç¶æä¸è½ä¿çï¼
794 æ常è§çåå æ¯å¨å建表æ¶å¯¹è¡¨åææ¯åå使ç¨äºåå¼å·ââï¼å½ä½¿ç¨äºåå¼å·åï¼è¡
795 ¨åæååï¼ç§°ä¸ºæ è¯ç¬¦ï¼åå¨æ¶æ¯åºå 大å°åçï¼
796 è¿æè°çä½ å¨æ¥è¯¢æ¶è¡¨åæååä¹åºä½¿ç¨åå¼å·ï¼ä¸äºå·¥å·è½¯ä»¶ï¼åpgAdminä¼å¨
797 ååºå建表çæ令æ¶èªå¨å°å¨æ¯ä¸ªæ è¯ç¬¦ä¸å åå¼å·ã å æ
798 ¤ï¼ä¸ºäºæ è¯ç¬¦çç»ä¸ï¼ä½ åºè¯¥ï¼
799 * å¨å建表æ¶é¿åå°æ è¯ç¬¦ä½¿ç¨åå¼å·å¼èµ·æ¥ã
800 * å¨æ è¯ç¬¦ä¸åªä½¿ç¨å°ååæ¯ã
801 * ï¼ä¸ºäºä¸å·²åå¨çæ è¯ç¬¦ç¸åï¼å¨æ¥è¯¢ä¸ä½¿ç¨åå¼å·å°æ è¯ç¬¦å¼èµ·æ¥ã