Console - CREATE LINK

Creates a link between two or more records of the Document type.

Syntax

  1. CREATE LINK <link-name> FROM <source-class>.<source-property> TO <target-class>.<target-property>
  • <link-name> Defines the logical name of the property for the link. When not expressed, it overwrites the <target-property> field.
  • <source-class> Defines the source class for the link.
  • <source-property> Defines the source property for the link.
  • <target-class> Defines the target class for the link.
  • <target-property> Defines the target property for the link.

Examples

  • Create a 1-n link connecting comments to posts:

    1. orientdb> CREATE LINK comments FROM Comments.!PostId TO Posts.Id INVERSE

Links are useful when importing data from a Relational database. In the Relational world, the database resolves relationships as foreign keys. For instance, consider the above example where you need to show instances in the class Post as having a 1-n relationship to instances in class Comment. That is, Post 1 ---> * Comment.

In a Relational database, where classes are tables, you might have something like this:

  1. reldb> SELECT * FROM Post;
  2.  
  3. +----+----------------+
  4. | Id | Title |
  5. +----+----------------+
  6. | 10 | NoSQL movement |
  7. | 20 | New OrientDB |
  8. +----+----------------+
  9. 2 rows in (0.01 sec)
  10.  
  11. reldb> SELECT * FROM Comment;
  12.  
  13. +----+--------+--------------+
  14. | Id | PostId | Text |
  15. +----+--------+--------------+
  16. | 0 | 10 | First |
  17. | 1 | 10 | Second |
  18. | 21 | 10 | Another |
  19. | 41 | 20 | First again |
  20. | 82 | 20 | Second Again |
  21. +----+--------+--------------+
  22. 5 rows in sec (0.03 sec)

In OrientDB, you have a direct relationship in your object model. Navigation runs from Post to Comment and not vice versa, (as in the Relational database model). For this reason, you need to create a link as INVERSE.

For more information on SQL commands, see SQL Commands.
For more information on other commands, see Console Commands.