/*==============================================================*/
/* DBMS name:      Microsoft SQL Server 2008                    */
/* Created on:     10/12/2015 7:21:31 PM                        */
/*==============================================================*/


if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('RENDELES') and o.name = 'FK_RENDELES_RENDEL_VEVO')
alter table RENDELES
   drop constraint FK_RENDELES_RENDEL_VEVO
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('RENDELES_TETEL') and o.name = 'FK_RENDELES_ARU_SZERE_ARU_ARLI')
alter table RENDELES_TETEL
   drop constraint FK_RENDELES_ARU_SZERE_ARU_ARLI
go

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('RENDELES_TETEL') and o.name = 'FK_RENDELES_RENDELESH_RENDELES')
alter table RENDELES_TETEL
   drop constraint FK_RENDELES_RENDELESH_RENDELES
go

if exists (select 1
            from  sysobjects
           where  id = object_id('ARU_ARLISTA')
            and   type = 'U')
   drop table ARU_ARLISTA
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('RENDELES')
            and   name  = 'RENDEL_FK'
            and   indid > 0
            and   indid < 255)
   drop index RENDELES.RENDEL_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('RENDELES')
            and   type = 'U')
   drop table RENDELES
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('RENDELES_TETEL')
            and   name  = 'ARU_SZEREPEL_A_TETELBEN_FK'
            and   indid > 0
            and   indid < 255)
   drop index RENDELES_TETEL.ARU_SZEREPEL_A_TETELBEN_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('RENDELES_TETEL')
            and   name  = 'RENDELESHEZ_TARTOZIK_FK'
            and   indid > 0
            and   indid < 255)
   drop index RENDELES_TETEL.RENDELESHEZ_TARTOZIK_FK
go

if exists (select 1
            from  sysobjects
           where  id = object_id('RENDELES_TETEL')
            and   type = 'U')
   drop table RENDELES_TETEL
go

if exists (select 1
            from  sysobjects
           where  id = object_id('VEVO')
            and   type = 'U')
   drop table VEVO
go

/*==============================================================*/
/* Table: ARU_ARLISTA                                           */
/*==============================================================*/
create table ARU_ARLISTA (
   ARU_KOD              smallint             not null,
   MERTEKEGYSEG         char(10)             not null,
   ARU_MEGNEVEZES       varchar(20)          null,
   EGYSEGAR             numeric(15,2)        null,
   constraint PK_ARU_ARLISTA primary key nonclustered (ARU_KOD, MERTEKEGYSEG)
)
go

/*==============================================================*/
/* Table: RENDELES                                              */
/*==============================================================*/
create table RENDELES (
   RENDELES_SZ          smallint             not null,
   VEVO_KOD             smallint             not null,
   KELT_DATUM_          datetime             null,
   constraint PK_RENDELES primary key nonclustered (RENDELES_SZ)
)
go

/*==============================================================*/
/* Index: RENDEL_FK                                             */
/*==============================================================*/
create index RENDEL_FK on RENDELES (
VEVO_KOD ASC
)
go

/*==============================================================*/
/* Table: RENDELES_TETEL                                        */
/*==============================================================*/
create table RENDELES_TETEL (
   ARU_KOD              smallint             not null,
   MERTEKEGYSEG         char(10)             not null,
   RENDELES_SZ          smallint             not null,
   RENDELT_MENNY        numeric(12,2)        null,
   constraint PK_RENDELES_TETEL primary key nonclustered (ARU_KOD, MERTEKEGYSEG, RENDELES_SZ)
)
go

/*==============================================================*/
/* Index: RENDELESHEZ_TARTOZIK_FK                               */
/*==============================================================*/
create index RENDELESHEZ_TARTOZIK_FK on RENDELES_TETEL (
RENDELES_SZ ASC
)
go

/*==============================================================*/
/* Index: ARU_SZEREPEL_A_TETELBEN_FK                            */
/*==============================================================*/
create index ARU_SZEREPEL_A_TETELBEN_FK on RENDELES_TETEL (
ARU_KOD ASC,
MERTEKEGYSEG ASC
)
go

/*==============================================================*/
/* Table: VEVO                                                  */
/*==============================================================*/
create table VEVO (
   VEVO_KOD             smallint             not null,
   VEVO_NEV             varchar(30)          null,
   IR_SZAM              numeric(5)           not null default 24000
      constraint CKC_IR_SZAM_VEVO check (IR_SZAM between 20000 and 29999),
   HELYSEG              varchar(20)          null,
   UTCA_HSZ             varchar(20)          null,
   constraint PK_VEVO primary key nonclustered (VEVO_KOD)
)
go

alter table RENDELES
   add constraint FK_RENDELES_RENDEL_VEVO foreign key (VEVO_KOD)
      references VEVO (VEVO_KOD)
go

alter table RENDELES_TETEL
   add constraint FK_RENDELES_ARU_SZERE_ARU_ARLI foreign key (ARU_KOD, MERTEKEGYSEG)
      references ARU_ARLISTA (ARU_KOD, MERTEKEGYSEG)
go

alter table RENDELES_TETEL
   add constraint FK_RENDELES_RENDELESH_RENDELES foreign key (RENDELES_SZ)
      references RENDELES (RENDELES_SZ)
go

