if exists (select 1
            from  sysobjects
           where  id = object_id('tblPollOption')
            and   type = 'U')
alter table tblPollOption
   drop constraint FK_TBLPOLLO_RLTPOLLSU_TBLPOLLS;
if exists (select 1
            from  sysobjects
           where  id = object_id('tblPollRecord')
            and   type = 'U')
alter table tblPollRecord
   drop constraint FK_TBLPOLLR_RLTPOLLOP_TBLPOLLO;
if exists (select 1
            from  sysindexes
           where  id    = object_id('tblPollOption')
            and   name  = 'tblPollSubjectOption_FK'
            and   indid > 0
            and   indid < 255)
   drop index tblPollOption.tblPollSubjectOption_FK;
if exists (select 1
            from  sysindexes
           where  id    = object_id('tblPollRecord')
            and   name  = 'tblPollOptionRecord_FK'
            and   indid > 0
            and   indid < 255)
   drop index tblPollRecord.tblPollOptionRecord_FK;
if exists (select 1
            from  sysobjects
           where  id = object_id('tblPollOption')
            and   type = 'U')
   drop table tblPollOption;
if exists (select 1
            from  sysobjects
           where  id = object_id('tblPollRecord')
            and   type = 'U')
   drop table tblPollRecord;
if exists (select 1
            from  sysobjects
           where  id = object_id('tblPollSubject')
            and   type = 'U')
   drop table tblPollSubject;
create table [dbo].[tblPollSubject](
   subject_id           int                  IDENTITY(1,1) not null,
   subject_name         varchar(50)          not null,
   secret_ballot        bit                  not null default 1,
   closed		bit		     not null default 0,
   subject_deadline	datetime	     not null,
   subject_description  text                 null,
   constraint PK_TBLPOLLSUBJECT primary key nonclustered (subject_id)
);
create table [dbo].[tblPollOption](
   option_id            int                  IDENTITY(1,1) not null,
   subject_id           int                  not null,
   option_name          varchar(50)          not null,
   option_votes         int                  not null default 0
      constraint CKC_OPTION_VOTES_TBLPOLLO check (option_votes >= 0),
   constraint PK_TBLPOLLOPTION primary key nonclustered (option_id)
);
create index tblPollSubjectOption_FK on tblPollOption (
subject_id ASC
);
create table [dbo].[tblPollRecord](
   record_id            int                  IDENTITY(1,1) not null,
   option_id            int                  not null,
   user_id              varchar(50)          not null,
   constraint PK_TBLPOLLRECORD primary key nonclustered (record_id)
);
create index tblPollOptionRecord_FK on tblPollRecord (
option_id ASC
);