上一节咱们浏览了一些OrientDB的基本概念,而且说起一个讨论最多的:支持SQL语言。Is not a contradiction for a DBMS that is defined NoSQL embrace this standard?Maybe not。
【讲了一堆废话,懒得翻了,浪费时间,我也没怎么看懂】
We defined in the introductory movement NoSQL not as something contrary to SQL itself, but as the warning "use the DBMS right for your use case. So why support its SQL ? Incidentally the relational model is very different from the graph and very far from the concepts of schema-less document database.
The answer is simple: anyone reading this know SQL because it has been the predominant technology in the last 30 years. Rather than inventing Yet Another Language I thought from ancient SQL would be a good idea to allow anyone to use OrientDB from day one.
Obviously the standard SQL language has no concept of schema-less, trees or graphs because it was designed for a model that does not cover this kind of structures, if not purely through adaptation applications. To support these new paradigms are created for new entrants and an extended syntax.
那么咱们开始使用绑定的演示数据库。打开控制台(见前面的章节)并链接到你电脑上的数据库demo。如今运行:select * from city(就像你在关系数据库中查找City表中的全部记录)。
如今,让咱们建立一个带条件的查询,包含多个链接的记录。在关系世界就是一个多表间的join,but the links are directly OrientDB waterways。下面就是它的实现方法:
> select * from city where country.name = 'Italy'
在这个例子中,这个查询将返回国家名是“Italy”的城市:简单,快速,简洁。想象一个更复杂的查询,它的条件牵涉更多的记录。等价的关系查询很是长而且难以阅读,但用OrientDB,全部事情都很简单和可读。若是你省略了projections(就是在关键字“SELECT”和“FROM”之间的,这个例子里是“*”),OrientDB老是返回全部记录。例如:
E 'can also navigate through records in the projections, useful if you do affect, not as a result the current class, but connected elements。例如咱们想从Address类型的记录中抽取全部意大利的前三个城市的名字,能够这样:
> select city.name from address where city.country.name = 'Italy' limit 3
注意limit子句3,它约束结果为3个元素。如今咱们已经了解了链接(LINK),让咱们看看如何使用复杂类型好比Collection,设计为一个列表或集合。这两个之间的区别是,列表保持按位置排序的元素的顺序并且容许重复,然而在集合中不能有重复,并且排序是任意的【一堆废话,其实就是说List是有序有重复,Set是无序无重复】。在下面的例子中我想尝试包含多个地址的帐户。“addresses”属性是一个包含Address类型记录的集合,它描述了1-N(一对多)关系。关系数据库中须要一个外键来描述关系,OrientDB用引用集合(或链接)。
> select from account where addresses.size() > 0
size()操做是其中一个可用的操做。在下一节,咱们将看到其余更详细的集合和映射操做。如今咱们将用更复杂例子来结束这一节:查找全部地址是“Washington”的帐户(addresses是Address的collection)。
> select flatten(addresses) from Account where addresses contains ( city.country.name = 'Washington' )
这个例子中咱们用contains操做,它为collection中全部元素执行括号中的条件,若是最终有一个(以上)元素知足条件,就返回TRUE。flatten()操做抽取全部结果的collection,而后所有放到一个collection中,使他们更加简单。