Overview

MUMPS (Massachusetts General Hospital Utility Multi-Programming System), also known as M, is a language designed for building database applications. One of its core strengths is its integrated database management system (DBMS), which allows for efficient data storage, retrieval, and manipulation. This module will introduce you to the fundamental concepts of MUMPS databases, including their structure, operations, and unique features.

Key Concepts

  1. Global Variables

  • Definition: In MUMPS, global variables are used to store data persistently. They are accessible across different sessions and processes.
  • Syntax: Global variables are prefixed with a caret (^).
  • Example:
    SET ^Patient(1)="John Doe"
    SET ^Patient(2)="Jane Smith"
    

  1. Nodes and Subscripts

  • Nodes: Each global variable can have multiple nodes, which are essentially hierarchical levels.
  • Subscripts: Nodes are accessed using subscripts, which can be numeric or string values.
  • Example:
    SET ^Patient(1,"Name")="John Doe"
    SET ^Patient(1,"Age")=30
    SET ^Patient(2,"Name")="Jane Smith"
    SET ^Patient(2,"Age")=25
    

  1. Data Storage

  • Hierarchical Structure: MUMPS databases use a hierarchical structure, making it easy to represent complex data relationships.
  • Persistence: Data stored in global variables is persistent, meaning it remains available even after the program terminates.

  1. Data Retrieval

  • Direct Access: Data can be retrieved directly using the global variable and its subscripts.
  • Example:
    WRITE ^Patient(1,"Name")  ; Outputs: John Doe
    WRITE ^Patient(2,"Age")   ; Outputs: 25
    

Practical Examples

Example 1: Storing Patient Information

; Storing patient information
SET ^Patient(1,"Name")="John Doe"
SET ^Patient(1,"Age")=30
SET ^Patient(1,"Gender")="Male"

SET ^Patient(2,"Name")="Jane Smith"
SET ^Patient(2,"Age")=25
SET ^Patient(2,"Gender")="Female"

Example 2: Retrieving Patient Information

; Retrieving patient information
WRITE "Patient 1 Name: ", ^Patient(1,"Name"), !
WRITE "Patient 1 Age: ", ^Patient(1,"Age"), !
WRITE "Patient 1 Gender: ", ^Patient(1,"Gender"), !

WRITE "Patient 2 Name: ", ^Patient(2,"Name"), !
WRITE "Patient 2 Age: ", ^Patient(2,"Age"), !
WRITE "Patient 2 Gender: ", ^Patient(2,"Gender"), !

Example 3: Iterating Over Data

; Iterating over patient data
NEW patientID
SET patientID=0
FOR  SET patientID=$ORDER(^Patient(patientID)) QUIT:patientID=""  DO
. WRITE "Patient ID: ", patientID, !
. WRITE "Name: ", ^Patient(patientID,"Name"), !
. WRITE "Age: ", ^Patient(patientID,"Age"), !
. WRITE "Gender: ", ^Patient(patientID,"Gender"), !
. WRITE !

Exercises

Exercise 1: Store and Retrieve Employee Data

  1. Task: Create a global variable to store employee data, including ID, name, and department.
  2. Steps:
    • Store data for three employees.
    • Retrieve and display the data for each employee.

Solution:

; Storing employee data
SET ^Employee(1,"Name")="Alice Johnson"
SET ^Employee(1,"Department")="HR"

SET ^Employee(2,"Name")="Bob Brown"
SET ^Employee(2,"Department")="IT"

SET ^Employee(3,"Name")="Charlie Davis"
SET ^Employee(3,"Department")="Finance"

; Retrieving employee data
NEW empID
SET empID=0
FOR  SET empID=$ORDER(^Employee(empID)) QUIT:empID=""  DO
. WRITE "Employee ID: ", empID, !
. WRITE "Name: ", ^Employee(empID,"Name"), !
. WRITE "Department: ", ^Employee(empID,"Department"), !
. WRITE !

Exercise 2: Update and Delete Data

  1. Task: Update the department of an employee and delete an employee record.
  2. Steps:
    • Update the department of employee with ID 2 to "Marketing".
    • Delete the record of employee with ID 3.

Solution:

; Updating employee data
SET ^Employee(2,"Department")="Marketing"

; Deleting employee data
KILL ^Employee(3)

; Display updated data
NEW empID
SET empID=0
FOR  SET empID=$ORDER(^Employee(empID)) QUIT:empID=""  DO
. WRITE "Employee ID: ", empID, !
. WRITE "Name: ", ^Employee(empID,"Name"), !
. WRITE "Department: ", ^Employee(empID,"Department"), !
. WRITE !

Summary

In this section, you learned about the basics of MUMPS databases, including global variables, nodes, and subscripts. You also practiced storing, retrieving, updating, and deleting data using practical examples. Understanding these fundamental concepts is crucial for working with MUMPS databases effectively. In the next module, we will delve deeper into database operations, including more advanced data manipulation techniques.

© Copyright 2024. All rights reserved