-
Notifications
You must be signed in to change notification settings - Fork 0
SQL Create Link
The Create Link transform two simple values in a link. This is very useful when you're importing data from a Relational database. In facts in the Relational world relationships are resolved as foreign keys.
This is not the way to create links in general, but a way to convert two values in two different classes in a link. To create a link in OrientDB look at For more information about importing a Relational Database into OrientDB look at [[ImportFromRDBMS Import from RDBMS.]].
Consider this example where the class "Post" has a relationship 1-N to "Comment":
Post 1 ---> * Comment
In a Relational database you'll have something like that:
Table Post
+----+----------------+
| Id | Title |
+----+----------------+
| 10 | NoSQL movement |
| 20 | New OrientDB |
+----+----------------+
Table Comment
+----+--------+--------------+
| Id | PostId | Text |
+----+--------+--------------+
| 0 | 10 | First |
| 1 | 10 | Second |
| 21 | 10 | Another |
| 41 | 20 | First again |
| 82 | 20 | Second Again |
+----+--------+--------------+
Using OrientDB, instead, you have direct relationship as in your object model. So the navigation is from Post to Comment and not viceversa as for Relational model. For this reason you need to create a link as INVERSE.
{{{CREATE LINK FROM . TO . [Where:
- link-name is the name of the property for the link. If not expressed will be overwritten the destination-property field
- link-type, optional, is the type to use for the link. In case of inverse relationships (the most commons) you can specify LINKSET or LINKLIST for 1-N relationships
- source-class, is the source class
- source-property, is the source property
- destination-class, is the destination class
- destination-property, is the destination property
CREATE LINK comments TYPE LINKSET FROM comments.!PostId To posts.Id INVERSE
To know more about other SQL commands look at SQL SQL commands.