create table tblAufstatus
(
    ast_id             Counter               not null,
    ast_bezeichnung    Text(30)              not null,
    ast_nummer         Long                  null    ,
    constraint PK_TBLAUFSTATUS primary key (ast_id)
);

create table tblMengeneinheit
(
    men_id             Counter               not null,
    men_bezeichnung    Text(30)              not null,
    constraint PK_TBLMENGENEINHEIT primary key (men_id)
);

create table tblMitarbeiter
(
    mit_id             Counter               not null,
    mit_name           Text(50)              not null,
    mit_vorname        Text(50)              null    ,
    mit_gebdatum       DateTime              null    ,
    mit_kontakt        Text(255)             null    ,
    mit_bemerkung      Memo                  null    ,
    constraint PK_TBLMITARBEITER primary key (mit_id)
);

create table tblKontakttyp
(
    ktyp_id            Counter               not null,
    ktyp_name          Text(50)              not null,
    constraint PK_TBLKONTAKTTYP primary key (ktyp_id)
);

create table tblKunde
(
    kun_id             Counter               not null,
    kun_name           Text(50)              not null,
    kun_vorname        Text(50)              null    ,
    kun_bemerkung      Memo                  null    ,
    constraint PK_TBLKUNDE primary key (kun_id)
);

create table tblMaterialart
(
    mat_id             Counter               not null,
    men_id_f           Long                  not null,
    mat_name           Text(255)             not null,
    mat_preis          Currency              null    ,
    mat_bemerkung      Memo                  null    ,
    constraint PK_TBLMATERIALART primary key (mat_id)
);

create table tblKundenauftrag
(
    kauf_id            Counter               not null,
    kun_id_f           Long                  not null,
    ast_id_f           Long                  not null,
    kauf_datum         DateTime              not null,
    kauf_nummer        Text(255)             null    ,
    kauf_beschreibung  Text(255)             null    ,
    kauf_von           DateTime              null    ,
    kauf_bis           DateTime              null    ,
    kauf_bemerkung     Memo                  null    ,
    constraint PK_TBLKUNDENAUFTRAG primary key (kauf_id)
);

create table tblKauf_Mat
(
    kaufmat_id         Counter               not null,
    mat_id_f           Long                  not null,
    kauf_id_f          Long                  not null,
    kaufmat_menge      Single                null    ,
    kaufmat_bemerkung  Memo                  null    ,
    constraint PK_TBLKAUF_MAT primary key (kaufmat_id)
);

create table tblKauf_Mit
(
    kaufmit_id         Counter               not null,
    mit_id_f           Long                  not null,
    kauf_id_f          Long                  not null,
    kaufmit_von        DateTime              null    ,
    kaufmit_bis        DateTime              null    ,
    kaufmit_bemerkung  Memo                  null    ,
    constraint PK_TBLKAUF_MIT primary key (kaufmit_id)
);

create table tblKontakt
(
    kon_id             Counter               not null,
    ktyp_id_f          Long                  not null,
    kun_id_f           Long                  not null,
    kon_kontakt        Text(255)             null    ,
    kon_bemerkung      Memo                  null    ,
    constraint PK_TBLKONTAKT primary key (kon_id)
);

alter table tblMaterialart
    add constraint FK_TBLMATERIALART_REF_304_TBLM foreign key  (men_id_f)
       references tblMengeneinheit (men_id);

alter table tblKundenauftrag
    add constraint FK_TBLKUNDENAUFTRAG_REF_153_TB foreign key  (kun_id_f)
       references tblKunde (kun_id);

alter table tblKundenauftrag
    add constraint FK_TBLKUNDENAUFTRAG_REF_157_TB foreign key  (ast_id_f)
       references tblAufstatus (ast_id);

alter table tblKauf_Mat
    add constraint FK_TBLKAUF_MAT_REF_161_TBLMATE foreign key  (mat_id_f)
       references tblMaterialart (mat_id);

alter table tblKauf_Mat
    add constraint FK_TBLKAUF_MAT_REF_165_TBLKUND foreign key  (kauf_id_f)
       references tblKundenauftrag (kauf_id);

alter table tblKauf_Mit
    add constraint FK_TBLKAUF_MIT_REF_169_TBLMITA foreign key  (mit_id_f)
       references tblMitarbeiter (mit_id);

alter table tblKauf_Mit
    add constraint FK_TBLKAUF_MIT_REF_173_TBLKUND foreign key  (kauf_id_f)
       references tblKundenauftrag (kauf_id);

alter table tblKontakt
    add constraint FK_TBLKONTAKT_REF_177_TBLKONTA foreign key  (ktyp_id_f)
       references tblKontakttyp (ktyp_id);

alter table tblKontakt
    add constraint FK_TBLKONTAKT_REF_181_TBLKUNDE foreign key  (kun_id_f)
       references tblKunde (kun_id);

