Declarative Objectivity (DO) Language : Data Statements : CREATE Statement
CREATE Statement
A CREATE statement creates a new referenceable object in the federated database and returns a reference to that object.  
Syntax 
createStatement : createClause ;
createClause : CREATE objectStructure;
Used In 
ADD Clause, SET Clause, LET Statement  
Quick Look 
Create a vehicle with default attribute values:
CREATE Vehicle{};
Create a vehicle, setting the values of two of its attributes:
CREATE Vehicle{license: 'XLT5663', available: true};
Create a new rental contract and a new customer, one referencing the other:
CREATE RentalContract{trackingNumber: 345, customer: CREATE Customer{firstName: 'Fred', lastName: 'Smith'}};
Create a new rental contract that references an existing customer:
CREATE RentalContract{trackingNumber: 453, customer: 3-3-1-5};
Create a new location with an embedded address:
CREATE Location{airportCode: 'SJC', address: Address{city: 'San Jose', state: 'CA'}};
Create a new stall and add it to a location’s collection of referenced stalls:
UPDATE Location WHERE airportCode == 'SJC' ADD CREATE Stall{number: 1} TO stalls;
Create a new vehicle, and update a rental contract to reference it:
UPDATE RentalContract WHERE trackingNumber == 453 SET vehicle TO CREATE Vehicle{license: 'ABC1997'};
Set a parameter to a reference to a newly created vehicle, and use the parameter in another statement to create a new rental contract that references the vehicle:
LET v = CREATE Vehicle{license: 'XLT5663', available: true};
CREATE RentalContract{vehicle: $v};
Discussion 
A CREATE statement creates a new persistent object with default or specified values. The newly created object is stored persistently in the federated database when the statement returns.
A CREATE statement returns a reference to the newly created object. The reference is displayed as the object identifier (OID) of the new object.
Nested Create Statements
A CREATE statement can be nested within an ADD Clause, a SET Clause, a LET Statement, or an object structure, to create a new referenceable object. When nested, a CREATE statement does not include its own semicolon. The newly created object is stored persistently in the federated database when the enclosing statement returns.
Specifying the Class of the New Object
A CREATE statement uses an object structure to specify the class of the new object. The specified class must be a referenceable class defined in the schema. For example, the following CREATE statement can evaluate successfully, because Vehicle is a referenceable schema class:
CREATE Vehicle{};
3-3-1-100
In contrast, the following CREATE statement produces an error, because Address is an embeddable schema class:
CREATE Address{};
Error: Cannot create object of an embeddable class.
Note:Embedded objects are created implicitly whenever you create a referenceable object with embedded-object attribute. See Creating Embedded Objects, below.
Specifying Attribute Values for the New Object
You may use the object structure in a CREATE statement to specify nondefault values for one or more attributes of the object being created. Any attribute you omit from the object structure is given a default value of the appropriate type. You typically use Literal Expressions to specify nondefault attribute values, although more complex expressions can be used as long as they return values of the required type.
For example, the following statement creates a new Vehicle object with default values for all of its attributes:
CREATE Vehicle{};
The following statement creates a new Vehicle object with specified values for its license attribute (a string value) and for its servRecs attribute (a variable list of string values):
CREATE Vehicle{license: 'ABC1997', servRecs: LIST{'initial inspection', 'oil change'}};
The newly created object has default values for all other attributes defined by the Vehicle class. For example, the Boolean attribute retired is set to the default value false.
Note:You may provide a value for a collection attribute within the object being created only if the attribute is defined to hold a variable-size collection. An error occurs if the attribute is for fixed-size collections. (You must set the elements of a fixed-size collection individually after the enclosing object is created; see Setting List Elements.)
Creating Objects With Reference Attributes
When you create a new object of a class that defines a reference attribute, you can set that attribute with a reference to a new or existing object of the type expected by the attribute. For example, the Vehicle class defines an attribute called currentRental for holding a reference to an object of class RentalContract. When you create a new RentalContract, you can initialize the currentRental attribute in any of the following ways:
Use an object identifier (OID literal) to specify a reference to an existing RentalContract object:
CREATE Vehicle{license: 'ABC1997', currentRental:3-3-1-8};
Use a nested CREATE statement to create a new RentalContract object for the Vehicle’s currentRental attribute to reference:
CREATE Vehicle{license: 'ABC1997', currentRental: CREATE RentalContract{trackingNumber:42}};
Omit the currentRental attribute from the object structure to the allow that attribute to default to the null reference 0-0-0-0.
CREATE Vehicle{license: 'ABC1997'};
Creating Objects With Bidirectional References
When you create a new object of a class that defines a bidirectional-reference attribute, and you set that attribute to reference a new or existing object, the CREATE operation automatically updates the inverse attribute of the referenced object.
For example, the schema defines a bidirectional relationship between the Vehicle class and the RentalContract class through a pair of inverse reference attributes (namely, the Vehicle’s pastRentals attribute and the RentalContract’s vehicle attribute). This enables a vehicle to be associated with a list of rental contracts, where each rental contract is associated with exactly one vehicle. Now consider what happens when you create a new Vehicle and initialize its pastRentals list with a reference to an existing rental contract:
CREATE Vehicle{license: 'ABC1997', pastRentals:LIST{3-3-1-8}};
This statement:
Explicitly creates a new Vehicle object.
Explicitly sets the specified RentalContract as the first (and only) element of the new Vehicle’s pastRentals list.
Implicitly sets the inverse vehicle attribute in the RentalContract to reference the new Vehicle.
It is important to note that this statement succeeds only if the specified RentalContract’s vehicle attribute is not already set to reference some other Vehicle. That is, a CREATE operation can automatically “fill in” an inverse that is currently null, but cannot be used to “switch” an object from one bidirectional relationship to another. (You must execute a separate statement that clears the inverse first, before trying to perform the CREATE operation.)
Creating Embedded Objects
Whereas a referenceable object must be created explicitly using a CREATE statement, an embedded object is created implicitly whenever you create an object of a class that defines an embedded-object attribute. An embedded object is stored within the data of another object and cannot be referenced or otherwise accessed independently of the top-level referenceable object that encloses it.
When you create a new referenceable object that has an embedded-object attribute, you can optionally specify values for the implicitly created embedded object. For example, the Location class defines an attribute called address for holding an object of the embeddable class Address. When you create a new Location object, you can initialize the attributes of the embedded Address in any of the following ways:
Use a nested object structure to set values for some or all of the embedded Address’s attributes:
CREATE Location{airportCode: 'SJC', address: Address{city: 'San Jose', state: 'CA'}};
Treat the attributes of the embedded Address as indirect attributes of the enclosing Location:
CREATE Location{airportCode: 'SJC', address.city: 'San Jose', address.state: 'CA'};
Omit the address attribute from the top-level object structure to set all of the embedded Address’s attributes to their respective default values.
CREATE Location{airportCode: 'SJC'};