
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 06/09/2018 13:24:19
-- Generated from EDMX file: C:\Users\ism_c\source\repos\oop_II-6\Web_Server_EHEC\Web_Server_EHEC\Model\EHEC_DB_Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [EHEC_MODEL_DB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ResultExam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ExamSet] DROP CONSTRAINT [FK_ResultExam];
GO
IF OBJECT_ID(N'[dbo].[FK_DoctorExam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ExamSet] DROP CONSTRAINT [FK_DoctorExam];
GO
IF OBJECT_ID(N'[dbo].[FK_PatientExam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Patient] DROP CONSTRAINT [FK_PatientExam];
GO
IF OBJECT_ID(N'[dbo].[FK_OriginExam]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ExamSet] DROP CONSTRAINT [FK_OriginExam];
GO
IF OBJECT_ID(N'[dbo].[FK_DoctortypeDoctor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Doctor] DROP CONSTRAINT [FK_DoctortypeDoctor];
GO
IF OBJECT_ID(N'[dbo].[FK_Doctor_inherits_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Doctor] DROP CONSTRAINT [FK_Doctor_inherits_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_Patient_inherits_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonSet_Patient] DROP CONSTRAINT [FK_Patient_inherits_Person];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[PersonSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet];
GO
IF OBJECT_ID(N'[dbo].[OriginSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OriginSet];
GO
IF OBJECT_ID(N'[dbo].[ExamSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ExamSet];
GO
IF OBJECT_ID(N'[dbo].[ResultSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ResultSet];
GO
IF OBJECT_ID(N'[dbo].[DoctortypeSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DoctortypeSet];
GO
IF OBJECT_ID(N'[dbo].[PersonSet_Doctor]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet_Doctor];
GO
IF OBJECT_ID(N'[dbo].[PersonSet_Patient]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonSet_Patient];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'PersonSet'
CREATE TABLE [dbo].[PersonSet] (
    [PersonId] int IDENTITY(1,1) NOT NULL,
    [Firstname] nvarchar(max)  NOT NULL,
    [Lastname] nvarchar(max)  NOT NULL,
    [Birthdate] datetime  NOT NULL,
    [Street] nvarchar(max)  NOT NULL,
    [City] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'OriginSet'
CREATE TABLE [dbo].[OriginSet] (
    [OriginId] int IDENTITY(1,1) NOT NULL,
    [SicknessId] int  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Street] nvarchar(max)  NOT NULL,
    [City] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'ExamSet'
CREATE TABLE [dbo].[ExamSet] (
    [ExamId] int IDENTITY(1,1) NOT NULL,
    [DoctorId] int  NOT NULL,
    [SicknessStatus] bit  NOT NULL,
    [SicknessDesignation] nvarchar(max)  NOT NULL,
    [OriginOriginId] int  NOT NULL,
    [Result_ResultId] int  NOT NULL
);
GO

-- Creating table 'ResultSet'
CREATE TABLE [dbo].[ResultSet] (
    [ResultId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'DoctortypeSet'
CREATE TABLE [dbo].[DoctortypeSet] (
    [TypeId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'PersonSet_Doctor'
CREATE TABLE [dbo].[PersonSet_Doctor] (
    [DoctorId] int IDENTITY(1,1) NOT NULL,
    [DoctortypeId] int  NOT NULL,
    [PersonId] int  NOT NULL
);
GO

-- Creating table 'PersonSet_Patient'
CREATE TABLE [dbo].[PersonSet_Patient] (
    [PatientId] int IDENTITY(1,1) NOT NULL,
    [PersonId] int  NOT NULL,
    [Exam_ExamId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [PersonId] in table 'PersonSet'
ALTER TABLE [dbo].[PersonSet]
ADD CONSTRAINT [PK_PersonSet]
    PRIMARY KEY CLUSTERED ([PersonId] ASC);
GO

-- Creating primary key on [OriginId] in table 'OriginSet'
ALTER TABLE [dbo].[OriginSet]
ADD CONSTRAINT [PK_OriginSet]
    PRIMARY KEY CLUSTERED ([OriginId] ASC);
GO

-- Creating primary key on [ExamId] in table 'ExamSet'
ALTER TABLE [dbo].[ExamSet]
ADD CONSTRAINT [PK_ExamSet]
    PRIMARY KEY CLUSTERED ([ExamId] ASC);
GO

-- Creating primary key on [ResultId] in table 'ResultSet'
ALTER TABLE [dbo].[ResultSet]
ADD CONSTRAINT [PK_ResultSet]
    PRIMARY KEY CLUSTERED ([ResultId] ASC);
GO

-- Creating primary key on [TypeId] in table 'DoctortypeSet'
ALTER TABLE [dbo].[DoctortypeSet]
ADD CONSTRAINT [PK_DoctortypeSet]
    PRIMARY KEY CLUSTERED ([TypeId] ASC);
GO

-- Creating primary key on [PersonId] in table 'PersonSet_Doctor'
ALTER TABLE [dbo].[PersonSet_Doctor]
ADD CONSTRAINT [PK_PersonSet_Doctor]
    PRIMARY KEY CLUSTERED ([PersonId] ASC);
GO

-- Creating primary key on [PersonId] in table 'PersonSet_Patient'
ALTER TABLE [dbo].[PersonSet_Patient]
ADD CONSTRAINT [PK_PersonSet_Patient]
    PRIMARY KEY CLUSTERED ([PersonId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Result_ResultId] in table 'ExamSet'
ALTER TABLE [dbo].[ExamSet]
ADD CONSTRAINT [FK_ResultExam]
    FOREIGN KEY ([Result_ResultId])
    REFERENCES [dbo].[ResultSet]
        ([ResultId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_ResultExam'
CREATE INDEX [IX_FK_ResultExam]
ON [dbo].[ExamSet]
    ([Result_ResultId]);
GO

-- Creating foreign key on [DoctorId] in table 'ExamSet'
ALTER TABLE [dbo].[ExamSet]
ADD CONSTRAINT [FK_DoctorExam]
    FOREIGN KEY ([DoctorId])
    REFERENCES [dbo].[PersonSet_Doctor]
        ([PersonId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_DoctorExam'
CREATE INDEX [IX_FK_DoctorExam]
ON [dbo].[ExamSet]
    ([DoctorId]);
GO

-- Creating foreign key on [Exam_ExamId] in table 'PersonSet_Patient'
ALTER TABLE [dbo].[PersonSet_Patient]
ADD CONSTRAINT [FK_PatientExam]
    FOREIGN KEY ([Exam_ExamId])
    REFERENCES [dbo].[ExamSet]
        ([ExamId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_PatientExam'
CREATE INDEX [IX_FK_PatientExam]
ON [dbo].[PersonSet_Patient]
    ([Exam_ExamId]);
GO

-- Creating foreign key on [OriginOriginId] in table 'ExamSet'
ALTER TABLE [dbo].[ExamSet]
ADD CONSTRAINT [FK_OriginExam]
    FOREIGN KEY ([OriginOriginId])
    REFERENCES [dbo].[OriginSet]
        ([OriginId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_OriginExam'
CREATE INDEX [IX_FK_OriginExam]
ON [dbo].[ExamSet]
    ([OriginOriginId]);
GO

-- Creating foreign key on [DoctortypeId] in table 'PersonSet_Doctor'
ALTER TABLE [dbo].[PersonSet_Doctor]
ADD CONSTRAINT [FK_DoctortypeDoctor]
    FOREIGN KEY ([DoctortypeId])
    REFERENCES [dbo].[DoctortypeSet]
        ([TypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating non-clustered index for FOREIGN KEY 'FK_DoctortypeDoctor'
CREATE INDEX [IX_FK_DoctortypeDoctor]
ON [dbo].[PersonSet_Doctor]
    ([DoctortypeId]);
GO

-- Creating foreign key on [PersonId] in table 'PersonSet_Doctor'
ALTER TABLE [dbo].[PersonSet_Doctor]
ADD CONSTRAINT [FK_Doctor_inherits_Person]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[PersonSet]
        ([PersonId])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [PersonId] in table 'PersonSet_Patient'
ALTER TABLE [dbo].[PersonSet_Patient]
ADD CONSTRAINT [FK_Patient_inherits_Person]
    FOREIGN KEY ([PersonId])
    REFERENCES [dbo].[PersonSet]
        ([PersonId])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------