数组类型的用途是什么? [英] what's the utility of array type?

查看:84
本文介绍了数组类型的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Postgresql的新手,但是我对mysql有很好的经验。我在阅读文档时发现Postgresql具有数组类型。我很困惑,因为我无法理解在rdbms中这种类型在哪种情况下有用。为什么我必须选择这种类型而不是使用经典的一对多关系?

I'm totally newbie with postgresql but I have a good experience with mysql. I was reading the documentation and I've discovered that postgresql has an array type. I'm quite confused since I can't understand in which context this type can be useful within a rdbms. Why would I have to choose this type instead of using a classical one to many relationship?

预先感谢。

推荐答案

我已经使用它们使使用树(例如注释线程)更容易。您可以存储从树的根到数组中单个节点的路径,数组中的每个数字都是该节点的分支编号。然后,您可以执行以下操作:

I've used them to make working with trees (such as comment threads) easier. You can store the path from the tree's root to a single node in an array, each number in the array is the branch number for that node. Then, you can do things like this:

SELECT id, content
FROM nodes
WHERE tree = X
ORDER BY path -- The array is here.

PostgreSQL将以自然方式逐元素比较数组,因此 ORDER BY路径将以合理的线性显示顺序转储树;然后,您检查路径的长度以找出节点的深度,从而使您可以缩进以获得正确的渲染。

PostgreSQL will compare arrays element by element in the natural fashion so ORDER BY path will dump the tree in a sensible linear display order; then, you check the length of path to figure out a node's depth and that gives you the indentation to get the rendering right.

以上方法可让您一次访问数据即可将您从数据库转到呈现的页面。

The above approach gets you from the database to the rendered page with one pass through the data.

PostgreSQL也具有几何类型,简单的键/值类型,并支持其他复合类型

PostgreSQL also has geometric types, simple key/value types, and supports the construction of various other composite types.

通常最好使用传统的关联表,但是在其中添加更多工具没有什么错您的工具箱。

Usually it is better to use traditional association tables but there's nothing wrong with having more tools in your toolbox.

这篇关于数组类型的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆