OrientDB – Sequences

OrientDB – Sequences ”; Previous Next Sequences is a concept used in auto increment mechanism and it is introduced in OrientDB v2.2. In database terminology, sequence is a structure that manages the counter field. Simply said sequences are mostly used when you need a number that always increments. It supports two types− ORDERED − Each time the pointer calls the .next method that returns a new value. CACHED − The sequence will cache ‘N’ items on each node. To call each item we use .next(), which is preferred when the cache contains more than one item. Create Sequence Sequence is usually used to auto increment the id value of a person. Like other SQL concepts of OrientDB it also preforms similar operations as Sequence in RDBMS. The following statement is the basic syntax to create sequences. CREATE SEQUENCE <sequence> TYPE <CACHED|ORDERED> [START <start>] [INCREMENT <increment>] [CACHE <cache>] Following are the details about the options in the above syntax. <Sequence> − Local name for sequence. TYPE − Defines the sequence type ORDERED or CACHED. START − Defines the initial value. INCREMENT − Defines the increment for each .next method call. CACHE − Defines the number of value to pre-cache, in the event that you used to cache sequence type. Let us create a sequence named ‘seqid’ which starts with number 1201. Try the following queries to implement this example with sequence. CREATE SEQUENCE seqid START 1201 If the above query is executed successfully, you will get the following output. Sequence created successfully Try the following query to use sequence ‘seqid’ to insert the id value of Account table. INSERT INTO Account SET id = sequence(”seqid”).next() If the above query is executed successfully, you will get the following output. Insert 1 record(s) in 0.001000 sec(s) Alter Sequence Alter sequence is a command used to change the properties of a sequence. It will modify all the sequence options except sequence type. The following statement is the basic syntax to alter sequence. ALTER SEQUENCE <sequence> [START <start-point>] [INCREMENT <increment>] [CACHE <cache>] Following are the details about the options in the above syntax. <Sequence> − Defines the sequence you want to change. START − Defines the initial value. INCREMENT − Defines the increment for each .next method call. CACHE − Defines the number of value to pre-cache in the event that you used to cache sequence type. Try the following query to alter the start value from ‘1201 to 1000’ of a sequence named seqid. ALTER SEQUENCE seqid START 1000 If the above query is executed successfully, you will get the following output. Altered sequence successfully Drop Sequence Drop sequence is a command used to drop a sequence. The following statement is the basic syntax to drop a sequence. DROP SEQUENCE <sequence> Where <Sequence> defines the sequence you want to drop. Try the following query to drop a sequence named ‘seqid’. DROP SEQUENCE seqid If the above query is executed successfully, you will get the following output. Sequence dropped successfully Print Page Previous Next Advertisements ”;

OrientDB – Create Edge

OrientDB – Create Edge ”; Previous Next In OrientDB, the concept Edge works like a relation between vertices with the help of some properties. Edges and vertices are the main components of a graph database. It applies polymorphism on Edges. The base class for an Edge is E. While implementing edges, if source or destination vertices are missing or don’t exist, then the transaction will be rollback. The following statement is the basic syntax of Create Edge Command. CREATE EDGE <class> [CLUSTER <cluster>] FROM <rid>|(<query>)|[<rid>]* TO <rid>|(<query>)|[<rid>]* [SET <field> = <expression>[,]*]|CONTENT {<JSON>} [RETRY <retry> [WAIT <pauseBetweenRetriesInMs]] [BATCH <batch-size>] Following are the details about the options in the above syntax. <class> − Defines the class name for the edge. <cluster> − Defines the cluster in which you want to store the edge. JSON − Provides JSON content to set as the record. RETRY − Defines the number of retries to attempt in the event of conflict. WAIT − Defines the time to delay between retries in milliseconds. BATCH − Defines whether it breaks the command down into smaller blocks and the size of the batches. Example Execute the following query to create an edge E between two vertices #9:0 and #14:0. orientdb> CREATE EDGE FROM #11:4 TO #13:2 If the above query is executed successfully, you will get the following output. Created edge ”[e[#10:0][#9:0->#14:0]]” in 0.012000 sec(s) Execute the following query to create a new edge type and an edge of new type. orientdb> CREATE CLASS E1 EXTENDS E orientdb> CREATE EDGE E1 FROM #10:3 TO #11:4 If the above query is executed successfully, you will get the following output. Created edge ”[e[#10:1][#10:3->#11:4]]” in 0.011000 sec(s) Print Page Previous Next Advertisements ”;

OrientDB – Drop Property

OrientDB – Drop Property ”; Previous Next The Drop property command removes the property from the schema. It does not remove the property values from the record, it just change the schema. The following statement is the basic syntax of Drop Property Command. DROP PROPERTY <class>.<property> [FORCE] Following are the details about the options in the above syntax. <class> − Defines the class where the property exists. <property> − Defines the property you want to remove. [Force] − In case one or more indexes are defined on the property. Example Try the following command to remove ‘age’ property from the class ‘Customer’. orientdb> DROP PROPERTY Customer.age If the above command is executed successfully, you will get the following output. Property dropped successfully Print Page Previous Next Advertisements ”;

OrientDB – Functions

OrientDB – Functions ”; Previous Next This chapter explains the complete reference of different types of functions in OrientDB. The following table defines the list of functions, which are categorized by their functionality. Graph Functions The functions which are used to manipulate the graph data. Sr.No. Function Name & Description 1 Out(): Gets the adjacent outgoing vertices starting from the current record as Vertex. Syntax − out([<label-1>][,<label-n>]*) 2 In(): Gets the adjacent incoming vertices starting from the current record as Vertex. Syntax − in([<label-1>][,<label-n>]*) 3 Both(): Gets the adjacent outgoing and incoming vertices starting from the current record as Vertex. Syntax − both([<label1>][,<label-n>]*) 4 outE(): Gets the adjacent outgoing edges starting from the current record as Vertex. Syntax − outE([<label1>][,<label-n>]*) 5 inE(): Gets the adjacent incoming edges starting from the current record as Vertex. Syntax − inE([<label1>][,<label-n>]*) 6 bothE(): Gets the adjacent outgoing and incoming edges starting from the current record as Vertex. Syntax − bothE([<label1>][,<label-n>]*) 7 outV(): Gets the outgoing vertices starting from the current record as Edge. Syntax − outV() 8 inV(): Get the incoming vertices from the current record as Edge. Syntax − inV() 9 traversedElement(): Returns the traversed element(s) in Traverse commands. Syntax − traversedElement(<index> [,<items>]) 10 traversedVertex(): Return the traversed vertex(es) in Traverse commands. Syntax − traversedVertex(<index> [,<items>]) 11 traversedEdge(): Returns the traversed edge(s) in Traverse commands. Syntax − traversedEdge(<index> [,<items>]) 12 shortestPath(): Returns the shortest path between two vertices. Direction can be OUT (default), IN or BOTH. Synatx − shortestPath( <sourceVertex>, <destinationVertex> [, <direction> [, <edgeClassName>]]) 13 dijkstra(): Returns the cheapest path between two vertices using the Dijkstra algorithm. Syntax − dijkstra(<sourceVertex>, <destinationVertex>, <weightEdgeFieldName> [, <direction>]) Try some graph functions along with the following queries. Execute the following query to get all the outgoing vertices from all the vehicle vertices. orientdb {db = demo}>SELECT out() from Vehicle If the above query is executed successfully, you will get the following output. —+———-+——— # | @class | out —+———-+——— 0 | Vehicle | #11:2 1 | Vehicle | #13:1 2 | Vehicle | #13:4 —+———-+——— Execute the following query to get both incoming and outgoing vertices from the vertex #11:3. orientdb {db = demo}>SELECT both() FROM #11:3 If the above query is executed successfully, you will get the following output. —+———-+——–+——- # | @class | out | in —+———-+——–+——- 0 | Vehicle | #13:2 | #10:2 —+———-+——-+——- Math Functions The following table defines the list of Math functions which are used to execute mathematical expressions. Sr.No. Function Name & Description 1 eval(): Evaluates the expression between quotes (or double quotes). Syntax − eval(”<expression>”) 2 min(): Returns the minimum value. If invoked with more than one parameter, then it returns minimum argument value between all the arguments. Syntax − min(<field> [, <field-n>]* ) 3 max(): Returns the maximum value. If invoked with more than one parameter, then returns the maximum value between all the arguments. Syntax − max(<field> [, <field-n>]* ) 4 sum() Returns the sum of all the values returned. Syntax − sum(<field>) 5 abs(): Returns the absolute value. It works with Integer, Long, Short, Double, Float, BigInteger, BigDecimal, null. Syntax − abs(<field>) 6 avg(): Returns the average value. Syntax − avg(<field>) 7 count(): Counts the record that matches the query condition. If * is not used as a field, then the record will be counted only if the content is not null. Syntax − count(<field>) 8 mode(): Returns the value that occurs with the greatest frequency. Nulls are ignored in the calculation. Syntax − mode(<field>) 9 median(): Returns the middle value or an interpolated value that represents the middle value after the values are sorted. Nulls are ignored in the calculation. Syntax − median(<field>) 10 percentile(): Returns the nth percentile. Null is ignored in the calculation. Syntax − percentile(<field> [, <quantile-n>]*) 11 variance() Returns the middle variance: The average of squared difference from the mean. Syntax − variance(<field>) 12 stddev() Returns the standard deviation: The measure of how spread out values are. Nulls are ignored in the calculation. Syntax − stddev(<field>) Try some math functions using the following queries. Execute the following query to get the sum of salaries of all employees. orientdb {db = demo}>SELECT SUM(salary) FROM Employee If the above query is executed successfully, you will get the following output. —+———-+——— # | @CLASS | sum —+———-+——— 0 | null | 150000 —+———-+——— Execute the following query to get the average salary of all employees. orientdb {db = demo}>SELECT avg(salary) FROM Employee If the above query is executed successfully, you will get the following output. —+———-+——— # | @CLASS | avg —+———-+——— 0 | null | 25 —+———-+——— Collections Functions The following table defines the list of functions that manipulate the collections data. Sr.No. Function Name & Description 1 set(): Adds a value to a set. If the value is a collection, then it is merged with the set, otherwise <value> is added. Syntax − set(<field>) 2 map(): Adds a value to a map the first time the map is created. If <value> is a map, then it is merged with the map, otherwise the pair <key> and <value> is added to map as new entry. Syntax − map(<key>, <value>) 3 ist(): Adds a value to list the first time the list is created. If <value> is a collection, then it is merged with the list, otherwise <value> is added to list. Syntax − list(<field>) 4 difference(): Works as aggregate or inline. If only one argument is passed then aggregates, otherwise executes, and returns the DIFFERENCE between the collections received as parameters. Syntax − difference(<field> [,<field-n>]*) 5 first(): Retrieves only the first item of multi-value fields (arrays, collections and maps). For non-multi-value types just returns the value. Syntax − first(<field>) 6 intersect(): Works as aggregate or inline. If only one argument is passed then aggregates, otherwise executes, and returns, the INTERACTION of the collections received as parameters. Syntax − intersect(<field> [,<field-n>]*) 7 distinct(): Retrieves only unique data entries depending on the field you have specified as argument. The main

OrientDB – Alter Property

OrientDB – Alter Property ”; Previous Next Alter Property is a command used to modify or update the Property of a particular class. Altering the property means modifying the fields of a table. In this chapter, you can learn how to update the property. The following statement is the basic syntax of Alter Property Command. ALTER PROPERTY <class>.<property> <attribute-name> <attribute-value> Following are the details about the options in the above syntax. <class> − Defines the class to which the property belongs. <property> − Defines the property you want to update. <attribute-name> − Defines the attribute of a property you want to update. <attribute-value> − Defines the value you want to set on the attribute. The following table defines the list of attributes to alter the property. Attribute Type Description LINKEDCLASS String Defines the linked class name. Use NULL to remove an existing value. LINKEDTYPE String Defines the link type. Use NULL to remove an existing value. MIN Integer Defines the minimum value as a constraint. Use NULL to remove an existing constraint. MANDATORY Boolean Defines whether the property requires a value. MAX Integer Defines the maximum value as a constraint. Use NULL to remove an existing constraint. NAME String Defines the property name. NOTNULL Boolean Defines whether the property can have a NULL value. REGEX String Defines a Regular Expression as constraint. Use NULL to remove an existing constraint. TYPE String Defines a property type. COLLATE String Sets collate to one of the defined comparison strategies. By default, it is set to case-sensitive (cs). You can also set it to case-insensitive (ci). READONLY Boolean Defines whether the property value is immutable. That is, if it is possible to change it after the first assignment. Use with DEFAULT to have immutable values on creation. CUSTOM String Defines custom properties. The syntax for custom properties is <custom-name> = <custom-value>, such as stereotype = icon. DEFAULT   Defines the default value or function. Note − if you are altering NAME or TYPE, this command will take some time to update depending on the amount of data. Example Try some queries which are given below to understand Alter property. Execute the following query to change the name of the property from ‘age’ to ‘born’ in the class Customer. orinetdb {db = demo}> ALTER PROPERTY Customer.age NAME born If the above query is executed successfully, you will get the following output. Property altered successfully Execute the following query to make ‘name’ as the mandatory property of the class ‘Customer’. orientdb {db = demo}> ALTER PROPERTY Customer.name MANDATORY TRUE If the above query is executed successfully, you will get the following output. Property altered successfully Print Page Previous Next Advertisements ”;

OrientDB – Truncate Cluster

OrientDB – Truncate Cluster ”; Previous Next The Truncate Cluster command deletes all records of a cluster. The following statement is the basic syntax of Truncate Cluster Command. TRUNCATE CLUSTER <cluster-name> Where <cluster-name> is the name of the cluster. Example Try the following query to truncate the cluster named sales. Orientdb {db = demo}> TRUNCATE CLUSTER Profile If the above query is executed successfully, you will get the following output. Cluster truncated successfully. Print Page Previous Next Advertisements ”;

OrientDB – Create Vertex

OrientDB – Create Vertex ”; Previous Next OrientDB database is not only a Document database but also a Graph database. New concepts such as Vertex and Edge are used to store the data in the form of graph. It applies polymorphism on vertices. The base class for Vertex is V. In this chapter you can learn how to create vertex to store graph data. The following statement is the basic syntax of Create Vertex Command. CREATE VERTEX [<class>] [CLUSTER <cluster>] [SET <field> = <expression>[,]*] Following are the details about the options in the above syntax. <class> − Defines the class to which the vertex belongs. <cluster> − Defines the cluster in which it stores the vertex. <field> − Defines the field you want to set. <expression> − Defines the express to set for the field. Example Try the following example to understand how to create vertex. Execute the following query to create a vertex without ‘name’ and on the base class V. orientdb> CREATE VERTEX If the above query is executed successfully, you will get the following output. Created vertex ”V#9:0 v1” in 0.118000 sec(s) Execute the following query to create a new vertex class named v1, then create vertex in that class. orientdb> CREATE CLASS V1 EXTENDS V orientdb> CREATE VERTEX V1 If the above query is executed successfully, you will get the following output. Created vertex ”V1#14:0 v1” in 0.004000 sec(s) Execute the following query to create a new vertex of the class named v1, defining its properties such as brand = ”Maruti” and name = ”Swift”. orientdb> CREATE VERTEX V1 SET brand = ”maruti”, name = ”swift” If the above query is executed successfully, you will get the following output. Created vertex ”V1#14:1{brand:maruti,name:swift} v1” in 0.004000 sec(s) Print Page Previous Next Advertisements ”;

OrientDB – Create Property

OrientDB – Create Property ”; Previous Next Property in OrientDB works like a field of class and column in the database table. Create Property is a command used to create a property for a particular class. The class name that you used in the command must exist. The following statement is the basic syntax of Create Property command. CREATE PROPERTY <class-name>.<property-name> <property-type> [<linked-type>][ <linked-class>] Following are the details about the options in the above syntax. <class-name> − Defines the class you want to create the property in. <property-name> − Defines the logical name of the property. <property-type> − Defines the type of property you want to create. <linked-type> − Defines the container type, used in container property type. <linked-class> − Defines the container class, used in container property type. The following table provides the data type for property so that OrientDB knows the type of data to store. BOOLEAN INTEGER SHORT LONG FLOAT DATE STRING EMBEDDED LINK BYTE BINARY DOUBLE In addition to these there are several other property types that work as containers. EMBEDDEDLIST EMBEDDEDSET EMBEDDEDMAP LINKLIST LINKSET LINKMAP Example Try the following example to create a property name on the class Employee, of the String type. orientdb> CREATE PROPERTY Employee.name STRING If the above query is executed successfully, you will get the following output. Property created successfully with id = 1 Print Page Previous Next Advertisements ”;

OrientDB – Drop Cluster

OrientDB – Drop Cluster ”; Previous Next The Drop Cluster command removes the cluster and all its related content. This operation is permanent and rollback. The following statement is the basic syntax of Drop Cluster command. DROP CLUSTER <cluster-name>|<cluster-id> Where <cluster-name> defines the name of the cluster you want to remove and <cluster-id> defines the ID of the cluster you want to remove. Example Try the following command to remove Sales cluster. orientdb> DROP CLUSTER Sales If the above query is executed successfully, you will get the following output. Cluster dropped successfully Print Page Previous Next Advertisements ”;

OrientDB – Create Cluster

OrientDB – Create Cluster ”; Previous Next Cluster is an important concept in OrientDB which is used to store records, documents, or vertices. In simple words, cluster is a place where a group of records are stored. By default, OrientDB will create one cluster per class. All the records of a class are stored in the same cluster, which has the same name as the class. You can create up to 32,767(2^15-1) clusters in a database. The CREATE class is a command used to create a cluster with a specific name. Once the cluster is created, you can use the cluster to save records by specifying the name during the creation of any data model. If you want to add a new cluster to a class, use Alter Class command and ADDCLUSTER command. The following statement is the basic syntax of Create Cluster command. CREATE CLUSTER <cluster> [ID <cluster-id>] Where <cluster> defines the name of the cluster you want to create and <cluster-id> defines the numeric ID you want to use for the cluster. The following table provides the list of Cluster selection strategies. Sr.No. Strategy & Description 1 Default Selects the cluster using the class property default ClusterId. 2 Round-robin Selects the next cluster in a circular order. It is restarting once complete. 3 Balanced Selects the smallest cluster. Allows the class to have all underlying clusters balanced on size. When adding a new cluster to an existing class, it fills the new cluster first. Example Let us take an example to create a cluster named sales. orientdb> CREATE CLUSTER sales If the above query is executed successfully, you will get the following output. Cluster created correctly with id #12 Print Page Previous Next Advertisements ”;