IF Not EXISTS(SELECT * FROM sysobjects WHERE name='ConnectedUser')
Begin
CREATE TABLE [dbo].[ConnectedUser](
	[UserId] [SMALLINT] NOT NULL,
	[ConnectionId] [UNIQUEIDENTIFIER] NOT NULL,
	[ProgramType] [CHAR](1) NOT NULL,
	[SystemName] [VARCHAR](50) NOT NULL,
 CONSTRAINT [PK_ConnectedUser] PRIMARY KEY CLUSTERED 
(
	[UserId] ASC,
	[ConnectionId] ASC
)
) ON [PRIMARY]

IF NOT EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_ConnectedUser_Users]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
Begin
  ALTER TABLE [dbo].[ConnectedUser]  WITH CHECK ADD  CONSTRAINT [FK_ConnectedUser_Users] FOREIGN KEY([UserId])
  REFERENCES [dbo].[Users] ([Id])
  ON DELETE CASCADE

  ALTER TABLE [dbo].[ConnectedUser] CHECK CONSTRAINT [FK_ConnectedUser_Users]
End
End
