292 lines
8.0 KiB
PL/PgSQL
292 lines
8.0 KiB
PL/PgSQL
|
|
-- --------------------------------------------------
|
|
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
|
|
-- --------------------------------------------------
|
|
-- Date Created: 06/09/2018 13:24:19
|
|
-- Generated from Cadaroski Ismail
|
|
-----------------------------------------------------
|
|
-- CHANGE LOG:
|
|
-- 180618 Ivan, Add Create DB
|
|
-- 180621 Ismail, change Tables
|
|
-- 180629 Ivan, Add inserts for fixed lists
|
|
-- --------------------------------------------------
|
|
|
|
-- Set DB toSingle User so open connections do not block
|
|
-- --------------------------------------------------
|
|
PRINT 'Set Multiuser to Singleuser so all open connections are cut...';
|
|
use master
|
|
ALTER DATABASE EHEC_DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
|
|
-- --------------------------------------------------
|
|
|
|
|
|
|
|
-- Auskommentieren für Löschung der Datenbank
|
|
IF db_id('EHEC_DB') is not null
|
|
BEGIN;
|
|
PRINT 'EHEC_DB already EXISTS, db will be deleted for a new creation.'
|
|
USE [master];
|
|
DROP DATABASE EHEC_DB;
|
|
END;
|
|
ELSE
|
|
BEGIN;
|
|
PRINT 'EHEC_DB does NOT EXISTS, the db will be created now...';
|
|
END;
|
|
GO
|
|
|
|
-- Datenbank erstellen:
|
|
IF db_id('EHEC_DB') is null
|
|
BEGIN;
|
|
CREATE DATABASE EHEC_DB;
|
|
END;
|
|
ELSE
|
|
BEGIN;
|
|
PRINT 'EHEC_DB existiert bereits, Tabellen werden nun gelöscht...';
|
|
END;
|
|
GO
|
|
|
|
SET QUOTED_IDENTIFIER OFF;
|
|
GO
|
|
USE [EHEC_DB];
|
|
GO
|
|
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
|
|
GO
|
|
|
|
-- --------------------------------------------------
|
|
PRINT 'Dropping existing FOREIGN KEY constraints';
|
|
-- --------------------------------------------------
|
|
|
|
IF OBJECT_ID(N'[dbo].[FK_ResultExam]', 'F') IS NOT NULL
|
|
ALTER TABLE [dbo].[Exam] DROP CONSTRAINT [FK_ResultExam];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[FK_PatientExam]', 'F') IS NOT NULL
|
|
ALTER TABLE [dbo].[Exam] DROP CONSTRAINT [FK_PatientExam];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[FK_DoctorExam]', 'F') IS NOT NULL
|
|
ALTER TABLE [dbo].[Exam] DROP CONSTRAINT [FK_DoctorExam];
|
|
GO
|
|
|
|
IF OBJECT_ID(N'[dbo].[FK_Exam]', 'F') IS NOT NULL
|
|
ALTER TABLE [dbo].[Origin_Exam] DROP CONSTRAINT [FK_Exam];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[FK_Origin]', 'F') IS NOT NULL
|
|
ALTER TABLE [dbo].[Origin_Exam] DROP CONSTRAINT [FK_Origin];
|
|
GO
|
|
-- --------------------------------------------------
|
|
PRINT 'Dropping existing TABLES';
|
|
-- --------------------------------------------------
|
|
|
|
IF OBJECT_ID(N'[dbo].[Patient]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Patient];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[Doctor]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Doctor];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[Origin_Exam]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Origin_Exam];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[Origin]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Origin];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[Exam]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Exam];
|
|
GO
|
|
IF OBJECT_ID(N'[dbo].[Result]', 'U') IS NOT NULL
|
|
DROP TABLE [dbo].[Result];
|
|
GO
|
|
|
|
-- --------------------------------------------------
|
|
PRINT 'Creating all TABLES';
|
|
-- --------------------------------------------------
|
|
|
|
-- Creating table 'Patient'
|
|
CREATE TABLE [dbo].[Patient] (
|
|
[PatientId] 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,
|
|
[Region] nvarchar(max) NOT NULL
|
|
);
|
|
GO
|
|
|
|
-- Creating table 'Doctor'
|
|
CREATE TABLE [dbo].[Doctor] (
|
|
[DoctorId] int IDENTITY(1,1) NOT NULL,
|
|
[FirstName] nvarchar(max) NOT NULL,
|
|
[LastName] nvarchar(max) NOT NULL,
|
|
[DoctorOrigin] nvarchar(max) NOT NULL,
|
|
[Region] nvarchar(max) NOT NULL
|
|
);
|
|
GO
|
|
|
|
-- Creating table 'Origin'
|
|
CREATE TABLE [dbo].[Origin] (
|
|
[OriginId] int IDENTITY(1,1) NOT NULL,
|
|
[Name] nvarchar(max) NOT NULL,
|
|
[Food] nvarchar(max) NOT NULL,
|
|
[Street] nvarchar(max) NOT NULL,
|
|
[City] nvarchar(max) NOT NULL
|
|
);
|
|
GO
|
|
|
|
-- Creating table 'Exam'
|
|
CREATE TABLE [dbo].[Exam] (
|
|
[ExamId] int IDENTITY(1,1) NOT NULL,
|
|
[DoctorId] int NOT NULL,
|
|
[PatientId] int NOT NULL,
|
|
[ResultId] int NOT NULL
|
|
);
|
|
GO
|
|
|
|
-- Creating table 'Result'
|
|
CREATE TABLE [dbo].[Result] (
|
|
[ResultId] int IDENTITY(1,1) NOT NULL,
|
|
[Name] nvarchar(max) NOT NULL,
|
|
);
|
|
GO
|
|
|
|
-- Creating table 'Origin_Exam'
|
|
CREATE TABLE [dbo].[Origin_Exam] (
|
|
[Origin_ExamId] int IDENTITY(1,1) NOT NULL,
|
|
[OriginId] int NOT NULL,
|
|
[ExamId] int NOT NULL
|
|
);
|
|
GO
|
|
|
|
-- --------------------------------------------------
|
|
PRINT 'Creating all PRIMARY KEY constraints';
|
|
-- --------------------------------------------------
|
|
-- Creating primary key on [Origin_ExamId] in table 'Origin_Exam'
|
|
ALTER TABLE [dbo].[Origin_Exam]
|
|
ADD CONSTRAINT [PK_Origin_Exam]
|
|
PRIMARY KEY CLUSTERED ([Origin_ExamId] ASC);
|
|
GO
|
|
|
|
-- Creating primary key on [PatientId] in table 'Patient'
|
|
ALTER TABLE [dbo].[Patient]
|
|
ADD CONSTRAINT [PK_Patient]
|
|
PRIMARY KEY CLUSTERED ([PatientId] ASC);
|
|
GO
|
|
|
|
-- Creating primary key on [DoctorId] in table 'Doctor'
|
|
ALTER TABLE [dbo].[Doctor]
|
|
ADD CONSTRAINT [PK_Doctor]
|
|
PRIMARY KEY CLUSTERED ([DoctorId] ASC);
|
|
GO
|
|
|
|
-- Creating primary key on [OriginId] in table 'Origin'
|
|
ALTER TABLE [dbo].[Origin]
|
|
ADD CONSTRAINT [PK_Origin]
|
|
PRIMARY KEY CLUSTERED ([OriginId] ASC);
|
|
GO
|
|
|
|
-- Creating primary key on [ExamId] in table 'Exam'
|
|
ALTER TABLE [dbo].[Exam]
|
|
ADD CONSTRAINT [PK_Exam]
|
|
PRIMARY KEY CLUSTERED ([ExamId] ASC);
|
|
GO
|
|
|
|
-- Creating primary key on [ResultId] in table 'Result'
|
|
ALTER TABLE [dbo].[Result]
|
|
ADD CONSTRAINT [PK_Result]
|
|
PRIMARY KEY CLUSTERED ([ResultId] ASC);
|
|
GO
|
|
|
|
-- --------------------------------------------------
|
|
PRINT 'Creating all FOREIGN KEY constraints';
|
|
-- --------------------------------------------------
|
|
|
|
-- Creating foreign key on [ResultId] in table 'Exam'
|
|
ALTER TABLE [dbo].[Exam]
|
|
ADD CONSTRAINT [FK_ResultExam]
|
|
FOREIGN KEY ([ResultId])
|
|
REFERENCES [dbo].[Result]
|
|
([ResultId])
|
|
ON DELETE NO ACTION ON UPDATE NO ACTION;
|
|
GO
|
|
|
|
-- Creating foreign key on [DoctorId] in table 'Exam'
|
|
ALTER TABLE [dbo].[Exam]
|
|
ADD CONSTRAINT [FK_DoctorExam]
|
|
FOREIGN KEY ([DoctorId])
|
|
REFERENCES [dbo].[Doctor]
|
|
([DoctorId])
|
|
ON DELETE NO ACTION ON UPDATE NO ACTION;
|
|
GO
|
|
|
|
-- Creating foreign key on [PatientId] in table 'Exam'
|
|
ALTER TABLE [dbo].[Exam]
|
|
ADD CONSTRAINT [FK_PatientExam]
|
|
FOREIGN KEY ([PatientId])
|
|
REFERENCES [dbo].[Patient]
|
|
([PatientId])
|
|
ON DELETE NO ACTION ON UPDATE NO ACTION;
|
|
GO
|
|
|
|
-- Creating foreign key on [OriginId] in table 'Origin_Exam'
|
|
ALTER TABLE [dbo].[Origin_Exam]
|
|
ADD CONSTRAINT [FK_Origin]
|
|
FOREIGN KEY ([OriginId])
|
|
REFERENCES [dbo].[Origin]
|
|
([OriginId])
|
|
ON DELETE NO ACTION ON UPDATE NO ACTION;
|
|
GO
|
|
|
|
-- Creating foreign key on [ExamId] in table 'Origin_Exam'
|
|
ALTER TABLE [dbo].[Origin_Exam]
|
|
ADD CONSTRAINT [FK_Exam]
|
|
FOREIGN KEY ([ExamId])
|
|
REFERENCES [dbo].[Exam]
|
|
([ExamId])
|
|
ON DELETE NO ACTION ON UPDATE NO ACTION;
|
|
GO
|
|
-- --------------------------------------------------
|
|
PRINT 'Creating all INDEXES';
|
|
-- --------------------------------------------------
|
|
-- Creating non-clustered index for FOREIGN KEY 'FK_ResultExam'
|
|
CREATE INDEX [IX_FK_ResultExam]
|
|
ON [dbo].[Exam]
|
|
([ResultId]);
|
|
GO
|
|
-- Creating non-clustered index for FOREIGN KEY 'FK_DoctorExam'
|
|
CREATE INDEX [IX_FK_DoctorExam]
|
|
ON [dbo].[Exam]
|
|
([DoctorId]);
|
|
GO
|
|
-- Creating non-clustered index for FOREIGN KEY 'FK_DoctorExam'
|
|
CREATE INDEX [IX_FK_PatientExam]
|
|
ON [dbo].[Exam]
|
|
([PatientId]);
|
|
GO
|
|
-- Creating non-clustered index for FOREIGN KEY 'FK_Origin_Exam'
|
|
CREATE INDEX [IX_FK_Origin]
|
|
ON [dbo].[Origin_Exam]
|
|
([OriginId]);
|
|
GO
|
|
-- Creating non-clustered index for FOREIGN KEY 'FK_Origin_Exam'
|
|
CREATE INDEX [IX_FK_Exam]
|
|
ON [dbo].[Origin_Exam]
|
|
([ExamId]);
|
|
GO
|
|
|
|
-- --------------------------------------------------
|
|
PRINT 'Creating inserts for fixed list objects';
|
|
-- --------------------------------------------------
|
|
|
|
INSERT INTO Result(Name)
|
|
VALUES
|
|
('EHEC-A'),
|
|
('EHEC-B'),
|
|
('EHEC-C'),
|
|
('EHEC-D'),
|
|
('EHEC-E')
|
|
GO
|
|
-- --------------------------------------------------
|
|
PRINT 'Set Singleuser mode back to Multiuser mode.';
|
|
-- set DB back to Multiuser so multiconnections are alowed again.
|
|
ALTER DATABASE EHEC_DB SET MULTI_USER
|
|
-- --------------------------------------------------
|
|
PRINT 'Successfull created your DB!';
|
|
-- Script has ended
|
|
-- -------------------------------------------------- |