Sei sulla pagina 1di 5

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;

SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;


SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS `lab` ;
CREATE SCHEMA IF NOT EXISTS `lab` DEFAULT CHARACTER SET latin1 COLLATE latin1_sw
edish_ci ;
USE `lab` ;
-- -----------------------------------------------------
-- Table `lab`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`users` ;
CREATE TABLE IF NOT EXISTS `lab`.`users` (
`UserID` INT NOT NULL AUTO_INCREMENT ,
`Username` VARCHAR(45) NOT NULL ,
`Password` VARCHAR(45) NOT NULL ,
`Permission` INT NULL ,
PRIMARY KEY (`UserID`) ,
UNIQUE INDEX `Username_UNIQUE` (`Username` ASC) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`labs`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`labs` ;
CREATE TABLE IF NOT EXISTS `lab`.`labs` (
`LabID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
`Site` VARCHAR(45) NULL ,
PRIMARY KEY (`LabID`) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`personnel`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`personnel` ;
CREATE TABLE IF NOT EXISTS `lab`.`personnel` (
`PersonnelID` INT NOT NULL AUTO_INCREMENT ,
`LabID` INT NOT NULL ,
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
PRIMARY KEY (`PersonnelID`) ,
INDEX `fk_personnel2` (`LabID` ASC) ,
UNIQUE INDEX `Email_UNIQUE` (`Email` ASC) ,
INDEX `fk_personnel1` (`UserID` ASC) ,
CONSTRAINT `fk_personnel2`
FOREIGN KEY (`LabID` )
REFERENCES `lab`.`labs` (`LabID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_personnel1`
FOREIGN KEY (`UserID` )
REFERENCES `lab`.`users` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`programmes`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`programmes` ;
CREATE TABLE IF NOT EXISTS `lab`.`programmes` (
`ProgrammeID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`ProgrammeID`) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`tests`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`tests` ;
CREATE TABLE IF NOT EXISTS `lab`.`tests` (
`TestID` INT NOT NULL AUTO_INCREMENT ,
`Title` VARCHAR(45) NOT NULL ,
`StartDate` TIMESTAMP NULL ,
`EndDate` TIMESTAMP NULL ,
`ProgrammeID` INT NOT NULL ,
PRIMARY KEY (`TestID`) ,
INDEX `fk_tests4` (`ProgrammeID` ASC) ,
CONSTRAINT `fk_tests4`
FOREIGN KEY (`ProgrammeID` )
REFERENCES `lab`.`programmes` (`ProgrammeID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`analytes`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`analytes` ;
CREATE TABLE IF NOT EXISTS `lab`.`analytes` (
`AnalyteID` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`AnalyteID`) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`samples`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`samples` ;
CREATE TABLE IF NOT EXISTS `lab`.`samples` (
`SampleID` INT NOT NULL ,
`Name` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`SampleID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lab`.`results`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`results` ;
CREATE TABLE IF NOT EXISTS `lab`.`results` (
`ResultID` INT NOT NULL ,
`LabID` INT NOT NULL ,
`TestID` INT NOT NULL ,
`AnalyteID` INT NOT NULL ,
`SampleID` INT NOT NULL ,
`SampleNo` VARCHAR(45) NULL ,
`Results` DECIMAL(20,10) NOT NULL ,
`ConfirmedBy` INT NULL ,
`Date` TIMESTAMP NULL DEFAULT now() ,
PRIMARY KEY (`ResultID`) ,
INDEX `fk_tests1` (`LabID` ASC) ,
INDEX `fk_tests2` (`AnalyteID` ASC) ,
INDEX `fk_tests3` (`TestID` ASC) ,
INDEX `fk_results1` (`SampleID` ASC) ,
CONSTRAINT `fk_tests1`
FOREIGN KEY (`LabID` )
REFERENCES `lab`.`labs` (`LabID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tests2`
FOREIGN KEY (`AnalyteID` )
REFERENCES `lab`.`analytes` (`AnalyteID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tests3`
FOREIGN KEY (`TestID` )
REFERENCES `lab`.`tests` (`TestID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_results1`
FOREIGN KEY (`SampleID` )
REFERENCES `lab`.`samples` (`SampleID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`machines`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`machines` ;
CREATE TABLE IF NOT EXISTS `lab`.`machines` (
`MachineID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NOT NULL ,
`Manufacturer` VARCHAR(45) NOT NULL ,
`Model` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`Model`, `Manufacturer`) ,
UNIQUE INDEX `MachineID_UNIQUE` (`MachineID` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lab`.`test_machine_reg`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`test_machine_reg` ;
CREATE TABLE IF NOT EXISTS `lab`.`test_machine_reg` (
`MachineID` INT NOT NULL ,
`TestID` INT NOT NULL ,
INDEX `fk_test_machine_reg` (`MachineID` ASC) ,
INDEX `fk_test_machine_reg1` (`TestID` ASC) ,
CONSTRAINT `fk_test_machine_reg`
FOREIGN KEY (`MachineID` )
REFERENCES `lab`.`machines` (`MachineID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_test_machine_reg1`
FOREIGN KEY (`TestID` )
REFERENCES `lab`.`results` (`ResultID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`test_personnel_reg`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`test_personnel_reg` ;
CREATE TABLE IF NOT EXISTS `lab`.`test_personnel_reg` (
`TestID` INT NOT NULL ,
`PersonnelID` INT NOT NULL ,
INDEX `fk_test_personnel_reg1` (`TestID` ASC) ,
INDEX `fk_test_personnel_reg2` (`PersonnelID` ASC) ,
CONSTRAINT `fk_test_personnel_reg1`
FOREIGN KEY (`TestID` )
REFERENCES `lab`.`results` (`ResultID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_test_personnel_reg2`
FOREIGN KEY (`PersonnelID` )
REFERENCES `lab`.`personnel` (`PersonnelID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `lab`.`admin`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lab`.`admin` ;
CREATE TABLE IF NOT EXISTS `lab`.`admin` (
`AdminID` INT NOT NULL AUTO_INCREMENT ,
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
PRIMARY KEY (`AdminID`) ,
INDEX `fk_admin1` (`UserID` ASC) ,
CONSTRAINT `fk_admin1`
FOREIGN KEY (`UserID` )
REFERENCES `lab`.`users` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Placeholder table for view `lab`.`resultview`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lab`.`resultview` (`Programme` INT, `Test` INT, `Sam
ple` INT, `'Sample_Number'` INT, `Analyte` INT, `Lab` INT, `'Added_By'` INT, `'R
esult'` INT);
-- -----------------------------------------------------
-- View `lab`.`resultview`
-- -----------------------------------------------------
DROP VIEW IF EXISTS `lab`.`resultview` ;
DROP TABLE IF EXISTS `lab`.`resultview`;
USE `lab`;
CREATE OR REPLACE VIEW `lab`.`resultview` AS
SELECT programmes.Name AS Programme, tests.Title AS Test, samples.Name AS Sample
, samples.SampleNo AS 'Sample_Number',
analytes.Name as Analyte, labs.Name as Lab, personnel.Name AS 'Added_By', r
esults.result AS 'Result'
from results, personnel, samples, tests, programmes
where (results.UserID=users.UserID, results.AnalyteID=analytes.AnalyteID, result
s.SampleID=samples.SampleID,
results.LabID=labs.LabID)
AND results.ConfirmedBy IS NOT NULL
;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;