目 录
PostgreSQL 学习手册(数据表) .............................................................................................................................................. 4
一、表的定义: ............................................................................................................................................................... 4
PostgreSQL 学习手册(模式 Schema) ..................................................................................................................................... 9
PostgreSQL 学习手册(表的继承和分区) ............................................................................................................................ 10
一、表的继承: ............................................................................................................................................................. 10
PostgreSQL 学习手册(常用数据类型) ................................................................................................................................ 16
一、数值类型: ............................................................................................................................................................. 16
六、数组: ..................................................................................................................................................................... 22
PostgreSQL 学习手册(函数和操作符<一>) ........................................................................................................................ 25
一、逻辑操作符: ......................................................................................................................................................... 25
四、字符串函数和操作符: ......................................................................................................................................... 27
五、位串函数和操作符: ............................................................................................................................................. 29
PostgreSQL 学习手册(函数和操作符<二>) ........................................................................................................................ 30
六、模式匹配: ............................................................................................................................................................. 30
八、时间/日期函数和操作符: .................................................................................................................................... 33
PostgreSQL 学习手册(函数和操作符<三>) ........................................................................................................................ 35
九、序列操作函数: ..................................................................................................................................................... 35
十二、系统信息函数: ................................................................................................................................................. 38
PostgreSQL 学习手册(索引) ................................................................................................................................................ 42
一、索引的类型: ......................................................................................................................................................... 42
四、唯一索引: ............................................................................................................................................................. 43
PostgreSQL 学习手册(事物隔离) ...................................................................................................................................... 45
PostgreSQL 学习手册(性能提升技巧) ................................................................................................................................ 46
一、使用 EXPLAIN: ...................................................................................................................................................... 46
PostgreSQL 学习手册(服务器配置) .................................................................................................................................. 50
一、服务器进程的启动和关闭: ................................................................................................................................. 50
PostgreSQL 学习手册(角色和权限) .................................................................................................................................... 52
PostgreSQL 学习手册(数据库管理) ................................................................................................................................ 54
一、概述: ..................................................................................................................................................................... 54
PostgreSQL 学习手册(数据库维护) .................................................................................................................................. 56
一、恢复磁盘空间: ..................................................................................................................................................... 56
二、更新规划器统计: ................................................................................................................................................. 57
四、定期重建索引: ..................................................................................................................................................... 59
PostgreSQL 学习手册(系统表) ............................................................................................................................................ 61
一、pg_class: .................................................................................................................................................................. 61
三、pg_attrdef: ............................................................................................................................................................... 63
四、pg_authid: ................................................................................................................................................................ 64
五、pg_auth_members: ................................................................................................................................................. 64
七、pg_tablespace: ......................................................................................................................................................... 65
十、pg_index: ................................................................................................................................................................. 67
PostgreSQL 学习手册(系统视图) ........................................................................................................................................ 68
一、pg_tables: ................................................................................................................................................................ 68
二、pg_indexes: .............................................................................................................................................................. 68
三、pg_views: ................................................................................................................................................................. 68
四、pg_user: ................................................................................................................................................................... 69
五、pg_roles: .................................................................................................................................................................. 69
六、pg_rules: .................................................................................................................................................................. 69
七、pg_settings: ............................................................................................................................................................. 70
PostgreSQL 学习手册(客户端命令<一>) ............................................................................................................................ 70
零、口令文件: ............................................................................................................................................................. 70
PostgreSQL 学习手册(客户端命令<二>) ............................................................................................................................ 75
七、pg_dump:................................................................................................................................................................. 75
八、pg_restore: .............................................................................................................................................................. 77
PostgreSQL 学习手册(SQL 语言函数) ................................................................................................................................. 83
一、基本概念: ............................................................................................................................................................. 83
PostgreSQL 学习手册(PL/pgSQL 过程语言) ....................................................................................................................... 86
一、概述: ..................................................................................................................................................................... 86
PostgreSQL 学习手册(数据表)
一、表的定义:
对于任何一种关系型数据库而言,表都是数据存储的最核心、最基础的对象单元。现在就让我们从这里起步吧。
1. 创建表:
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
2. 删除表:
DROP TABLE products;
3. 创建带有缺省值的表:
CREATE TABLE products (
product_no integer,
name text,
price numeric DEFAULT 9.99 --DEFAULT
是关键字,其后的数值
9.99
是字段
price
的默认值。
);
CREATE TABLE products (
product_no SERIAL, --SERIAL
类型的字段表示该字段为自增字段,完全等同于
Oracle
中的
Sequence。
name text,
price numeric DEFAULT 9.99
);
输出为:
NOTICE: CREATE TABLE will create implicit sequence "products_product_no_seq" for serial column
"products.product_no"
4. 约束:
检查约束是表中最为常见的约束类型,它允许你声明在某个字段里的数值必须满足一个布尔表达式。不仅如此,我们也可以声明表
级别的检查约束。
CREATE TABLE products (
product_no integer,
name text,
--price
字段的值必须大于
0
,否则在插入或修改该字段值是,将引发违规错误。还需要说明的是,该检查约束
--
是匿名约束,即在表定义时没有显示命名该约束,这样
PostgreSQL
将会根据当前的表名、字段名和约束类型,
--
为该约束自动命名,如:
products_price_check
。
price numeric CHECK (price > 0)
);
CREATE TABLE products (
product_no integer,
name text,
--
该字段的检查约束被显示命名为
positive_price
。这样做好处在于今后维护该约束时,可以根据该名进行直接操作。
price numeric CONSTRAINT positive_price CHECK (price > 0)
);
下面的约束是非空约束,即约束的字段不能插入空值,或者是将已有数据更新为空值。
CREATE TABLE products (
product_no integer NOT NULL,
name text NOT NULL,
price numeric
);
如果一个字段中存在多个约束,在定义时可以不用考虑约束的声明顺序。
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 UNIQUE,
name text,
price numeric
);
CREATE TABLE products (
product_no integer,
name text,
price numeric,
UNIQUE (product_no)
);
为表中的多个字段定义联合唯一性。
CREATE TABLE example (
a integer,
b integer,
c integer,
UNIQUE (a, c)
);
为唯一性约束命名。
CREATE TABLE products (
product_no integer CONSTRAINT must_be_different UNIQUE,
name text,
price numeric
);
在插入数据时,空值(NULL)之间被视为不相等的数据,因此对于某一唯一性字段,可以多次插入空值。然而需要注意的是,这一
规则并不是被所有数据库都遵守,因此在进行数据库移植时可能会造成一定的麻烦。
5. 主键和外键:
从技术上来讲,主键约束只是唯一约束和非空约束的组合。
CREATE TABLE products (
product_no integer PRIMARY KEY, --
字段
product_no
被定义为该表的唯一主键。
name text,
price numeric
- 1
- 2
- 3
前往页