/****** Script Date: 1/18/2016 ******/
/****** Object:  Table [dbo].[ddField] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ddField]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ddField](
	[FieldId] [int] IDENTITY (1, 1) NOT NULL,
	[FieldName] [nvarchar](50) NOT NULL,
	[ShortDesc] [nvarchar](100) NULL,
	[LongDesc] [nvarchar](255) NULL,
	[TableId] [int] NOT NULL,
	[ColOrder] [int] NOT NULL,
	[ColTypeId] [int] NOT NULL,
	[Size] [int] NULL,
	[Prec] [int] NULL,
	[Scale] [int] NULL,
	[DefaultValue] [nvarchar](50) NULL,
	[MinValue] [nvarchar](50) NULL,
	[MaxValue] [nvarchar](50) NULL,
	[IsPrimaryKey] [bit] NOT NULL,
	[IsForeignKey] [bit] NOT NULL,
	[IsRequired] [bit] NOT NULL,
	[IsDisplayed] [bit] NOT NULL,
	[IsEditable] [bit] NOT NULL,
	[IsComputed] [bit] NOT NULL,
	[IsRW] [bit] NOT NULL,
	[IsAutoInc] [bit] NOT NULL,
	[ForeignTableId] [int] NULL,
	[ForeignFieldId] [int] NULL,
	[ForeignResultFieldId] [int] NULL,
	[IsInternal] [bit] NOT NULL CONSTRAINT [DF_Field_IsInternal]  DEFAULT ((0)),
 CONSTRAINT [PK_Field] PRIMARY KEY CLUSTERED 
(
	[FieldId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ddFieldType] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ddFieldType]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ddFieldType](
	[FieldTypeId] [int] IDENTITY (1, 1) NOT NULL,
	[Name] [nvarchar](50) NULL,
	[Length] [int] NULL,
	[Prec] [int] NULL,
	[Scale] [int] NULL,
	[DataType] [nvarchar](20) NULL,
	[IsFixedLength] [bit] NULL,
	[SQLName] [nvarchar](25) NULL,
	[JetName] [nvarchar](25) NULL,
	[XsdName] [nvarchar](25) NULL,
 CONSTRAINT [PK_FieldType] PRIMARY KEY CLUSTERED 
(
	[FieldTypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ddIndex] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ddIndex]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ddIndex](
	[IndexId] [int] IDENTITY (1, 1) NOT NULL,
	[IndexName] [nvarchar](50) NOT NULL,
	[TableId] [int] NOT NULL,
	[IsUnique] [bit] NOT NULL,
	[IsPrimary] [bit] NOT NULL,
	[IsRW] [bit] NOT NULL,
	[AllowNulls] [bit] NOT NULL,
	[IsPreferredSort] [bit] NOT NULL,
 CONSTRAINT [PK_Index] PRIMARY KEY CLUSTERED 
(
	[IndexId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ddIndexField] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ddIndexField]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ddIndexField](
	[IndexKeyId] [int] IDENTITY (1, 1) NOT NULL,
	[TableId] [int] NOT NULL,
	[IndexId] [int] NOT NULL,
	[FieldId] [int] NOT NULL,
	[ColOrder] [int] NOT NULL,
	[IsAscending] [bit] NOT NULL,
 CONSTRAINT [PK_IndexField] PRIMARY KEY CLUSTERED 
(
	[IndexKeyId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[ddTable] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ddTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ddTable](
	[TableId] [int] IDENTITY (1, 1) NOT NULL,
	[TableName] [nvarchar](50) NOT NULL,
	[ShortDesc] [nvarchar](100) NULL,
	[LongDesc] [nvarchar](255) NULL,
	[SchemaVer] [int] NULL,
	[IsRw] [bit] NOT NULL,
 CONSTRAINT [PK_Table] PRIMARY KEY CLUSTERED 
(
	[TableId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Field_FieldType]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddField]'))
ALTER TABLE [dbo].[ddField]  WITH CHECK ADD  CONSTRAINT [FK_Field_FieldType] FOREIGN KEY([ColTypeId])
REFERENCES [dbo].[ddFieldType] ([FieldTypeId])
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Field_FieldType]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddField]'))
ALTER TABLE [dbo].[ddField] CHECK CONSTRAINT [FK_Field_FieldType]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Field_Table]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddField]'))
ALTER TABLE [dbo].[ddField]  WITH CHECK ADD  CONSTRAINT [FK_Field_Table] FOREIGN KEY([TableId])
REFERENCES [dbo].[ddTable] ([TableId])
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Field_Table]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddField]'))
ALTER TABLE [dbo].[ddField] CHECK CONSTRAINT [FK_Field_Table]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Index_Table]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddIndex]'))
ALTER TABLE [dbo].[ddIndex]  WITH CHECK ADD  CONSTRAINT [FK_Index_Table] FOREIGN KEY([TableId])
REFERENCES [dbo].[ddTable] ([TableId])
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Index_Table]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddIndex]'))
ALTER TABLE [dbo].[ddIndex] CHECK CONSTRAINT [FK_Index_Table]
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_IndexField_Index]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddIndexField]'))
ALTER TABLE [dbo].[ddIndexField]  WITH CHECK ADD  CONSTRAINT [FK_IndexField_Index] FOREIGN KEY([IndexId])
REFERENCES [dbo].[ddIndex] ([IndexId])
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_IndexField_Index]') AND parent_object_id = OBJECT_ID(N'[dbo].[ddIndexField]'))
ALTER TABLE [dbo].[ddIndexField] CHECK CONSTRAINT [FK_IndexField_Index]
GO
