上一節咱們瀏覽了一些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中,使他們更加簡單。