USE [Mobile_Registration]
GO

/****** Object:  Table [dbo].[Hosts_Guids]    Script Date: 01/01/2013 23:54:12 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

if not exists(select * from sys.columns where Name = N'UpdatedToVersion' and Object_ID = Object_ID(N'[dbo].[Hosts_Guids]'))
begin
ALTER TABLE [dbo].[Hosts_Guids] ADD [UpdatedToVersion] VARCHAR(15) NULL;
ALTER TABLE [dbo].[Hosts_Guids] ADD [LastServerUpdate] DateTime NULL;
ALTER TABLE [dbo].[Hosts_Guids] ADD [IsInternal] bit NULL;

-- Create new Primary Key Constraint off of ID
ALTER TABLE [dbo].[Hosts_Guids] ADD CONSTRAINT [Hosts_Guids.Primary Key - ID] PRIMARY KEY CLUSTERED ([ID] ASC);

-- Create System Api Key Constraint of SystemApiKey
ALTER TABLE [dbo].[Hosts_Guids] ADD  CONSTRAINT [IX_SystemApiKeys] UNIQUE NONCLUSTERED 
(
	[SystemApiKey] ASC
)
--Only works on SQLServer 2008 it seems
--WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = OFF) ON [PRIMARY]

--
-- INDEXES 
--

-- Now tie System Api Keys and Host Guids together
CREATE UNIQUE NONCLUSTERED INDEX [IX_SystemApiKeys - HostGuid] ON [dbo].[Hosts_Guids] 
(
      [SystemApiKey] ASC,
      [Host_Guid] ASC
) ON [PRIMARY];

-- Create an Index on Host Guids
CREATE NONCLUSTERED INDEX [IX_HostGUIDs] ON [dbo].[Hosts_Guids] 
(
  [Host_Guid] ASC
) ON [PRIMARY];

-- Create an Index on Mobile Api Versions (for later lookup)
CREATE NONCLUSTERED INDEX [IX_ApiVersions] ON [dbo].[Hosts_Guids] 
(
  [UpdatedToVersion] DESC
) ON [PRIMARY];

-- Create an Index on last IP Update (for seeing who doesn't use it)
CREATE NONCLUSTERED INDEX [IX_IPUpade] ON [dbo].[Hosts_Guids] 
(
      [LastServerUpdate] ASC      
) ON [PRIMARY];

END 
GO

