USE [EnVisage]
GO

if exists (select 1 from sys.tables where name like 'Training')
begin
	ALTER TABLE [dbo].[PeopleResourceTraining] DROP CONSTRAINT [FK_PeopleResourceTraining_PeopleResource]
	ALTER TABLE [dbo].[PeopleResourceTraining] DROP CONSTRAINT [FK_PeopleResourceTraining_Training]
	ALTER TABLE [dbo].[PeopleResourceTraining] DROP CONSTRAINT [DF_PeopleResourceTraining_Id]
	DROP TABLE [dbo].[PeopleResourceTraining]

	ALTER TABLE [dbo].[Training] DROP CONSTRAINT [FK_Training_TrainingType]
	ALTER TABLE [dbo].[Training] DROP CONSTRAINT [DF_Training_Id]
	DROP TABLE [dbo].[Training]

	ALTER TABLE [dbo].[TrainingType] DROP CONSTRAINT [DF_TrainingType_Id]
	DROP TABLE [dbo].[TrainingType]

end


CREATE TABLE [dbo].[TrainingType](
	[Id] [uniqueidentifier] NOT NULL,
	[Name] [nvarchar](255) NOT NULL
 CONSTRAINT [PK_TrainingType] 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]


CREATE TABLE [dbo].[Training](
	[Id] [uniqueidentifier] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[StartDate] [datetime] NOT NULL,
	[EndDate] [datetime] NOT NULL,
	[TrainingTypeId] [uniqueidentifier] NOT NULL,
	[Cost] [decimal](15,4) NOT NULL,
	[PercentAllocated] smallint NOT NULL,
 CONSTRAINT [PK_Training] 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

CREATE TABLE [dbo].[PeopleResourceTraining](
	[Id] [uniqueidentifier] NOT NULL,
	[TrainingId] [uniqueidentifier] NOT NULL,
	[PeopleResourceId] [uniqueidentifier] NOT NULL,
	[WeekEndingDate] [datetime] NOT NULL,
	[HoursOff] [int] NOT NULL,
 CONSTRAINT [PK_PeopleResourceTraining] 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

ALTER TABLE [dbo].[Training] ADD  CONSTRAINT [DF_Training_Id]  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [dbo].[PeopleResourceTraining] ADD  CONSTRAINT [DF_PeopleResourceTraining_Id]  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [dbo].[TrainingType] ADD  CONSTRAINT [DF_TrainingType_Id]  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [dbo].[Training]  WITH CHECK ADD  CONSTRAINT [FK_Training_TrainingType] FOREIGN KEY([TrainingTypeId])
REFERENCES [dbo].[TrainingType] ([Id])
GO

ALTER TABLE [dbo].[Training] CHECK CONSTRAINT [FK_Training_TrainingType]
GO

ALTER TABLE [dbo].[PeopleResourceTraining]  WITH CHECK ADD  CONSTRAINT [FK_PeopleResourceTraining_PeopleResource] FOREIGN KEY([PeopleResourceId])
REFERENCES [dbo].[PeopleResource] ([Id])
GO

ALTER TABLE [dbo].[PeopleResourceTraining] CHECK CONSTRAINT [FK_PeopleResourceTraining_PeopleResource]
GO

ALTER TABLE [dbo].[PeopleResourceTraining]  WITH CHECK ADD  CONSTRAINT [FK_PeopleResourceTraining_Training] FOREIGN KEY([TrainingId])
REFERENCES [dbo].[Training] ([Id])
GO

ALTER TABLE [dbo].[PeopleResourceTraining] CHECK CONSTRAINT [FK_PeopleResourceTraining_Training]
GO