從PostgreSQL使用手冊整理的一些筆記(1)

以下內容節錄PostgreSQL使用手冊


中文參考
原文參考

1. Keyword & Identifier


SELECT, UPDATE, INSERT都屬於keyword的範圍, identifier則用來識別表格名稱、欄位名稱或其他資料庫物件,且在無引號’’下都不分大小寫,因此不看整個命令的話基本上難以區分是keyword還是identifier,但當情況為受引號限制的identifier,例如:UPDATE "my_table" set “a” = 5;,引號內就一定是identifer或"SELECT"在這就會是identifier,不會是keyword,而有部分的寫法常見會將keyword以大寫表示,identifier使用小寫。


SQL Keyword


2. 特殊字元



$
錢字引號常數或後接數字表示函數宣告(?)或預備指令的參數編號(?)
()
優先運算
[]
組成陣列的各個元素
,
分隔列表的單元
;
表示SQL指令的結束,不能出現在指令的其他位置,除非在引號內。
:
取得陣列小項(?)
*
表示表格中所有的欄位
.
區分結構、表格及欄位名稱
--
註解
/**/
註解
::
PostgreSQL-style typecast

3. 第五章部分節錄 (Data Definition)


預設值的指定,如果沒有指定就是NULL

指定方式如下:

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

其中的CHECK表示限制條件,price的數值必須是正數,以括號內擺條件式(price > 0)設定。而前方的CONSTRAINT則讓這個限制條件有另一個名稱,在有ERROR的時候你可以知道是哪一條限制出問題了。

除此之外,限制條件也可以做兩次的CHECK,例如:
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),
  CHECK (price > discounted_price)
CONSTRAINT valid_discount CHECK (price > discounted_price)
);
限制欄位無空值得範例如下,只能使勇在欄位限制上,等同於CHECK (IS NOT NULL):

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

反言之,

CREATE TABLE products (
  product_no integer NULL,
  name text NULL,
  price numeric NULL
);

UNIQUE的使用可以確保某欄位資料 is unique among all the rows in the table.
限制某欄位

CREATE TABLE products (
  product_no integer CONSTRAINT must_be_different UNIQUE,
  name text,
  price numeric
);

限制表格

CREATE TABLE products (
  product_no integer,
  name text,
  price numeric,
UNIQUE (product_no)
);

(a, c)這個組合 is unique
CREATE TABLE example (
  a integer,
  b integer,
  c integer,
UNIQUE (a, c)
);

UNIQUE跟DISTINCT的相關討論

Primary Keys用法則同使用UNIQUE+NOT NULL,參考下例:

CREATE TABLE products (
  product_no integer UNIQUE NOT NULL,
  name text,
  price numeric
);

CREATE TABLE products (
  product_no integer PRIMARY KEY,
  name text,
  price numeric
);
可獲得相同Table。Primary keys也可以使用在多個欄位上,但一個表格只能有一個Primary key:

CREATE TABLE example (
  a integer,
  b integer,
  c integer,
PRIMARY KEY (a, c)
);

接下來紀錄Foreign Keys,Foreign keys用來指定某欄位必須存在另一張表格中,以為故兩表間的關聯,例如:

CREATE TABLE products (
  product_no integer PRIMARY KEY,
  name text,
  price numeric
);

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

若product_no沒有出現在produsts (referencing table) 則order (referenced table) 這張表會無法建立。不多寫一次product_no,可簡化為:

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

另外,Foreign  keys也可以參考一組欄位 (數量需相等):

CREATE TABLE t1 (
a integer PRIMARY KEY,
b integer,
c integer,
FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)
);

三張表也不是問題,其中RESTRICT防止參考資料被刪除,CASCADE則是當參考資料被刪除時,同步刪除引用的資料列。

CREATE TABLE products (
  product_no integer PRIMARY KEY,
  name text,
  price numeric
);
CREATE TABLE orders (
  order_id integer PRIMARY KEY,
  shipping_address text,
  ...
);
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)
);
其餘還有SET  NULL, SET DEFAULT,引用的資料會被設定為空值及預設值。而ON DELETE則有ON  UPDATE當引用資料被更新時,參考資料可以做的處理方式,例如:CASCADE就會同步更新引用資料。


4. Modifying Tables



Add a Column


ALTER TABLE products ADD COLUMN description text CHECK (description <> '');


Removing a Column


ALTER TABLE products DROP COLUMN description CASCADE;
(同時刪除參考欄位)


Adding a Constraint


ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;


Removing a Constraint
限制條件在宣告時有命名的話就用命名的名稱。如果限制條件名稱是$2這種,溫馨提醒用雙引號刮住才能正確被識別是名稱。


ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;


Changeing a Column’s  Default Value
之後新增的資料才會套用此預設值。
ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;


Changing a Cloumn’s Data Type


ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);


Renaming a Column


ALTER TABLE products RENAME COLUMN product_no TO product_number;


Renaming a Table


ALTER TABLE products RENAME TO items;


留言