The M tip of the day: Defining a simple tree

With a simple tree I mean a self-referencing one-to-many relationship without a strategy to handle cycles between linked instances. An excellent MSDN-article on that subject is found here.

 

module Tree

{

    type Person

    {

        Id : Integer32 = AutoNumber();

        Name : Text#50;

        Parent : Person? where value in People;

    } where identity(Id);

 

    People : Person*;

}

This is translated into the following T-SQL:

 

create schema [Tree];

go

 

create table [Tree].[People]

(

  [Id] int not null identity,

  [Name] nvarchar(50) not null,

  [Parent] int null,

  constraint [PK_People] primary key clustered ([Id]),

  constraint [FK_People_Parent_Tree_People]

  foreign key ([Parent]) references [Tree].[People] ([Id])

);

go

About these ads

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: