Orientdb 简明教程

OrientDB - Create Class

OrientDB 支持多模型功能,并提供不同的方法来理解和了解数据库的基本概念。然而,我们可以轻松访问这些模型,从文档数据库 API 的角度来看。与 RDBMS 类似,OrientDB 也使用 Record 作为存储元素,但它使用 Document 类型。文档以键值对的形式存储。我们将字段和属性存储为属于概念类的键值对。

OrientDB supports multi-model feature and provides different ways in approaching and understanding the basic concepts of a database. However, we can easily access these models from the perspective of Document database API. Like RDBMS, OrientDB also uses the Record as an element of storage but it uses the Document type. Documents are stored in the form of Key/Value pairs. We are storing fields and properties as key/value pairs which belong to a concepts class.

Class 是一种数据模型,这种概念取自面向对象编程模式。基于传统的文档数据库模型,数据以集合的形式存储,而在关系数据库模型中,数据存储在表中。OrientDB 遵循文档 API 和 OPPS 模式。作为一个概念,OrientDB 中的类与关系数据库中的表有最密切的关系,但是(与表不同)类可以无模式、全模式或混合。类可以从其他类继承,创建类树。每个类都有自己的集群或多个集群(如果未定义,则默认创建)。

Class is a type of data model and the concept is drawn from the Object-oriented programming paradigm. Based on the traditional document database model, data is stored in the form of collection, while in the relational database model data it is stored in tables. OrientDB follows the Document API along with OPPS paradigm. As a concept, class in OrientDB has the closest relationship with the table in relational databases, but (unlike tables) classes can be schema-less, schema-full or mixed. Classes can inherit from other classes, creating trees of classes. Each class has its own cluster or clusters, (created by default, if none are defined).

以下语句是 Create Class Command 的基本语法。

The following statement is the basic syntax of the Create Class Command.

CREATE CLASS <class>
[EXTENDS <super-class>]
[CLUSTER <cluster-id>*]
[CLUSTERS <total-cluster-number>]
[ABSTRACT]

以下是上文中选项的详细信息。

Following are the details about the options in the above syntax.

<class> − 定义要创建的类的名称。

<class> − Defines the name of the class you want to create.

<super-class> − 定义要使用此类扩展的超类。

<super-class> − Defines the super-class you want to extend with this class.

<total-cluster-number> − 定义此类中使用的集群总数。默认值为 1。

<total-cluster-number> − Defines the total number of clusters used in this class. Default is 1.

ABSTARCT − 定义类是抽象的。此为可选。

ABSTARCT − Defines the class is abstract. This is optional.

Example

如前所述,类是一个与表相关的概念。因此,这里我们将创建一个表 Account。但是,在创建类时,我们无法定义字段,即基于 OOPS 模式的属性。

As discussed, class is a concept related to table. Therefore here we will create a table Account. However, while creating class we cannot define fields i.e., properties based on OOPS paradigm.

以下命令用于创建一个名为 Account 的类。

The following command is to create a class named Account.

orientdb> CREATE CLASS Account

如果上述命令执行成功,您将获得以下输出内容。

If the above command is executed successfully, you will get the following output.

Class created successfully

您可以使用以下命令来创建一个 Car 类,该类扩展至 Vehicle 类。

You can use the following command to create a class Car which extends to class Vehicle.

orientdb> CREATE CLASS Car EXTENDS Vehicle

如果上述命令执行成功,您将获得以下输出内容。

If the above command is executed successfully, you will get the following output.

Class created successfully

您可以使用以下命令来创建一个抽象 Person 类。

You can use the following command to create a class Person as abstract.

orientdb> CREATE CLASS Person ABSTRACT

如果上述命令执行成功,您将获得以下输出内容。

If the above command is executed successfully, you will get the following output.

Class created successfully

Note − 在没有属性的情况下,该类是无用的并且无法构建实际对象。在后面的章节中,您可以学习如何为特定类创建属性。

Note − Without having properties, the class is useless and unable to build real object. In the further chapters, you can learn how to create properties for a particular class.