CREATE TABLE [dbo].[md_fields]
(
[fld_table_name] [dbo].[ais_name] NOT NULL,
[fld_field_name] [dbo].[ais_name] NOT NULL,
[fld_rowver] [timestamp] NOT NULL,
[fld_l1] [nvarchar] (64) COLLATE French_CI_AS NOT NULL,
[fld_l2] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_l3] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_dsc_l1] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_dsc_l2] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_dsc_l3] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_type] [dbo].[ais_name] NOT NULL,
[fld_size] [smallint] NOT NULL,
[fld_nullable] [bit] NOT NULL CONSTRAINT [DF__md_fields__fld_nullable] DEFAULT ((0)),
[fld_read_only] [bit] NOT NULL CONSTRAINT [DF__md_fields__fld_read_only] DEFAULT ((0)),
[fld_virtual] [bit] NOT NULL CONSTRAINT [DF__md_fields__fld_virtual] DEFAULT ((0)),
[fld_mask_code] [dbo].[ais_code_short] NULL,
[fld_default] [nvarchar] (64) COLLATE French_CI_AS NULL,
[fld_link_table_name] [dbo].[ais_name] NULL,
[fld_pk] [bit] NOT NULL CONSTRAINT [DF__md_fields__fld_pk] DEFAULT ((0)),
[fld_ordinal_position] [int] NOT NULL,
[fld_grid_name] [dbo].[ais_name] NULL,
[fld_grid_hidden] [bit] NOT NULL CONSTRAINT [DF__md_fields__fld_grid_hidden] DEFAULT ((0))
) ON [PRIMARY]
ALTER TABLE [dbo].[md_fields] ADD
CONSTRAINT [FK_md_fields_md_link_tables] FOREIGN KEY ([fld_link_table_name]) REFERENCES [dbo].[md_link_tables] ([ltbl_link_table_name]) ON UPDATE CASCADE

ALTER TABLE [dbo].[md_fields] ADD
CONSTRAINT [CK__md_fields_fld_field_name__string_pk] CHECK ((len(ltrim(rtrim([fld_field_name])))>(0) AND charindex('*',[fld_field_name])=(0) AND charindex('%',[fld_field_name])=(0)))

GO
ALTER TABLE [dbo].[md_fields] ADD CONSTRAINT [PK_md_fields] PRIMARY KEY CLUSTERED  ([fld_table_name], [fld_field_name]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[md_fields] ADD CONSTRAINT [FK_md_fields_md_grids] FOREIGN KEY ([fld_grid_name]) REFERENCES [dbo].[md_grids] ([grd_grid_name])
GO

ALTER TABLE [dbo].[md_fields] ADD CONSTRAINT [FK_md_fields_md_masks] FOREIGN KEY ([fld_mask_code]) REFERENCES [dbo].[md_masks] ([msk_mask_code])
GO
ALTER TABLE [dbo].[md_fields] WITH NOCHECK ADD CONSTRAINT [FK_md_fields_md_tables] FOREIGN KEY ([fld_table_name]) REFERENCES [dbo].[md_tables] ([tbl_table_name]) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[md_fields] ADD CONSTRAINT [FK_md_fields_md_types] FOREIGN KEY ([fld_type]) REFERENCES [dbo].[md_types] ([typ_type_name])
GO
