MySQL Routine (Stored Procedure) To Insert a User Into tUser Table

We assume that you have read about personnel sub system in the hospital information system that presented before at http://databasescript.blogspot.com/2012/10/entity-relationship-model-of-hospital.html
In this article will take up about inserting a user id, password and other fields that related to tUser table in personnel sub system. This procedure of inserting a user will affect to the procedure of user login at the later. Here is the syntax of creating the routine.
CREATE DEFINER=`root`@`localhost` PROCEDURE `insertuser`(
   IN p_user_id varchar(20),
   IN p_encrypted_pass varbinary(100),
   IN p_personID varchar(18))
BEGIN
 DECLARE vMidUserId varchar(3);
 DECLARE vMidIdPegawai varchar(6);
 SET vMidUserId = MID(p_user_id,1,3);
 SET vMidIdPegawai = CONCAT(RIGHT(p_personID,2),LEFT(p_personID,4));

 INSERT INTO tuser(user_id, kata_sandi,idPegawai)
 VALUES(p_user_id,
       AES_ENCRYPT (p_encrypted_pass ,CONCAT(vMidUserId,vMidIdPegawai)),
       p_personID);
END 

One important thing in this routine is the encryption of the user password. Of course you can modify the AES_ENCRYPT option with one you like. The AES_ENCRYPT also use in the login stored procedure, and we will talk about it in the later article.

Entity Relationship Model of a Hospital-Based Database System

In the ER diagram of a Hospital-Based Database System, we can view the entities are Patient, Doctor, Receptionist, Department, Medicine, Test, etc. Among these entities, relationships exist which connect all the entities in the diagram. For example, Patient, Doctor and Receptionist are connected via the relationship Appointment. In other words, a receptionist will set up a doctor’s appointment for a patient. Similarly, Doctor, Patient and Medicine are connected via the relationship Prescription. Here, a doctor may prescribe one or more medicine to a patient. In a similar way, other entities are connected via relationships in a meaningful way.
In this article will be explained about entities or tables that consist in the sub system personnel/ employee of the hospital database.
This sub system maintain some functionalities such as user login, user permission on accessing the system, etc. These tables are created by using DDL syntax in MySql Workbench for MySql database management system. We assume that you are familiar with SQL syntax.

Person
CREATE TABLE Person(
  `personID` varchar(18) NOT NULL,
  `Name` varchar(45) DEFAULT NULL,
  `Is_Active` bit(1) DEFAULT NULL,
  PRIMARY KEY (`personID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This table will store all personnel/ employee of the hospital

Specialists
CREATE TABLE `specialists` (
  `specialistID` varchar(2) NOT NULL,
  `Title` varchar(15) DEFAULT NULL,
  `Specialist` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`specialistID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This table is for look up table used by Doctor/ Physician table.

Doctor/ Physician
CREATE TABLE `dokter` (
  `doctorID` varchar(4) NOT NULL,
  `personID` varchar(18) DEFAULT NULL,
  `specialistID` varchar(2) DEFAULT NULL,
  PRIMARY KEY (`doctorID`),
  UNIQUE KEY `personID_UNIQUE` (`personID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This table stores the identity of each doctor, and will be used in another sub system too such as emergency room sub system, ambulatory sub system, inpatient sub system, etc

User.
CREATE TABLE `tuser` (
  `user_id` varchar(20) NOT NULL,
  `encrypted_pass` varbinary(100) DEFAULT NULL,
  `personID` varchar(18) DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `personID_UNIQUE` (`personID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This table stores the user id and password for each person that use the hospital information system entirely. When aplication of each sub system is launched, user will be prompted to input user id and password, if user id and password is match then system will query to table that store every user permition to the system, this table may be "user_modul" or something else like that. Of course this happen at the application level/ user interface level.
May be we need more tables such as Nurse, Officials, etc. It will be presented in next articles.