IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'RKaraNotifyUsers')
BEGIN

    CREATE TABLE [dbo].[RKaraNotifyUsers]
    (
        [Id] [UNIQUEIDENTIFIER] NOT NULL
            DEFAULT (NEWID()),
        [UserId] [SMALLINT] NULL,
        [NotifyId] [UNIQUEIDENTIFIER] NULL,
        [ReadOn] [SMALLDATETIME] NULL
            DEFAULT (GETDATE()),
        CONSTRAINT [PK_RKaraNotifyUsers]
            PRIMARY KEY CLUSTERED ([Id] ASC)
    ) ON [PRIMARY];
    IF (EXISTS
    (
        SELECT *
        FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
        WHERE CONSTRAINT_NAME = 'FK_RKaraNotifyUsers_RKaraNotify'
    )
       )
        ALTER TABLE RKaraNotifyUsers
        DROP CONSTRAINT FK_RKaraNotifyUsers_RKaraNotify;


    ALTER TABLE [dbo].[RKaraNotifyUsers] WITH CHECK
    ADD CONSTRAINT [FK_RKaraNotifyUsers_RKaraNotify]
        FOREIGN KEY ([NotifyId])
        REFERENCES [dbo].[RKaraNotify] ([Id]) ON DELETE CASCADE;

    ALTER TABLE [dbo].[RKaraNotifyUsers] CHECK CONSTRAINT [FK_RKaraNotifyUsers_RKaraNotify];

END;
