PostgreSQL可以直接存储二进制的文件。例如图片、文档,视频等。
其他数据库系统也是可以存数组的,不过还是最终以字符串的形式存的,取出和读取都是用程序来序列化。假如不用字符串存,那就得多准备一张表,例如,一篇文章要记录什么人收藏过。就得多一张表,每次判断用户是否收藏过,就得查那张表,而数据以冗余的方式存在数据中,就是把user_id存进去一个字段,这样就大大方便了。PostgreSQL默认就支持数据的存取,还支持对数据的各种操作,比如查找等。
create_table :books do |t|
t.string 'title'
t.string 'tags', array: true
t.integer 'ratings', array: true
end
add_index :books, :tags, using: 'gin'
add_index :books, :ratings, using: 'gin'
# app/models/book.rb
class Book < ActiveRecord::Base
end
# Usage
Book.create title: "Brave New World",
tags: ["fantasy", "fiction"],
ratings: [4, 5]
## Books for a single tag
Book.where("tags @> ARRAY[?]::varchar[]", ["fantasy", "fiction"])
## Books with 3 or more ratings
Book.where("array_length(ratings, 1) >= 3")
PostgreSQL还支持对array的各种操作,给了详细的解释。
比如,把数组进行类似join的操作。
Book.select("title, array_to_string(tags, '_')")
SELECT title, array_to_string(tags, '_') FROM "books";
title | array_to_string
-----------------+-----------------
Brave New World | fantasy_fiction
(1 row)
Hstore是PostgreSQL的一个扩展,它能够存放键值对,比如,json,hash等半结构化数据。一般的数据库系统是没有这种功能,而这种需求是很常见的,所以说,PostgreSQL是最具Nosql特性的。只要前端通过js提交一些hash或json,或者通过form提交一些数据,就能直接以json等形式存到数据库中。例如,一个用户有1个,0个,或多个联系人,如果以关系型数据库来存的话,只能多建立一张表来存,然后用has_many,belongs_to来处理。而Hstore就是以字段的形式来存,这就很方便了。
像array一样,Hstore也是支持很多操作的,官方文档hstore给了详细的描述。
rails365_dev=# SELECT "profiles".settings -> 'color' FROM "profiles"
;
?column?
----------
yellow
blue
(2 rows)
?column?
----------
t
t
(2 rows)
rails365_dev=# SELECT hstore_to_json("profiles".settings) FROM "profiles"
;
hstore_to_json
---------------------------------------------------------------
{"color": "yellow", "resolution": "1280x1024"}
{"color": "blue", "resolution": "[\"800x600\", \"750x670\"]"}
(2 rows)
rails365_dev=# SELECT "profiles".settings -> 'color' FROM "profiles"
where settings->'color' = 'yellow';
?column?
----------
yellow
(1 row)
更多详细只要查看官文文档就好了。
关于Hstore有一个gem是,这个gem提供了很多关于Hstore的查询方法。
using-postgres-hstore-rails4这篇文章介绍了Hstore的用法。
完结。