-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial: Classes
Previous : Tutorial: Run the console - Next: Tutorial: Clusters
As with the relational DBMS, in OrientDB we have the concept of records as an element of storage: there are different types of records, but over the next examples we always use the document. A document is composed of attributes and can belong to one class. From now on we will refer to the attributes also with the term of fields and properties.
The concept of class is well known to those who program using object-oriented languages and also in OrientDB is a type of data modeled according to certain rules. To know more about Classes look at Wikipedia.
To list all the configured classes, type the classes command in the console:
orientdb> classes
CLASSES:
----------------------------------------------+---------------------+-----------+
NAME | CLUSTERS | RECORDS |
----------------------------------------------+---------------------+-----------+
AbstractPerson | -1 | 0 |
Account | 11 | 1126 |
Actor | 91 | 3 |
Address | 19 | 166 |
Animal | 17 | 0 |
.... | .... | .... |
Whiz | 14 | 1001 |
----------------------------------------------+---------------------+-----------+
TOTAL 22775 |
--------------------------------------------------------------------------------+
To create a new class use the create class command:
orientdb> create class Student
Class created successfully. Total classes in database now: 92
OrientDB allows to work in schema-less mode without defining properties. But properties are mandatory id you define indexes or constraints. To create a new property use the create property command. Example of creating three properties against the Student class:
orientdb> create property Student.name string
Property created successfully with id=1
orientdb> create property Student.surname string
Property created successfully with id=2
orientdb> create property Student.birthDate date
Property created successfully with id=3
To display the class Student use the info class command:
orientdb> info class Student
Class................: Student
Default cluster......: student (id=96)
Supported cluster ids: [96]
Properties:
-------------------------------+-------------+-------------------------------+-----------+----------+----------+-----------+-----------+
NAME | TYPE | LINKED TYPE/CLASS | MANDATORY | READONLY | NOT NULL | MIN | MAX |
-------------------------------+-------------+-------------------------------+-----------+----------+----------+-----------+-----------+
birthDate | DATE | null | false | false | false | | |
name | STRING | null | false | false | false | | |
surname | STRING | null | false | false | false | | |
-------------------------------+-------------+-------------------------------+-----------+----------+----------+-----------+-----------+
To add a constraint use the alter class command. For example the name field should be at least 3 characters:
orientdb> alter property Student.name min 3
Property updated successfully
To see all the records in a class use the browse class:
> browse class OUser
In this case we are enlisting all the users of the database (further deepen OrientDB security system), but for now OUser is a class like any other. For each query the console always shows us the number of the records in the result set and the Record's ID.
---+---------+--------------------+--------------------+--------------------+--------------------
#| RID |name |password |status |roles
---+---------+--------------------+--------------------+--------------------+--------------------
0| #5:0|admin |{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918|ACTIVE |[1]
1| #5:1|reader |{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30|ACTIVE |[1]
2| #5:2|writer |{SHA-256}B93006774CBDD4B299389A03AC3D88C3A76B460D538795BC12718011A909FBA5|ACTIVE |[1]
---+---------+--------------------+--------------------+--------------------+--------------------
The first column is a number used as an identifier to display the record's detail. To show the first record in detail is necessary to use the display record command with the number of record, in this case 0 (the first one):
orientdb> display record 0
--------------------------------------------------
ODocument - Class: OUser id: #5:0 v.0
--------------------------------------------------
name : admin
password : {SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918
status : ACTIVE
roles : [#4:0=#4:0]
Previous : Tutorial: Run the console - Next: Tutorial: Clusters