Postgresql_Leraning_Note_Till_20140509_Novice_To_Profes_学习笔记

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.select id from ers order by id asc limit 3 offset 1;

select cast((last_login/id) as int) as ratio from ers;

2.SET datestyle TO 'European, SQL';

--指定数据显示的样式为 SQL 样式且使用欧洲的样式转换月份和日期(日期在月份之前)

可以在 postmaster 主服务进程启动前设置 PGDATESTYLE 环境变量。在 Linux 设置这个环境变量,我们可以这样做更好的改变默认日期处理:

PGDATESTYLE="European, SQL"

export PGDATESTYLE

为了修改显示格式,你也需要设置 datestyle,但是要设置成以下四个值之一:

ISO,设置成 ISO-8601 标准,使用-作为分隔符,格式类似于 1997-02-01

SQL,用于传统样式,格式类似于 02/01/1997

Postgres,用于默认的 PostgreSQL 样式,格式类似于 Sat Feb 01

Geman,用于德国样式,格式类似于 01.02.1997

注:在最新的发布版本,PostgreSQL 默认的日期和时间戳样式使用 SQL 样式。

对于 NN/NN/NNNN 形式的输入,PostgreSQL 默认认为月份在日期之前(美国样式)

SET datestyle TO 'US, ISO';

show datestyle;

select date_part('month',cast('2014-04-01' as date)) as "Month";

我们可以提取日期和时间戳的以下部分:

Year(年),Month(月),Day(日),Hour(小时),Minute(分钟),Second(秒钟)

select now(),current_timestamp,current_date;

now | now | date

------------------------------+------------------------------+------------

2014-04-24 06:05:11.62848-04 | 2014-04-24 06:05:11.62848-04 | 2014-04-24

3.SQL92/99 语法,或者 ANSI 语法:

select a.id,,c.addr from core.test a

join core.test1 b on (a.id = b.id)

join core.test2 c on (a.id = c.id)

where c.addr = 'ChengDu';

4.pg_dump kian.gao_0421 -U propus -sv -n core > kian.gao_0421_schema_bak.sql

\i create_tables-bpsimple.sql

INSERT INTO customer VALUES(17, 'Mr', 'Shaun', 'O\'Rourke','32 Sheepy Lane', 'Milltown', 'MT9 8NQ', ' 746 3956');

5.\copy test from '/home/kian.gao/test/test_copy_cmd.txt' using delimiters ' ';

6.select a.id,,b.addr from test a left outer join tel b on (a.id = b.id) where b.addr = '123';

select a.id,,b.addr from test a left outer join tel b on (a.id = b.id and b.addr = '123');

7.表 8-1 指定布尔值(bool)的方法

翻译为 true: '1','yes','y','true','t'

翻译为 false:'0','no','n','false','f'

注:当 PostgreSQL 显示布尔类型列的内容是,它只会显示t,f,和空格。

8.select current_date - 1;

select current_date - integer '1';

select current_date - interval '1 month';

select current_date - interval '1 months'; -- 用 month months 都能接受

9.create table empworkday(id integer,workday bool[]); --PostgreSQL 语法

insert into empworkday values(1,'{0,1,1,1,1,1,0}');

select workday[3] from empworkday;

create table empworkday(id integer,workday bool array[7]); --SQL99 语法:必须指出元素的个数

10.SELECT sell_price, sell_price::int AS "Guide Price" FROM item;

SELECT sell_price, cast(sell_price as integer) AS "Guide Price" FROM item;

select strpos('Kian','a'); --3 /instr

select substr('KIan',2,2); --Ia

CURRENT_DATE

CURRENT_TIME

CURRENT_TIMESTAMP

CURRENT_USER

每当我们插入数据,PostgreSQL 使用一个随意的数字和一个行数作为响应,这个数字是一个内部参考数据,它是一个 PostgreSQL 为每一行保存的对象 ID,通常为隐藏的列,名为 oid。

create table customer(id integer,name varchar(32)) with (OIDS=TRUE);

SELECT oid, fname, lname FROM customer;

11.CREATE TABLE ttconst (

mykey1 int,

mykey2 int,

myforeignkey int,

mystring varchar(15),

CONSTRAINT cs1 CHECK (mystring <> ''),

CONSTRAINT cs2 PRIMARY KEY(mykey1, mykey2),

CONSTRAINT cs_fk FOREIGN KEY (myforeignkey)

References customer(id) INITIALLY DEFERRED);

PostgreSQL 会在对数据库进行任何变动前进行外键约束检查。但如果你使用了事物(我们将在下一章遇到)和 INITI ALLY DEFERRED,PostgreSQL 允许违反外键约束,允许在事务中违反外键约束,并且这个违例将在事务结束前被修正。事实上,发生的事情是 PostgreSQL 挂起事务检查,直到将要完成当前事务。

N UPDATE SET NULL ON DELETE CASCADE

相关文档
最新文档