How can the database structure be optimized to handle the scenario of removing individuals from rooms and retaining their information for future use?

To optimize the database structure for removing individuals from rooms while retaining their information, you can create a separate table to store historical data of individuals who have been removed from rooms. This table can include columns for individual ID, room ID, removal date, and any other relevant information. This way, the main room occupancy table can remain current while retaining a record of past occupants.

// Create a new table to store historical data of removed individuals
CREATE TABLE removed_individuals (
    id INT AUTO_INCREMENT PRIMARY KEY,
    individual_id INT,
    room_id INT,
    removal_date DATE,
    additional_info TEXT
);

// When removing an individual from a room, insert their information into the removed_individuals table
$individualId = 1;
$roomId = 2;
$removalDate = date('Y-m-d');
$additionalInfo = 'Reason for removal';

// Insert removed individual's information into the removed_individuals table
$query = "INSERT INTO removed_individuals (individual_id, room_id, removal_date, additional_info) 
          VALUES ($individualId, $roomId, '$removalDate', '$additionalInfo')";
mysqli_query($connection, $query);

// Remove the individual from the main room occupancy table
$deleteQuery = "DELETE FROM room_occupancy WHERE individual_id = $individualId AND room_id = $roomId";
mysqli_query($connection, $deleteQuery);