Share via


Creating Extents

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

An extent is a value that defines storage. Extents can be specified directly or derived from types. All module-scoped extents cause SQL tables to be created when compiled. Extents that occur inside types cause SQL tables to be generated when an extent of the containing type is declared in a module.

There are a number of ways to create extents. A collection of entities is one of the most common kinds of extents.

Any collection is an extent. This includes the following cases:

  • Collections of entities.

  • Collections of entity types.

  • Collection of simple (non-entity) types, such as ClaimIds : Integer32*;.

There are also ways to create non-collection extents. These include the following cases:

  • Derived from an entity type, such as Owner : Employee;.

  • Individual, simple, non-entity field, such as HomeSite : Text*;.

Collections of Entity Values

Extents can be defined without referencing a type. This is useful when a data structure is not expected to be reused. In this case the type of the extent is inferred and is anonymous, without a name.

The following example defines the extent by directly specifying the items that it contains.

module Northwind 
{
    Addresses 
    { 
        {Id => 1, Folder => 1, State => "NC", Zip => 28173},
        {Id => 2, Folder => 1, State => "WA", Zip => 55555}
    };
}

This code causes the following T-SQL code to be generated. Note that because initial values are specified, T-SQL insert statements are generated.

create table [Northwind].[Addresses]
(
  [Folder] int not null,
  [Id] int not null,
  [State] nvarchar(max) not null,
  [Zip] int not null
);
go
insert into [Northwind].[Addresses] ([Id], [Folder], [State], [Zip])
 values (1, 1, N'NC', 28173)
;
insert into [Northwind].[Addresses] ([Id], [Folder], [State], [Zip])
 values (2, 1, N'WA', 55555)
;

Collections of Entity Types

When you expect to reuse a data structure, define it as an entity type and reference that type when creating the extent. Do this by defining the entity type and then define the extent as a collection of those entity types.

In the following code, Employee is defined as an entity type. The Employees extent is defined as a collection of Employee types by referencing that type and appending the * operator, as shown in the following example.

The example also shows one way of initializing the entities in the Employees extent.

module Northwind {
    type Employee {
        ID      : Integer32;
        LastName : Text;
        FirstName : Text; 
    }
    Employees : {(Employee where identity ID)*};
    
    Employees
    {
        {ID => 1, LastName => "Doe",   FirstName => "John"},
        {ID => 2, LastName => "Jones", FirstName => "Jane"}
    }
}

This code causes the following T-SQL code to be generated.

create table [Northwind].[Employees]
(
    [ID] int not null,
    [LastName] nvarchar(max) not null,
    [FirstName] nvarchar(max) not null,
    constraint [PK_Employees] primary key clustered ([ID])
);
go
insert into [Northwind].[Employees] ([ID], [LastName], [FirstName])
    values (1, N'Doe', N'John'),
        (2, N'Jones', N'Jane');
go

Simple Field Extents

Extents can also be defined from simple types. In the following example, ItemCount is an extent.

module Northwind 
{
    ItemCount : Integer32;
}

This code causes the following T-SQL code to be generated.

create table [Northwind].[ItemCount]
(
  [Item] int not null
);