数组类型的用途是什么?

编程入门 行业动态 更新时间:2024-10-25 16:22:07
本文介绍了数组类型的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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.

更多推荐

数组类型的用途是什么?

本文发布于:2023-11-25 17:26:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   用途   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!