USE [EnVisage]
GO

if exists (select 1 from sys.tables where name like 'Vacation')
begin
	ALTER TABLE [dbo].[PeopleResourceVacation] DROP CONSTRAINT [FK_PeopleResourceVacation_PeopleResource]
	ALTER TABLE [dbo].[PeopleResourceVacation] DROP CONSTRAINT [DF_PeopleResourceVacation_Id]
	ALTER TABLE [dbo].[PeopleResourceVacation] DROP CONSTRAINT [FK_PeopleResourceVacation_Vacation]
	DROP TABLE [dbo].[PeopleResourceVacation]

	ALTER TABLE [dbo].[Vacation] DROP CONSTRAINT [FK_Vacation_PeopleResource]
	ALTER TABLE [dbo].[Vacation] DROP CONSTRAINT [DF_Vacation_Id]
	DROP TABLE [dbo].[Vacation]
end


CREATE TABLE [dbo].[Vacation](
	[Id] [uniqueidentifier] NOT NULL,
	[StartDate] [datetime] NOT NULL,
	[EndDate] [datetime] NOT NULL,
	[PeopleResourceId] [uniqueidentifier] NOT NULL,
	[HoursOff] [decimal](5,4) NOT NULL,
 CONSTRAINT [PK_Vacation] 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].[Vacation] ADD  CONSTRAINT [DF_Vacation_Id]  DEFAULT (newid()) FOR [Id]
GO

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

ALTER TABLE [dbo].[Vacation] CHECK CONSTRAINT [FK_Vacation_PeopleResource]
GO


CREATE TABLE [dbo].[PeopleResourceVacation](
	[Id] [uniqueidentifier] NOT NULL,
	[PeopleResourceId] [uniqueidentifier] NOT NULL,
	[VacationId] [uniqueidentifier] NOT NULL,
	[WeekEndingDate] [datetime] NOT NULL,
	[HoursOff] [int] NOT NULL,
	[LastUpdate] [datetime] NOT NULL,
 CONSTRAINT [PK_PeopleResourceVacation] 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].[PeopleResourceVacation] ADD  CONSTRAINT [DF_PeopleResourceVacation_Id]  DEFAULT (newid()) FOR [Id]
GO

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

ALTER TABLE [dbo].[PeopleResourceVacation] CHECK CONSTRAINT [FK_PeopleResourceVacation_PeopleResource]
GO

ALTER TABLE [dbo].[PeopleResourceVacation]  WITH CHECK ADD  CONSTRAINT [FK_PeopleResourceVacation_Vacation] FOREIGN KEY([VacationId])
REFERENCES [dbo].[Vacation] ([Id])
GO

ALTER TABLE [dbo].[PeopleResourceVacation] CHECK CONSTRAINT [FK_PeopleResourceVacation_Vacation]
GO