USE [master]
GO

CREATE DATABASE [openFLE] ON  PRIMARY 
( NAME = N'openFLE', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\openFLE.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'openFLE_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\openFLE_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

ALTER DATABASE [openFLE] SET COMPATIBILITY_LEVEL = 100
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [openFLE].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [openFLE] SET ANSI_NULL_DEFAULT OFF 
GO

ALTER DATABASE [openFLE] SET ANSI_NULLS OFF 
GO

ALTER DATABASE [openFLE] SET ANSI_PADDING OFF 
GO

ALTER DATABASE [openFLE] SET ANSI_WARNINGS OFF 
GO

ALTER DATABASE [openFLE] SET ARITHABORT OFF 
GO

ALTER DATABASE [openFLE] SET AUTO_CLOSE OFF 
GO

ALTER DATABASE [openFLE] SET AUTO_CREATE_STATISTICS ON 
GO

ALTER DATABASE [openFLE] SET AUTO_SHRINK OFF 
GO

ALTER DATABASE [openFLE] SET AUTO_UPDATE_STATISTICS ON 
GO

ALTER DATABASE [openFLE] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO

ALTER DATABASE [openFLE] SET CURSOR_DEFAULT  GLOBAL 
GO

ALTER DATABASE [openFLE] SET CONCAT_NULL_YIELDS_NULL OFF 
GO

ALTER DATABASE [openFLE] SET NUMERIC_ROUNDABORT OFF 
GO

ALTER DATABASE [openFLE] SET QUOTED_IDENTIFIER OFF 
GO

ALTER DATABASE [openFLE] SET RECURSIVE_TRIGGERS OFF 
GO

ALTER DATABASE [openFLE] SET  DISABLE_BROKER 
GO

ALTER DATABASE [openFLE] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO

ALTER DATABASE [openFLE] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO

ALTER DATABASE [openFLE] SET TRUSTWORTHY OFF 
GO

ALTER DATABASE [openFLE] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO

ALTER DATABASE [openFLE] SET PARAMETERIZATION SIMPLE 
GO

ALTER DATABASE [openFLE] SET READ_COMMITTED_SNAPSHOT OFF 
GO

ALTER DATABASE [openFLE] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [openFLE] SET  READ_WRITE 
GO

ALTER DATABASE [openFLE] SET RECOVERY FULL 
GO

ALTER DATABASE [openFLE] SET  MULTI_USER 
GO

ALTER DATABASE [openFLE] SET PAGE_VERIFY CHECKSUM  
GO

ALTER DATABASE [openFLE] SET DB_CHAINING OFF 
GO


USE [openFLE]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[FaultLocationAlgorithm](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NULL,
	[Description] [varchar](max) NULL,
	[AssemblyName] [varchar](max) NOT NULL,
	[TypeName] [varchar](max) NOT NULL,
	[MethodName] [varchar](200) NOT NULL,
	[Parameters] [varchar](max) NOT NULL,
	[Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_FaultLocationAlgorithm] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[FaultDetectionAlgorithm](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NULL,
	[Description] [varchar](max) NULL,
	[AssemblyName] [varchar](max) NOT NULL,
	[TypeName] [varchar](max) NOT NULL,
	[MethodName] [varchar](200) NOT NULL,
	[Parameters] [varchar](max) NOT NULL,
	[Enabled] [bit] NOT NULL,
 CONSTRAINT [PK_FaultDetectionAlgorithm] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Substation](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NOT NULL,
	[Description] [varchar](max) NULL,
	[Latitude] [decimal](9, 6) NOT NULL,
	[Longitude] [decimal](9, 6) NOT NULL,
 CONSTRAINT [PK_Substation] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Bus](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NOT NULL,
	[Description] [varchar](max) NULL,
	[SubstationID] [int] NOT NULL,
 CONSTRAINT [PK_Bus] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Bus]  WITH CHECK ADD  CONSTRAINT [FK_Bus_Substation] FOREIGN KEY([SubstationID])
REFERENCES [dbo].[Substation] ([ID])
GO

ALTER TABLE [dbo].[Bus] CHECK CONSTRAINT [FK_Bus_Substation]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Device](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NOT NULL,
	[Description] [varchar](max) NULL,
	[SubstationID] [int] NOT NULL,
 CONSTRAINT [PK_Device] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Device]  WITH CHECK ADD  CONSTRAINT [FK_Device_Substation] FOREIGN KEY([SubstationID])
REFERENCES [dbo].[Substation] ([ID])
GO

ALTER TABLE [dbo].[Device] CHECK CONSTRAINT [FK_Device_Substation]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Line](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](200) NOT NULL,
	[Description] [varchar](max) NULL,
	[SourceBusID] [int] NOT NULL,
	[DestinationBusID] [int] NOT NULL,
	[NominalKV] [int] NOT NULL,
	[LineMiles] [decimal](9, 6) NOT NULL,
	[R0] [decimal](9, 6) NOT NULL,
	[X0] [decimal](9, 6) NOT NULL,
	[R1] [decimal](9, 6) NOT NULL,
	[X1] [decimal](9, 6) NOT NULL,
	[FaultLocationAlgorithmID] [int] NULL,
	[FaultDetectionAlgorithmID] [int] NULL,
 CONSTRAINT [PK_Line] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Line]  WITH CHECK ADD  CONSTRAINT [FK_Line_DestinationBus] FOREIGN KEY([DestinationBusID])
REFERENCES [dbo].[Bus] ([ID])
GO

ALTER TABLE [dbo].[Line] CHECK CONSTRAINT [FK_Line_DestinationBus]
GO

ALTER TABLE [dbo].[Line]  WITH CHECK ADD  CONSTRAINT [FK_Line_FaultDetectionAlgorithm] FOREIGN KEY([FaultDetectionAlgorithmID])
REFERENCES [dbo].[FaultDetectionAlgorithm] ([ID])
GO

ALTER TABLE [dbo].[Line] CHECK CONSTRAINT [FK_Line_FaultDetectionAlgorithm]
GO

ALTER TABLE [dbo].[Line]  WITH CHECK ADD  CONSTRAINT [FK_Line_FaultLocationAlgorithm] FOREIGN KEY([FaultLocationAlgorithmID])
REFERENCES [dbo].[FaultLocationAlgorithm] ([ID])
GO

ALTER TABLE [dbo].[Line] CHECK CONSTRAINT [FK_Line_FaultLocationAlgorithm]
GO

ALTER TABLE [dbo].[Line]  WITH CHECK ADD  CONSTRAINT [FK_Line_SourceBus] FOREIGN KEY([SourceBusID])
REFERENCES [dbo].[Bus] ([ID])
GO

ALTER TABLE [dbo].[Line] CHECK CONSTRAINT [FK_Line_SourceBus]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[DeviceLine](
	[DeviceID] [int] NOT NULL,
	[LineID] [int] NOT NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[DeviceLine]  WITH CHECK ADD  CONSTRAINT [FK_DeviceLine_Device] FOREIGN KEY([DeviceID])
REFERENCES [dbo].[Device] ([ID])
GO

ALTER TABLE [dbo].[DeviceLine] CHECK CONSTRAINT [FK_DeviceLine_Device]
GO

ALTER TABLE [dbo].[DeviceLine]  WITH CHECK ADD  CONSTRAINT [FK_DeviceLine_Line] FOREIGN KEY([LineID])
REFERENCES [dbo].[Line] ([ID])
GO

ALTER TABLE [dbo].[DeviceLine] CHECK CONSTRAINT [FK_DeviceLine_Line]
GO


